#! /bin/csh -f
#
#  script to centralize version information in NEMO in the following files:
#       VERSION
#       inc/version.h
#
set v=$NEMO/VERSION
set h=$NEMOINC/version.h

if (! -e $v) then
    echo You are using an old version of NEMO without version control
    if (-e $h) then
        echo But you do have a version header file...
    endif
    exit 1
endif


set version=`cat $v`
set patch=$version:e
set id=$version:r
set minor=$id:e
set major=$id:r


if ($#argv == 0) then
  echo $version
  exit 0
endif

switch ($1)
  case "inc*":
        @ patch++
        breaksw
  case "set":
        set version=$2
        set patch=$version:e
        set id=$version:r
        set minor=$id:e
        set major=$id:r
        breaksw
  case "help":
  case "-h":
  case "-help":
  case "--help":
        goto usage
  default:
        echo 'Bad command, try none, or "help"'
        exit 1
endsw

set version=$major.$minor.$patch

echo $version

echo updating $v
echo $version                                             >! $v

echo updating $h
echo "/* do not edit - created by the src/scripts/version script */"  >! $h
echo "#if defined(GETPARAM_VERSION_ID)"                   >>! $h
echo -n 'static char *version_h = "$'                    >>! $h
echo 'Id: $";'                                           >>! $h
echo "#endif"                                            >>! $h 
echo '#define NEMO_VERSION "'$version'"'                 >>! $h

#  touch it again, so it's the last one touched :-)
touch $v

exit 0


#-----------------------------------------------------------------------
# alternate exit

usage:
 echo "By default version shows the current NEMO version"
 echo "Optional commands are:"
 echo "  set MAJOR.MINOR.PATCH      set a new version id (note 3 components)"
 echo "  inc                        increment patch level" 
 echo "        Note: both these commands write the version ID into NEMO"
 echo '        as maintained in $NEMO/.version and $NEMOINC/version.h'
 exit 0
