#!/bin/sh
#
usage="Call: $0 [-p Parameter] -c Command FileName"

PARAMETER=""
export PARAMETER

CVS=cvs
export CVS

#
# Parse Command-Line Parameters
#

while getopts hp:c: arg
do
  case $arg in
    p) PARAMETER=$OPTARG;;
    c) COMMAND=$OPTARG;;
    h|\?) echo $usage; exit 1;;
  esac
done  

#
# Get the Filename
#
shift `expr $OPTIND - 1`
if [ $# -gt 1 ]
then
  echo "More than one file name !!"
  echo $usage
  exit 10
else
  FILENAME=$1
fi  


if [ ! -d $IPSENCVS ]
then
  echo "Can't find cvs working directory"
  exit 1
fi

#
# Change the directory
#
OldPWD=$PWD
cd $IPSENCVS

if [ "$COMMAND" = "commit" ]
then
  $CVS add $FILENAME > /dev/null 2>&1
fi

if [ "$COMMAND" = "checkout" ]
then
  COMMAND="update"
fi

#
# Execute the cvs-command
#
if [ "$COMMAND" = "init" ]
then
  if [ -d CVS ] 
  then
    echo "Repository already exists!" > ERRORFILE
  else
    # Erst mal das init des Repositories
    $CVS $COMMAND > ERRORFILE 2>&1
    # Dann der Import des 'Working-Directories' ohne ERRORFILE und LOGFILE
    echo "ERRORFILE" >> .cvsignore
    echo "LOGFILE" >> .cvsignore
    $CVS import -m "My First Import" $FILENAME VTag RTag >> ERRORFILE 2>&1
    # Und nu das auschecken des 'Working-Directories'
    cd ..
    $CVS checkout $FILENAME >> ERRORFILE 2>&1
  fi
else
  $CVS $COMMAND $PARAMETER $FILENAME > ERRORFILE 2>&1
fi

cd $OldPWD




