#!/bin/sh
#
# This script collects all of the files for a particular version of a
# particular SUIF package and puts them together into distributable
# compressed tar files, including preformatting the documentation.
#
# usage: pack_distr <package-name> <version-label>
#

if test ${1}"" = "" ; then
    echo "no package specified" 1>&2
    echo 1>&2
    echo "usage: pack_distr <package-name> <version-label>" 1>&2
    echo 1>&2
    exit 1
fi

if test ${2}"" = "" ; then
    echo "no version specified" 1>&2
    echo 1>&2
    echo "usage: pack_distr <package-name> <version-label>" 1>&2
    echo 1>&2
    exit 1
fi

package=${1}
version=${2}
packandver=${package}-${version}
tarfile=${packandver}.tar

if test ${package} = basesuif ; then
    echo "`basesuif' is a special case; use pack_basesuif_distr instead" 1>&2
    exit 1
else
    echo > /dev/null
fi

startdir=`pwd`
if test ${?} != 0 ; then
    echo "unable to determine starting directory" 1>&2
    exit 1
fi

mkdir src
if test ${?} != 0 ; then
    echo "unable to create \"src\" directory" 1>&2
    exit 1
fi

mkdir src/${packandver}
if test ${?} != 0 ; then
    echo "unable to create \"src/"${packandver}"\" directory" 1>&2
    exit 1
fi

cd src/${packandver}
if test ${?} != 0 ; then
    echo "unable to cd to \"src/"${packandver}"\" directory" 1>&2
    exit 1
fi

ln -s ${SUIFHOME}/RCS/${package} RCS
if test ${?} != 0 ; then
    echo "unable to link RCS directory" 1>&2
    exit 1
fi

ver_get_sources_for_version ${version}
if test ${?} != 0 ; then
    echo "unable to get sources for version "${version} 1>&2
    exit 1
fi

gmake clean-rcs-links
if test ${?} != 0 ; then
    echo "unable to remove RCS directories" 1>&2
    exit 1
fi

mkdir preformatted_docs
if test ${?} != 0 ; then
    echo "unable to create \"preformatted_docs\" directory" 1>&2
    exit 1
fi

mkdir preformatted_docs/info preformatted_docs/docs preformatted_docs/html \
        preformatted_docs/man
if test ${?} != 0 ; then
    echo "unable to create subdirectories of \"preformatted_docs\" directory" \
            1>&2
    exit 1
fi

docdir=${startdir}/src/${packandver}/preformatted_docs

gmake install-info BUILD_INFO=yes BUILD_PS=yes BUILD_HTML=yes \
        INFODIR=${docdir}/info DOCDIR=${docdir}/docs HTMLDIR=${docdir}/html \
        MANDIR=${docdir}/man
if test ${?} != 0 ; then
    echo "unable to build pre-formated docs" 1>&2
    exit 1
fi

gmake clean
if test ${?} != 0 ; then
    echo "unable to cleanup after building pre-formated docs" 1>&2
    exit 1
fi

rm -rf preformatted_docs/man
if test ${?} != 0 ; then
    echo "unable to cleanup man directory after building pre-formated docs" \
            1>&2
    exit 1
fi

# Now remove other directories only if they're empty.  It's ok for
# these rmdir commands to fail -- they're supposed to if the
# directories aren't empty.

rmdir preformatted_docs/info > /dev/null 2>&1
rmdir preformatted_docs/docs > /dev/null 2>&1
rmdir preformatted_docs/html > /dev/null 2>&1
rmdir preformatted_docs > /dev/null 2>&1

cd ..
if test ${?} != 0 ; then
    echo "unable to cd from \"src/"${packandver}"\" directory" 1>&2
    exit 1
fi

ln -s ${packandver} ${package}
if test ${?} != 0 ; then
    echo "unable to link `"${package}"' to `"${packandver}"'" 1>&2
    exit 1
fi

cd ..
if test ${?} != 0 ; then
    echo "unable to cd from \"src\" directory" 1>&2
    exit 1
fi

rm -f ${tarfile}*
if test ${?} != 0 ; then
    echo "unable to remove old tar files" 1>&2
    exit 1
fi

echo tar cf ${tarfile} src
tar cf ${tarfile} src
if test ${?} != 0 ; then
    echo "tar failure" 1>&2
    exit 1
fi

rm -rf src
if test ${?} != 0 ; then
    echo "unable to cleanup \"src\" directory" 1>&2
    exit 1
fi

mkdir save
if test ${?} != 0 ; then
    echo "unable to create \"save\" directory" 1>&2
    exit 1
fi

cp ${tarfile} save
if test ${?} != 0 ; then
    echo "unable to copy \""${tarfile}"\" to \"save\" directory" 1>&2
    exit 1
fi

echo compress ${tarfile}
compress ${tarfile}
if test ${?} != 0 ; then
    echo "unable to compress \""${tarfile}"\"" 1>&2
    exit 1
fi

mv save/${tarfile} .
if test ${?} != 0 ; then
    echo "unable to move \""${tarfile}"\" from \"save\" directory" 1>&2
    exit 1
fi

rmdir save
if test ${?} != 0 ; then
    echo "unable to remove \"save\" directory" 1>&2
    exit 1
fi

echo gzip ${tarfile}
gzip ${tarfile}
if test ${?} != 0 ; then
    echo "unable to gzip \""${tarfile}"\"" 1>&2
    exit 1
fi
