#!/bin/sh
#
# $Source: /net/dworshak2/users/student/evans911/prog/cs481/RCS/build,v $
# $Author: evans911 $
# Current revision: $Revision: 0.4 $
# Last modified: $Date: 1996/12/12 02:11:52 $
# State of current revision: $State: Exp $
# Currently locked by: $Locker:  $
#
# Description: Bootstrap script for building the cca script.
#              
# $Log: build,v $
# Revision 0.4  1996/12/12 02:11:52  evans911
# It turns out that print isn't compatible with bash.  Fixed.
#
# Revision 0.3  1996/12/11 03:21:38  evans911
# Fixed stupid path problems.
#
# Revision 0.2  1996/12/11 03:15:54  evans911
# Hopefully catching errors in "merge" now.
#
# Revision 0.1  1996/12/11 03:00:43  evans911
# Initial revision
#
#

if [ "$1" = "" ] ; then
  perlexec=perl;
elif [ "$1" = "-h" ] ; then
  echo "Usage: build [ <perl_name> | -h ]"
  exit;
else
  perlexec=$1;
fi

goodness=0 # 0 means not found, 1 means 5.001 found, 2 means 5.002+ found

for i in $PERL_EXE_PATH `echo $PATH |tr -s ":" "\n"` 
do
  if [ -x $i/$perlexec ] ; then
    version=`$i/$perlexec -v |grep "This is perl, version" |awk '{print $5}'`
    major=`echo $version | cut -d "." -f 1`
    minor=`echo $version | cut -d "." -f 2`
    if [ $major -ge 5 ] ; then
      if [ $minor -ge 2 ] ; then
	echo "$i/$perlexec is $version.  Good."
	use=$i/$perlexec
	goodness=2
	break;
      elif [ $minor -eq 1 ] ; then
	echo "$i/$perlexec is version $version.  Hoping for something newer..."
	use=$i/$perlexec
	goodness=1
      else
	echo "$i/$perlexec is version $version.  Need at least 5.001.";
      fi
    else
      echo "$i/$perlexec (version $version) is too old.";
    fi
  fi
done

echo ""

goodness=2
use="/usr/local/bin/perl"

if [ $goodness -eq 2 ] ; then
  echo "";
elif [ $goodness -eq 1 ] ; then
  echo "Warning: Perl 5.001 has known problems with large strings, so you may"
  echo "not be able to successfully complete runs on large input files."
  echo "";
else 
  echo "Couldn't find a useable version of Perl.  You can specify a directory"
  echo "to look in by defining the shell variable PERL_EXE_PATH.  You can"
  echo "also specify a different filename by running this script as"
  echo "\"build <filename>\"."
  exit;
fi

echo "About to run merge..."
echo "#!$use" > ./merge
#echo "$use" >> ./merge
cat src/pmerge.pl >> ./merge
chmod u+x ./merge

if (cd src ; ../merge $use ../bin/cca) ; then
  echo "Done!";
else
  echo "Error: merge had a problem."
  exit;
fi

