#!/bin/sh
#
# Configure PAKCSHOME/pakcsinitrc with correct environment variables
# and define the appropriate symbolic links
# PAKCSHOME/bin/sicstusprolog  (if SICStus-Prolog is available)
# PAKCSHOME/bin/swiprolog      (if SWI-Prolog is available)
#

# Compute home of PAKCS installation:
PAKCSHOME=`(cd "\`dirname \"$0\"\`" > /dev/null ; pwd)`/..

# Distribution version of the pakcsinitrc file:
PAKCSINITRC=$PAKCSHOME/pakcsinitrc

# Initial repository version of the pakcsinitrc file:
REPOSINITRC=$PAKCSHOME/scripts/pakcsinitrc.sh

# Name of user-local pakcsinitrc file (if HOME is present):
if [ -n "$HOME" -a "$DISTPKGINSTALL" = "no" ] ; then
  HOMEINITRC=$HOME/.pakcsinitrc
else
  HOMEINITRC=
fi

# Generate installation-local pakcsinitrc file (if not present):
if [ ! -f "$PAKCSINITRC" -a -f "$REPOSINITRC" ] ; then
  # The following is only relevant for repository checkouts:
  cp "$REPOSINITRC" "$PAKCSINITRC"
elif [ ! -f "$PAKCSINITRC" ] ; then
  echo "ERROR: Incomplete distribution: $PAKCSINITRC is missing!"
  exit 1
fi

# if both SICSTUSDIR and SWIPROLOG are undefined, try to define them
# from installation-local or user-local pakcsinitrc file:
if [ -z "$SICSTUSDIR" -a -z "$SWIPROLOG" ] ; then
  echo "Try to use definitions from $PAKCSINITRC..."
  . "$PAKCSINITRC"
fi
if [ -z "$SICSTUSDIR" -a -z "$SWIPROLOG" -a -f "$HOMEINITRC" ] ; then
  echo "Try to use definitions from $HOMEINITRC..."
  . "$HOMEINITRC"
fi

# if both SICSTUSDIR and SWIPROLOG are still undefined, try find a system:
if [ -z "$SICSTUSDIR" -a -z "$SWIPROLOG" ] ; then
  # try to define SICSTUSDIR if it is not defined:
  echo "Try to find SICStus-Prolog..."
  SICSTUSBIN=`which sicstus 2> /dev/null`
  if [ -n "$SICSTUSBIN" ] ; then
    SICSTUSDIR=`expr $SICSTUSBIN : '\(.*\)/bin/sicstus'`
  fi
  if [ -x "$SICSTUSDIR/bin/sicstus" ] ; then
    echo "halt." | $SICSTUSDIR/bin/sicstus > /tmp/sicstusout$$ 2>&1
    if [ $? -ne 0 ]; then
      SICSTUSDIR=
    fi
    rm /tmp/sicstusout$$
  else
    SICSTUSDIR=
  fi
  # try to define SWIPROLOG if SICSTUSDIR is still undefined:
  if [ -z "$SICSTUSDIR" ] ; then
    echo "No SICStus-Prolog defined or found, looking for SWI-Prolog..."
    SWIPROLOG=`which swipl 2> /dev/null`
    if [ -x "$SWIPROLOG" ] ; then
      echo "halt." | $SWIPROLOG > /tmp/swiprologout$$ 2>&1
      if [ $? -ne 0 -o `grep -c SWI-Prolog /tmp/swiprologout$$` -eq 0 ]; then
        SWIPROLOG=
      fi
      rm /tmp/swiprologout$$
    fi
  fi
fi

# if both SICSTUSDIR and SWIPROLOG are still undefined, give up:
if [ -z "$SICSTUSDIR" -a -z "$SWIPROLOG" ] ; then
  echo "Please provide a correct definition for SICSTUSDIR or SWIPROLOG"
  echo "as a parameter for 'make'!"
  exit 1
fi

# store current definitions of SICSTUSDIR and SWIPROLOG in pakcsinitrc files:
mv "$PAKCSINITRC" "$PAKCSINITRC.bak"
cat "$PAKCSINITRC.bak" | sed 's|^SICSTUSDIR=.*$|SICSTUSDIR='$SICSTUSDIR'|' \
                       | sed 's|^SWIPROLOG=.*$|SWIPROLOG='$SWIPROLOG'|' \
                       > "$PAKCSINITRC"
rm "$PAKCSINITRC.bak"
if [ -n "$HOME" -a "$DISTPKGINSTALL" = "no" ] ; then
  cp "$PAKCSINITRC" "$HOMEINITRC"
fi

ORGMAKESTATE=scripts/pakcs-makesavedstate.sh
MAKESTATE=scripts/makesavedstate
# Create symbolic links in PAKCSHOME/bin and create scripts/makesavedstate:
rm -f bin/sicstusprolog bin/swiprolog $MAKESTATE # delete old definitions
if [ -n "$SICSTUSDIR" ] ; then
  ln -s "$SICSTUSDIR/bin/sicstus" bin/sicstusprolog
  # store the value of SICSTUSDIR in script scripts/makesavedstate :
  sed "s|^SICSTUSDIR=.*$|SICSTUSDIR=$SICSTUSDIR|" < $ORGMAKESTATE > $MAKESTATE
  chmod 755 $MAKESTATE
fi
if [ -n "$SWIPROLOG" ] ; then
  ln -s "$SWIPROLOG" bin/swiprolog
  cp $ORGMAKESTATE $MAKESTATE
  chmod 755 $MAKESTATE
fi

# Report current values:
echo '======================================================================'
if [ -f "$HOMEINITRC" ] ; then
  echo 'PAKCS installation configured with (saved in '$HOMEINITRC'):'
else
  echo 'PAKCS installation configured with:'
fi
echo "SICSTUSDIR=$SICSTUSDIR"
echo "SWIPROLOG=$SWIPROLOG"
echo '======================================================================'
