#!/bin/bash
# create - adevries
# renamed mk.cvs.tarballs - bame
# $Id: mk.cvs.tarballs,v 1.6 2001/10/24 20:27:19 bame Exp $

exec > /tmp/$(basename $0).log 2>&1

################### Lots of stuff you may want to customize here ######
# List of stuff in CVS to package
PACKAGES='linux palo gcc gdb'

# Number of older tarballs to keep around
KEEP=3

# Semi-stable directory with enough room to keep checked-out
# versions of PACKAGES
CHECKEDOUT=~ftp/cvs/checkedout

# Destination directory
FTPDIR=~ftp/cvs

# You'll want to add -z6 or something to CVS if you have a network link
CVS='cvs -qf'

# Where's the CVS archive?
#export CVSROOT=':pserver:anonymous@puffin.external.hp.com:/home/cvs/parisc'
export CVSROOT='/var/cvs'
#######################################################################

DATE=`date +%Y%m%d`

mkdir -p $CHECKEDOUT $FTPDIR
cd $CHECKEDOUT
for package in $PACKAGES
do
    changes=false
    if [ ! -d $package ]
    then
	# first update uses co
        $CVS co $package
	changes=true
    else
        $CVS update -APd $package |
		grep '^[^?] ' >/dev/null 2>&1 && changes=true
    fi

    # If there are no destination tarballs yet, need to make one
    if tarballs=$(cd $FTPDIR && ls -t $package-[0-9]*.tar.gz)
    then
        : all is well
    else
        tarballs=
	changes=true
    fi

    if $changes
    then
	(
	    # keep one previous package around -- remove other copies.
	    # removing copies now instead of later allows us to use less
	    # disk space
	    cd $FTPDIR
	    set -- $tarballs
	    while [ $KEEP != 0 -a $# != 0 ]
	    do
	        shift 1
	    done
	    [ $# != 0 ] && rm $*
	)
        TMPTARBALL=$FTPDIR/.$package.tar.gz
	# if the package is rolled more than once per day you might
	# want to change the DATE to include hours and minutes
        TARBALL=$FTPDIR/$package-$DATE.tar.gz
	tar -zcf $TMPTARBALL $package
	mv $TMPTARBALL $TARBALL
	ln -sf $package-$DATE.tar.gz $FTPDIR/$package-latest.tar.gz
    fi
done
