#!/bin/sh
# @(#)$tcsh$
#
# Copyright (c) 2005 Kimmo Suominen
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer
#    in the documentation and/or other materials provided with the
#    distribution.
# 3. The name of the author may not be used to endorse or promote
#    products derived from this software without specific prior
#    written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#
# Create tcsh release tar.gz and patch files.
# Before you run this follow the instructions in RELEASE-PROCEDURE
#
# 20050303  Kimmo Suominen
#
REPOSITORY=rebar.astron.com:/p/tcsh/cvsroot
PROG="$(basename "$0")"
set -e
#
if [ $# -ne 3 ]
then
    echo "Usage: $PROG major minor beta" 1>&2
    exit 1
fi
#
umask 022
#
WRKDIR=$(mktemp -d -t tcsh)
#
if [ ! -d "${WRKDIR}" ]
then
    echo "Creating work directory failed!" 1>&2
    exit 1
fi
#
cd ${WRKDIR} || exit 1
#
cleanit()
{
    (cd "$1" && rm -f \
	Announce* \
	BUG-TRACKING \
	MAKERELEASE \
	Makefile.ADMIN \
	RELEASE-PROCEDURE \
    )
    chmod -R u=rwX,go=rX "$1"
}
#
TagA=$(printf "TCSH%d_%02d_%02d" "${1}" "${2}" "${3}")
VerA=$(printf "%d.%02d.%02d" "${1}" "${2}" "${3}")
#
echo "Checking out ${TagA}"
cvs -d ${REPOSITORY} -q export -r "${TagA}" -d "tcsh-${VerA}" tcsh > /dev/null
cleanit "tcsh-${VerA}"
echo "Creating tarball"
tar -cf "tcsh-${VerA}.tar" "tcsh-${VerA}"
gzip -9 "tcsh-${VerA}.tar"
#
if [ "${3}" -gt 0 ]
then
    prev=$(expr "${3}" - 1 || true)
    TagB=$(printf "TCSH%d_%02d_%02d" "${1}" "${2}" "${prev}")
    VerB=$(printf "%d.%02d.%02d" "${1}" "${2}" "${prev}")
    pfile=$(printf "patch%d" "${3}")
    echo "Checking out ${TagB}"
    cvs -d ${REPOSITORY} -q export -r "${TagB}" -d "tcsh-${VerB}" tcsh > /dev/null
    cleanit "tcsh-${VerB}"
    echo "Creating patch"
    diff -purN "tcsh-${VerB}" "tcsh-${VerA}" > "${pfile}" || true
    gzip -9 "${pfile}"
fi
#
echo ""
echo "${WRKDIR}:"
ls -l "tcsh-${VerA}.tar.gz"
if [ "${3}" -gt 0 ]
then
    ls -l "${pfile}.gz"
fi
