#!/bin/sh 
# Verification de l usage 

usage="usage: config name binary configurator [llcore [sizes]]"

case $# in
   0|1|2) echo $usage
          exit 1;;
   *) cnom=$1; bin=$2; conf=$3; shift; shift; shift;;
esac

# le nom doit etre simple et sans path
nom=`echo $cnom | sed -e 's-.*/\(.*\)$-\1-'`

# Le fichier de configuration est-il un path absolu
#  ou bien un path relatif
case $conf in
	/?*) ;; 
	*)  conf=conf/$conf ;;
esac

# Le fichier de configuration est-il la
if test ! -f $conf
then
   echo "config: file $conf does'nt exist"
   exit 1
fi

DIR=/home/margaux/formel2/caml/V3.1/camlisp
SYSDIR=$DIR/decstation

# le repertoire systeme de la machine qu on utilise
if test ! -d $SYSDIR
then
  echo "config: directory $SYSDIR does'nt exist"
  echo $usage
  exit 1
fi

# le repertoire des cores
case "$1" in
	"") llcore=$SYSDIR/llcore ;;
        -*) llcore=$SYSDIR/llcore ;;
	*)  llcore=$1; shift ;;
esac

# le repertoire qu on utilise pour les cores
touch $llcore/test
if test ! -f $llcore/test
then
  echo "config: can't write in directory $llcore"
  exit 1
fi
rm $llcore/test

# Le fichier binaire est-il un path absolu
#  ou bien un path relatif
case $bin in
	/?*) ;; 
	*)  bin=$SYSDIR/$bin ;;
esac

# Le fichier binaire est-il la
if test ! -f $bin
then
   echo "config: binary file $bin does'nt exist"
   exit 1
fi

# Creation du script shell de commande
llcmd=/tmp/ll$$
trap "rm -f $llcmd;exit 1" 1 2 3 15

echo exec $bin $* -r $llcore/$nom.core \$\* > $llcmd

chmod +x $llcmd
 
#  Creation de l image memoire
#  Pour ne pas charger son .lelisp au moment de creer le core
HOME=qwerty

$llcmd $DIR/llib/startup.ll <<EOF
(setq #:system:name (quote |$nom|))
`cat $conf`
(end)
EOF

cp $llcmd $cnom
rm $llcmd
