#! /bin/sh
# This file is part of the
#
#      Delta Project  (ConversationBuilder)
#      Human-Computer Interaction Laboratory
#      University of Illinois at Urbana-Champaign
#      Department of Computer Science
#      1304 W. Springfield Avenue
#      Urbana, Illinois 61801
#      USA
#
#      c 1989,1990,1991,1992 Board of Trustees
#              University of Illinois
#              All Rights Reserved
#
# This code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY. No author or distributor accepts
# responsibility to anyone for the consequences of using this code
# or for whether it serves any particular purpose or works at all,
# unless explicitly stated in a written agreement.
#
# Everyone is granted permission to copy, modify and redistribute
# this code, except that the original author(s) must be given due credit,
# and this copyright notice must be preserved on all copies.
#
#      Author:  Doug Bogia (bogia@cs.uiuc.edu)
#
#      Project Leader:  Simon Kaplan (kaplan@cs.uiuc.edu)
#      Direct enquiries to the project leader please.
#

Usage=0
if [ $# -eq 0 ]; then
  Usage=1
else
  MAJOR=""
  MINOR=""
  CHANGE=""
  COMPILATION=""
  while [ $# -gt 1 ]; do  
    case $1 in
    -major)
      MAJOR=$2
      ;;
    -minor)
      MINOR=$2
      ;;
    -change)
      CHANGE=$2
      ;;
    -compilation)
      COMPILATION=$2
      ;;
    *)
      Usage=1
    esac
    shift; shift
  done
fi

if [ $# -eq 1 ]; then
  File=$1
else
  Usage=1;
fi

if [ $Usage -ne 0 ]; then
  echo "Usage:  `basename $0` [-major string|+ -minor string|+"
  echo "           -change string|+ -compilation string|+] file"
  echo "where:  string is replaced by the string you want to represent each"
  echo "        of the version types or + if you know the version number"
  echo "        is a number and you want it incremented"
  echo "        file is the name of the file you wish to have changed."
  echo "        It is assumed that the lines #define MAJOR_NUM, #define"
  echo "        MINOR_NUM, #define CHANGE_NUM, and #define COMPILATION_NUM"
  echo "        will be found in this file."
  exit
fi

if [ "$MAJOR" != "" ]; then
  if [ "$MAJOR" = "+" ]; then
    MAJOR=`awk '/^#define MAJOR_NUM/	{printf ("%d", substr($3, 2, length($3)-2)+1)}' $File`
  fi
  sed "s/#define MAJOR_NUM \".*\"/#define MAJOR_NUM \"$MAJOR\"/" $File > /tmp/vers.out
  mv /tmp/vers.out $File
  MINOR=0
fi

if [ "$MINOR" != "" ]; then
  if [ "$MINOR" = "+" ]; then
    MINOR=`awk '/^#define MINOR_NUM/	{printf ("%d", substr($3, 2, length($3)-2)+1)}' $File`
  fi
  sed "s/#define MINOR_NUM \".*\"/#define MINOR_NUM \"$MINOR\"/" $File > /tmp/vers.out
  mv /tmp/vers.out $File
  CHANGE=0
  COMPILATION=0
fi

if [ "$CHANGE" != "" ]; then
  if [ "$CHANGE" = "+" ]; then
    CHANGE=`awk '/^#define CHANGE_NUM/	{printf ("%d", substr($3, 2, length($3)-2)+1)}' $File`
  fi
  sed "s/#define CHANGE_NUM \".*\"/#define CHANGE_NUM \"$CHANGE\"/" $File > /tmp/vers.out
  mv /tmp/vers.out $File
fi

if [ "$COMPILATION" != "" ]; then
  if [ "$COMPILATION" = "+" ]; then
    COMPILATION=`awk '/^#define COMPILATION_NUM/	{printf ("%d", substr($3, 2, length($3)-2)+1)}' $File`
  fi
  sed "s/#define COMPILATION_NUM \".*\"/#define COMPILATION_NUM \"$COMPILATION\"/" $File > /tmp/vers.out
  mv /tmp/vers.out $File
fi
