#!/bin/sh

# Ensure XPAUTH_PATH is set
if [ _$XPAUTH_PATH = _ ]; then
  # Use XPRESS environment variable if XPAUTH_PATH unset
  if [ _$XPRESS = _ ]; then
    echo "Please set the XPAUTH_PATH environment variable to the location of your license file"
    exit
  else
    XPAUTH_PATH=$XPRESS
  fi
fi

# If XPAUTH_PATH points to a folder, find the license file within it
if [ -d "$XPAUTH_PATH" ]; then
  if [ -f "$XPAUTH_PATH/xpauth.ini" ]; then
    XPAUTH_PATH=$XPAUTH_PATH/xpauth.ini
  fi
  if [ -f "$XPAUTH_PATH/xpauth.xpr" ]; then
    XPAUTH_PATH=$XPAUTH_PATH/xpauth.xpr
  fi
fi

if [ -r "$XPAUTH_PATH"  ]; then
  until [ _$a = _0 ]; do
    echo "Please select the action to perform and press Enter:"
    echo "    1 - start the license manager"
    echo "    2 - stop the license manager"
    echo "    0 - quit this program"
    read a

    if [ _$a = _1 ]; then
      xpserver -xpress "$XPAUTH_PATH" -logfile /var/tmp/xprl_server.log -d
      if [ $? -eq 0 ]; then
        echo "License manager started"
      fi
      break
    elif [ _$a = _2 ]; then
      xpserver -xpress "$XPAUTH_PATH" -logfile /var/tmp/xprl_server.log -service stop
      break
    fi
  done

else
  echo "Cannot find the license file, xpauth.xpr. Please obtain one from"
  echo "FICO and put it in the directory pointed to by the XPAUTH_PATH environment"
  echo "variable: $XPAUTH_PATH"
fi
