#!/bin/bash
#
# $Id: cvsup,v 1.1.2.1 2004/06/20 21:54:34 nikki Exp $
#
export CVS_RSH=ssh
TAG=
WO=0


if [ $# -eq 0 ]; then
    DIR=.
else
    if [ $# -ne 1 ]; then
        echo "usage: $0 <working directory>"
        exit
    fi
    DIR=$1
fi

if [ ! -d $DIR ]; then
    echo "no such directory '$DIR'"
    exit
fi

if [ ! -d $DIR/CVS ]; then
    echo "'$DIR' has no CVS directory!"
    exit
fi

if [ ! -f $DIR/CVS/Repository ]; then
    echo "'$DIR' has no CVS/Repository!"
    exit
fi

if [ ! -f $DIR/CVS/Root ]; then
    echo "'$DIR' has no CVS/Root!"
    exit
fi

if [ -f $DIR/CVS/Tag ]; then
    TAG="-r `cat $DIR/CVS/Tag | cut -c 2-`"
fi

REP="`cat $DIR/CVS/Repository`"
ROOT="`cat $DIR/CVS/Root`"

if [ "x$DIR" = "x." ]; then
    COMMAND="cvs -q -z3 -d $ROOT update -P -d $TAG"
else
    COMMAND="cvs -q -z3 -d $ROOT co $TAG -d $DIR $REP"
fi

if [ ! -w $DIR/CVS ]; then
    if [ -O $DIR/CVS ]; then
        WO=1
        echo "Making $DIR writable"
        chmod -R u+w $DIR
    fi
fi

echo "Updating directory $DIR with $TAG $REP..."
echo "$COMMAND"

$COMMAND

if [ $WO -eq 1 ]; then
    echo "Making $DIR read-only"
    chmod -R a-w $DIR
fi

