#!/bin/sh
#
# This script makes a patch distribution for a SUIF package given two
# gzipped tar files, one for the old distribution and one for the new
# distribution.
#
# usage: make_non_basesuif_patch_distr <package-name> <old-version>
#                                      <new-version>
#


if test ${1}"" = "" ; then
    echo "no package name specified" 1>&2
    exit 1
fi

if test ${2}"" = "" ; then
    echo "no old-version specified" 1>&2
    exit 1
fi

if test ${3}"" = "" ; then
    echo "no new-version specified" 1>&2
    exit 1
fi


package_name=${1}
old_version=${2}
new_version=${3}


olddir=${package_name}-${old_version}
newdir=${package_name}-${new_version}
patchfile=${olddir}-to-${new_version}.patch



if test '!' -f ${olddir}.tar.gz ; then
    echo "Cannot find distribution file ${olddir}.tar.gz" 1>&2
    exit 1
fi

if test '!' -f ${newdir}.tar.gz ; then
    echo "Cannot find distribution file ${newdir}.tar.gz" 1>&2
    exit 1
fi



echo gunzip ${olddir}.tar.gz
gunzip ${olddir}.tar.gz
if test ${?} != 0 ; then
    echo "failed" 1>&2
    exit 1
fi

echo gunzip ${newdir}.tar.gz
gunzip ${newdir}.tar.gz
if test ${?} != 0 ; then
    echo "failed" 1>&2
    exit 1
fi

echo tar xf ${olddir}.tar
tar xf ${olddir}.tar
if test ${?} != 0 ; then
    echo "failed" 1>&2
    exit 1
fi

echo rm -f src/${package_name}
rm -f src/${package_name}
if test ${?} != 0 ; then
    echo "failed" 1>&2
    exit 1
fi

echo tar xf ${newdir}.tar
tar xf ${newdir}.tar
if test ${?} != 0 ; then
    echo "failed" 1>&2
    exit 1
fi

echo rm -f src/${package_name}
rm -f src/${package_name}
if test ${?} != 0 ; then
    echo "failed" 1>&2
    exit 1
fi

echo rm -f ${olddir}.tar
rm -f ${olddir}.tar
if test ${?} != 0 ; then
    echo "failed" 1>&2
    exit 1
fi

echo rm -f ${newdir}.tar
rm -f ${newdir}.tar
if test ${?} != 0 ; then
    echo "failed" 1>&2
    exit 1
fi



echo echo 'Prereq: '`cat src/${olddir}/stamp-version` '>' ${patchfile}
echo 'Prereq: '`cat src/${olddir}/stamp-version` > ${patchfile}
if test ${?} != 0 ; then
    echo "failed" 1>&2
    exit 1
fi

echo diff -crN src/${olddir}/stamp-version src/${newdir}/stamp-version \
        '1>&2 >>' ${patchfile}
diff -crN src/${olddir}/stamp-version src/${newdir}/stamp-version 1>&2 >> \
        ${patchfile}

echo rm -f src/${olddir}/stamp-version
rm -f src/${olddir}/stamp-version
if test ${?} != 0 ; then
    echo "failed" 1>&2
    exit 1
fi

echo rm -f src/${newdir}/stamp-version
rm -f src/${newdir}/stamp-version
if test ${?} != 0 ; then
    echo "failed" 1>&2
    exit 1
fi

echo diff -crN -x'^preformated_docs$' -x'^preformatted_docs$' \
        src/${olddir} src/${newdir} '1>&2 >>' ${patchfile}
diff -crN -x'^preformated_docs$' -x'^preformatted_docs$' \
        src/${olddir} src/${newdir} 1>&2 >> ${patchfile}



echo rm -rf src
rm -rf src
if test ${?} != 0 ; then
    echo "failed" 1>&2
    exit 1
fi



echo mkdir save
mkdir save
if test ${?} != 0 ; then
    echo "failed" 1>&2
    exit 1
fi

echo cp ${patchfile} save
cp ${patchfile} save
if test ${?} != 0 ; then
    echo "failed" 1>&2
    exit 1
fi

echo compress ${patchfile}
compress ${patchfile}
if test ${?} != 0 ; then
    echo "failed" 1>&2
    exit 1
fi

echo mv save/${patchfile} .
mv save/${patchfile} .
if test ${?} != 0 ; then
    echo "failed" 1>&2
    exit 1
fi

echo rmdir save
rmdir save
if test ${?} != 0 ; then
    echo "failed" 1>&2
    exit 1
fi

echo gzip ${patchfile}
gzip ${patchfile}
if test ${?} != 0 ; then
    echo "failed" 1>&2
    exit 1
fi
