#!/bin/sh
#
# # Copyright (C) 1989,1990,1991,1992 by
#	Wilfried Koch, Andreas Lampen, Axel Mahler, Juergen Nickelsen,
#	Wolfgang Obst and Ulrich Pralle 
# 
# This file is part of shapeTools.
#
# This software is published in the hope that it will be useful, but
# WITHOUT ANY WARRANTY for any part of this software to work correctly
# or as described in the manuals. See the ShapeTools Public License
# for details.
#
# Permission is granted to use, copy, modify, or distribute any part of
# this software but only under the conditions described in the ShapeTools 
# Public License. A copy of this license is supposed to have been given
# to you along with shapeTools in a file named LICENSE. Among other
# things, this copyright notice and the Public License must be
# preserved on all copies.

#

if [ "$1" = "-p" ]
  then
    patch=1
    shift
fi

ty=$1
sn=$2
vf=$3
pf=$4
pl="+PATCHLEVEL"
pls="pl"

if [ $# -ne 4 ];
  then 
  echo usage: gen_rel_id [-p] type systemname versionfile prerelfile 1>&2;
  exit 1;
fi

if [ $patch ]
then
  if [ ! -r $pl ]
    then
      echo "Warning: can't read $pl. Assuming patchlevel 0." 1>&2
      plev=0
    else
      plev=`cat $pl 2> /dev/null`
  fi
fi

pseq=`cat $pf 2> /dev/null`
if [ -n "$pseq" ]
then :
else
  pseq=0
fi

vnum=`vl -vnum -last $vf 2> /dev/null`
if [ -n "$vnum" ]
then :
else
  vnum="1.-1"
fi

case $ty in
  release)
    if [ $patch ]
    then
      rid=$sn-${vnum}${pls}$plev
    else
      rid=$sn-$vnum 
    fi;;
  prerelease)
    if pattr -uda norelease $vf > /dev/null 2>&1
      then nvnum=1.0;
      else nvnum=`echo $vnum | awk -F. '{ print $1 "." $2+1; }'`
    fi
    if [ $patch ]
    then
      nplnum=`expr $plev + 1`
      rid=$sn-${vnum}${pls}${nplnum}pre$pseq
    else
      rid=$sn-${nvnum}pre$pseq 
    fi;;
  lastrelease)
    ovnum=`echo $vnum | awk -F. '{ print $1 "." $2-1; }'`
    rid=$sn-$ovnum ;;
  lastprerelease)
    rid=$sn-${vnum}pre$pseq ;;
  *)
    echo invalid release type \"$ty\" >&2
    exit 1 ;;
esac

echo $rid
exit 0
