#!/bin/sh
#
# This script collects all of the files for a particular version of
# the base compiler system, creates the basic directory structure, and
# puts it all into a compressed tar file to be distributed.
#
# usage: pack_basesuif_distr version_number
#

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

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

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

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

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

ln -s ${SUIFHOME}/RCS RCS
if test ${?} != 0 ; then
    echo "unable to link with 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 src
if test ${?} != 0 ; then
    echo "unable to make directory src" 1>&2
    exit 1
fi

for sf in * ; do
    if test "(" -d ${sf} ")" -a "(" ${sf} != src ")" ; then
        mv ${sf} src
        if test ${?} != 0 ; then
            echo "unable to mv "${sf} 1>&2
            exit 1
        fi
    fi
done

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

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

cp src/basesuif/miscdocs/suif-overview.ps docs
if test ${?} != 0 ; then
    echo "unable to copy suif-overview.ps to docs directory" 1>&2
    exit 1
fi

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

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

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

docdir=${startdir}/${packandver}

for sf in * ; do
    cd ${sf}
    if test ${?} != 0 ; then
        echo "unable to enter "${sf}" directory" 1>&2
        exit 1
    fi

    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
        exit 1
    fi

    gmake clean
    if test ${?} != 0 ; then
        exit 1
    fi

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

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

rm -rf man

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

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

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

rm -rf ${packandver}
if test ${?} != 0 ; then
    echo "unable to cleanup \""${packandver}"\" 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
