#!/bin/sh
#
# configure
# $Id: configure,v 2.1 1994/10/16 18:21:46 remy Exp $
#
# remy@ccs.neu.edu
# 27 June 1994
#
# Copyright (C) 1994 by Remy Evard
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
#
# A copy of the license may be found in docs/license of the source
# distribution.
#
#
# configure [perl] [orig-defs]
#  - build extract with correct perl name
#  - build builddefs with correct perl name
#  - if orig-defs is given, build 'defs' with builddefs
#  - if orig-defs is not given and defs doesn't exist, copy it
#    in from defs.tmpl

PERL=""
OLDDEFS=""
TMPL=defs.tmpl
USAGE="usage: configure [-p path-to-perl] [orig-defs]"

if [ "$1" = "-p" ]; then
  if [  "$#" -lt 2 ]; then
    echo $USAGE 1>&2
    exit 1
  fi
  PERL=$2
  shift; shift
fi

if [ "$#" -eq 1 ]; then
  if [ -f "$1" ]; then
     OLDDEFS="$1"
  else
     echo "Can't find $1" 2>&1
     echo $USAGE 1>&2
     exit 1
  fi
elif [ $# -gt 1 ]; then
  echo $USAGE 1>&2
  exit 1
fi

if [ "$PERL" = "" ]; then
  if [ -f "$OLDDEFS" ]; then
    PERL=`grep "^PERL_PATH" "$OLDDEFS"`
    PERL=`echo "$PERL" | sed 's/[^\/]*\(\/.*\)/\1/'`
  elif [ -f defs ]; then
    PERL=`grep "^PERL_PATH" defs`
    PERL=`echo "$PERL" | sed 's/[^\/]*\(\/.*\)/\1/'`
  else
   SAVEIFS="$IFS"
   IFS=:
   for P in $PATH ; do
     if [ -f "$P/perl" ]; then
       PERL="$P/perl"
       break
     fi
   done
   IFS="$SAVEIFS"
  fi
fi

if [ "$PERL" = "" ]; then
  echo "Can't find perl anywhere!  Please specify it on the command line." 1>&2
  echo $USAGE 1>&2
  exit 1
else
  echo "Using #!$PERL for perl scripts"
fi

# Now make the extract script.
echo "Building extract."
echo "#!$PERL" > extract
cat utils/extract >> extract
chmod +x extract

# Now make the builddefs script.
echo "Building builddefs."
echo "#!$PERL" > builddefs
cat utils/builddefs >> builddefs
chmod +x builddefs

# Now see if we should make a defs file.
if [ -f "$OLDDEFS" ]; then
  if [ -f defs ]; then
    echo "moving defs to defs.old"
    /bin/mv defs defs.old
  fi
  echo "building a new defs file based on $TMPL and $OLDDEFS."
  ./builddefs $TMPL $OLDDEFS > defs
elif [ ! -f defs ]; then
  echo "copying in $TMPL as the new defs file"
  /bin/cp $TMPL defs
else
  echo "Not building a new defs file... one exists."
  echo "If you need a new defs, specify one on the command line."
  echo $USAGE
fi
