#! /bin/sh
#
# File: project
# Description: project managment tool, take care for release/version/revision.
#              
# Programmer: stolz (Horst Stolz)
# Time-stamp: <95/03/03 16:00:33 stolz>
#       
# try project -help for an usage-text!
#

# error message function
err () {
        echo "`basename $0`: $@" 1>&2
        exit 1
}

PROJECTDIR=.project
CURRDIR=`pwd`

# parse command line args
while [ $# -gt 0 ]; do
  case "$1" in
  -info|info)
    cd $PROJECTDIR
    echo "PROJECT INFORMATION"
    echo "Name   :" `cat name`
    echo "User   :" `cat user`
    echo "ID     :" `cat id`
    echo "Version:" `cat release`.`cat version`.`cat revision`
    echo Last Backup Nr.`cat backupnr`, `cat backupdate`
    echo "Backupfile:" `cat backupfile`
    cd $CURRDIR	
    ;;
  -install|install)
    if [ $# -lt 4 ]; then
	err "$1 requires projectname username and project-id as arguments"
    fi

    mkdir -p $PROJECTDIR
    cd $PROJECTDIR
    echo "$2" >name
    echo "$3" >user
    echo "$4" >id
    echo "0" >release
    echo "0" >version
    echo "0" >revision
    echo "0" >backupnr
    echo "<NO BACKUP>" >backupdate
    echo "<NONE>" >backupfile
    cd $CURRDIR

    shift
    shift
    shift
    updaterevc=1
    ;;	
  -rel|rel)
    if [ $# -lt 2 ]; then
	err "$1 requires release-number as arguments"
    fi
    echo "SET RELEASE TO $2"
    echo "$2" >$PROJECTDIR/release
    echo "0" >$PROJECTDIR/version
    echo "0" >$PROJECTDIR/revision 
    shift
    updaterevc=1
    ;;	
  -ver|ver)
    if [ $# -lt 2 ]; then
	err "$1 requires version-number as arguments"
    fi
    echo "SET VERSION TO $2"
    echo "$2" >$PROJECTDIR/version
    echo "0" >$PROJECTDIR/revision 
    shift
    updaterevc=1
    ;;	
  -rev|rev)
    if [ $# -lt 2 ]; then
	err "$1 requires revision-number as arguments"
    fi
    echo "SET REVISION TO $2"
    echo "$2" >$PROJECTDIR/revision
    shift
    updaterevc=1
    ;;	
  -increv|increv)
    cd $PROJECTDIR
    expr 1 + `cat revision` >revision
    echo CREATE NEW REVISION `cat revision` OF PROJECT `cat name`
    cd $CURRDIR
    updaterevc=1
    ;;
  -incver|incver)
    cd $PROJECTDIR
    expr 1 + `cat version` >version
    echo "0">revision
    echo CREATE NEW VERSION `cat version` OF PROJECT `cat name`
    cd $CURRDIR
    updaterevc=1
    ;;
  -increl|increl)
    cd $PROJECTDIR
    expr 1 + `cat release` >release
    echo "0">revision
    echo "0">version
    echo CREATE NEW RELEASE `cat release` OF PROJECT `cat name`
    cd $CURRDIR
    updaterevc=1
    ;;
  -mail|mail)
    if [ $# -lt 4 ]; then
	err "$1 requires adrs file text as arguments"
    fi
    cd $PROJECTDIR
    SUBJECT="Project information for "`cat name`
    echo "MAILING MESSAGE \"$3\" TO $2."
    echo "Hello," >mail.txt
    echo this is an information message of the project `cat name`  Version `cat release`.`cat version`.`cat revision`. >>mail.txt
    echo " " >>mail.txt
    echo "$4" >>mail.txt
    echo " " >>mail.txt
    cat -s $CURRDIR/$3 >>mail.txt
    echo " " >>mail.txt
    echo " " >>mail.txt
    echo "i.A." `cat user`", Your Automatic Project Mailer =P*)" >>mail.txt
    echo "." >>mail.txt

    mail -s "${SUBJECT}" $2 <mail.txt
    rm -f mail.txt
    cd $CURRDIR
    shift
    shift
    shift
    ;;	
  -backup|backup)
    cd $PROJECTDIR
    expr 1 + `cat backupnr` >backupnr
    date >backupdate
    BACKUPFILE=`cat name``cat backupnr`-`cat release`.`cat version`.`cat revision`.tar.gz
    echo "../$BACKUPFILE" >backupfile
    echo "BACKUP#"`cat backupnr` "OF CURRENT DIR TO FILE ../$BACKUPFILE"
    cd $CURRDIR
    cd ../
    echo "tar cf - `basename $CURRDIR` | gzip >$BACKUPFILE"
    tar cf - `basename $CURRDIR` | gzip >$BACKUPFILE
    cd $CURRDIR
    ;;
  -update|update)
    if [ $# -lt 2 ]; then
	err "$1 requires an archivename as argument"
    fi
    echo "DO UPDATE WITH FILE $2."
    rm -rf ${CURRDIR}_obsolent
    mkdir ${CURRDIR}_update
    cd ${CURRDIR}/..
    gzcat <$CURRDIR/$2 | tar xf -
    mv $CURRDIR ${CURRDIR}_obsolent
    mv ${CURRDIR}_update $CURRDIR
    cd $CURRDIR/$PROJECTDIR
    echo UPDATED TO `cat name` Version `cat release`.`cat version`.`cat revision`
    cd $CURRDIR
    shift
    ;;	
  [hH] | -*)
    echo "#######################################################"
    echo "USAGE: project [-install <name> <user> <id>>]"
    echo "               [-help] [-info]"
    echo "               [-backup] [-update <backupfile>]"
    echo "               [-mail <adrs> <file> <text>]"
    echo "               [-rel <nr>] [-ver <nr>] [-rev <nr>]"
    echo "               [-increl] [-incver] [-increv]"
    echo ""
    echo "DESCRIPTION: Invoke the raymany-script on the computers"
    echo "  defined by the <list of hostnames> to render the pov-"
    echo "  files in the <directory> by the pov-raytracer. The"
    echo "  imagesize can be defined be the options -width and"
    echo "  -height."
    echo "  \"raystart\" redirects the output of the raymany-script"
    echo "  to out.\$HOST. and save the list of hosts at the file"
    echo "  \"list.computers\". The image-dimension are saved at"
    echo "  \"widthxheight\". The pid-numbers of the rsh-commands"
    echo "  are stored in \$HOST.pid. All files are located in the"
    echo "  dir <directory>."
    echo "  Use the \"rayclear\ <directory>-script to stop all"
    echo "  running raymany-scripts or if something has gone wrong!"
    echo "  The default name of the povraytrace is \"povray\". This can"
    echo "  be overridden by the -raycommand-option."
    echo "  If the *.pov-files are very big, use -zippov to compress"
    echo "  them after the render-prozess."
    echo "  If you have access to a machine with an different login-"
    echo "  name. Write the loginname into the file \$HOME/\$HOST.login"
    echo "  and turn on the -login option."
    echo ""
    echo "  For an selective kill use the stored pid-nummbers."
    echo ""
    echo "  To render with new image-dimension remove all *.ppm.Z"
    echo "  and widthxheight and *.tga-file and invoke rayclear -cleanup."
    echo ""
    echo "  WARNINGS: 1. don't use a hostname twice!"
    echo "            2. use a path who is valid on all hosts!"
    echo "            3. never kill the povray-command only, cause"
    echo "               the shell-script keep running!"
    echo ""
    echo "ENVIRONMENT:"
    echo "  The variable RAYSTART_COMPUTERS overwrites the default"
    echo "  list of hostnames."
    echo "  Please use POVRAYOPT to spezifie the include-path!"  
    echo ""
    echo "DEFAULT SETTINGS:"
    echo "name = $raydir, width = $width, height = $height"
    echo "computers = $comlist"
    exit 1
    ;;
  *)
    err "unknown option $1"
    ;;
  esac
  shift
done

# generate rev.c
if [ $updaterevc ]; then
  echo "GENERATE VERSION FILE rev.c"    
  cd $PROJECTDIR
  PROJECT=`cat name`
  USER=`cat user`
  ID=`cat id`
  RELEASE=`cat release`
  VERSION=`cat version`
  REVISION=`cat revision`
  cd $CURRDIR
  rm -f rev.c
  echo "/* automatic rev-file for project ${PROJECT}," `date` "*/" >rev.c
  echo "char project_version_string[] = \"$RELEASE.$VERSION.$REVISION\";" >>rev.c
  echo "int project_release_nr = $RELEASE;" >>rev.c
  echo "int project_version_nr = $VERSION;" >>rev.c
  echo "int project_revision_nr = $REVISION;" >>rev.c
  echo "char project_user_string[] = \"$USER\";" >>rev.c
  echo "char project_id_string[] = \"$ID\";" >>rev.c
fi
