#!/bin/sh

set -e

PROJECT=PROJECTNAME
CVSR=CVSROOT

CVS="cvs -d$CVSR"
ABSPATH="`pwd`/$PROJECT"

rm -rf $PROJECT
mkdir $PROJECT
cd $PROJECT
$CVS import -m "Repository init" $PROJECT $PROJECT init
cd ..
rmdir $PROJECT
$CVS co $PROJECT
ls -d $PROJECT-* | sort -u | sed "s/$PROJECT-//" | while read VERSION ; do
    cd $ABSPATH
    echo "Starting $VERSION"
    find * -type f | grep -v CVS | while read fname; do 
        if [ ! -f "../$PROJECT-$VERSION/$fname" ] ; then 
            rm "$fname"
            cvs rm "$fname"
        fi
    done
    cp -r ../$PROJECT-$VERSION/. .
    while [ `cvs up 2>&1 | grep '^? ' | wc -l` -gt 0 ] ; do
        cvs up 2>&1 | grep '^? ' | sed -e 's/? /"/' -e 's/$/"/' | xargs cvs add
    done
    cvs commit -m "Update to $VERSION"
    $CVS rtag RELEASE_`echo $VERSION | sed 's/\./_/g'` $PROJECT
    echo "Done $VERSION"
done
