#!/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 optional argument, the label for the new version.  If the label
# is not given and there have been no changes since the current
# version, there is no effect.  If the label is not given and there
# have been changes, the new label used will be the old one
# incremented by one place.
#
# Once the proper label has been determined, a new version will be
# created containing all the latest revisions of the sources and it
# will be made the current version.

tempdir="ver_build_current_tmp"

if (test ! "(" -d RCS ")" ) ; then
    exit 0
fi

if (test -f versions.ver) ; then
    rm -f versions.ver
fi

if (test ${1}"" = "") ; then
    if (test -f ${tempdir}) ; then
        echo "cannote 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

    current_version=`ver_find_by_attribute current`
    if (test ${?} != 0) ; then
        current_version=1.1
        rm -f ${tempdir}/*
        rmdir ${tempdir}
    else
        ver_list_current_revisions > ${tempdir}/current_revisions
        if (test ${?} != 0) ; then
            rm -f ${tempdir}/*
            rmdir ${tempdir}
            exit 1
        fi

        ver_list_sources_for_version ${current_version} > \
                ${tempdir}/sources_for_version
        if (test ${?} != 0) ; then
            rm -f ${tempdir}/*
            rmdir ${tempdir}
            exit 1
        fi

        if (diff ${tempdir}/current_revisions \
                 ${tempdir}/sources_for_version > /dev/null) ; then
            rm -f ${tempdir}/*
            rmdir ${tempdir}
            exit 0
        fi

        rm -f ${tempdir}/*
        rmdir ${tempdir}

        current_version=`ver_increment_ver ${current_version}`
        if (test ${?} != 0) ; then
            exit 1
        fi
    fi
else
    current_version=${1}
fi

echo making version ${current_version} of package ${PACKAGE_NAME}

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

exit 0
