#!/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 no
# arguments.  It writes to standard output the current revision
# numbers of all the source files along with the source names in the
# format used by the versions.ver database.

tempdir="version_tmp"

if (test ! "(" -d RCS ")" ) ; then
    echo "missing RCS directory" 1>&2
    exit 1
fi

if (test -f ${tempdir}) ; then
    echo "cannot create temporary directory "${tempdir}", file exists" 1>&2
    exit 1
fi

if (test -d ${tempdir} ) ; then
    rm -f ${tempdir}/*
    rmdir ${tempdir}
fi

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

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

ln -s ../RCS RCS
if (test ${?} != 0) ; then
    exit 1
fi

if (test ! "(" "(" -r RCS/Makefile,v ")" -o \
               "(" -r RCS/Makefile,link ")" ")" ) ; then
    echo "cannot check out Makefile" 1>&2
    rm -f *
    cd ..
    rmdir ${tempdir}
    exit 1
fi

co -q Makefile
if test ${?} != 0 ; then
    exit 1
fi

VERSION=

gmake --no-print-directory source
if test ${?} != 0 ; then
    exit 1
fi

for sf in * ; do
    if test ${sf} != RCS ; then
        if test -r RCS/${sf},v ; then
            sf_file=${sf},v
        else
            if test -r RCS/${sf},link ; then
                sf_file=${sf},link
            else
                echo "unable to find either RCS file RCS/"${sf}",v or" \
                        "RCS/"${sf}",link" 1>&2
                exit 1
            fi
        fi
        echo "    "`head -1 RCS/${sf_file} | awk '{print $2}' - | \
                cut -f1 -d";"` ${sf}
    fi
done

sub_packages=`gmake --no-print-directory sub-packages`
if test ${?} != 0 ; then
    exit 1
fi

rm -f *

for sf in ${sub_packages} ; do
    if (test -f ../RCS/${sf}/versions.ver,v) ; then
        ln -s ../RCS/${sf} RCS
        echo "    "`ver_find_by_attribute current`" "${sf}
        rm -f *
    else
        echo "unable to find version info for sub-package "${sf} 1>&2
        exit 1
    fi
done

rm -f *
cd ..
rmdir ${tempdir}
