#!/bin/sh
#
# This file should only be run automatically by make, not directly by
# the user.
#
# This script is part of the SUIF version control system.  It takes
# one argument, the label for the version to be made.  It looks this
# label up in the version database and if it finds it, it checks out
# all the source file revisions for that version.


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

ver_database_ropen
if (test ${?} != 0) ; then
    exit 1
fi

if (grep "^"${1}":" versions.ver > /dev/null) ; then
    echo > /dev/null
else
    echo "version does not exist" 1>&2
    rm -f versions.ver
    exit 1
fi

mv versions.ver tmp.versions.ver
if (test ${?} != 0) ; then
    echo "error moving versions.ver to tmp.versions.ver"
    rm -f versions.ver
    rm -f tmp.versions.ver
    exit 1
fi

awk '/^[0-9]/ {FOUND=0} ; /^'${1}':/ {FOUND=1}
     /^    / {if (FOUND == 1) print "ver_source_by_num " $1 " " $2}' \
     tmp.versions.ver | /bin/sh -e
if (test ${?} != 0) ; then
    echo "error retrieving sources for version "${1}
    rm -f tmp.versions.ver
    exit 1
fi

rm -f tmp.versions.ver
if (test ${?} != 0) ; then
    echo "unable to remove tmp.versions.ver file"
    exit 1
fi

rm -f stamp-version
if (test ${?} != 0) ; then
    echo "unable to remove old stamp-version file"
    exit 1
fi

echo ${1} > stamp-version
if (test ${?} != 0) ; then
    echo "unable to create stamp-version file"
    exit 1
fi

exit 0
