#!/bin/sh

if [ `id -u` -ne 0 ]
then
  echo "You must run this script as root."
  exit 1
fi

umask 022

FLEX_DIR=/usr/local/foundry/FLEXlm

#NOTE : The Foundry Server Utility uses this element to locate lmutil, 
#if you alter this ensure it still points to the FFT tools dir 
TOOL_DIR=/usr/local/foundry/FLEXlmTools5.0

LIC_DIR=${FLEX_DIR}
BIN_DIR=${TOOL_DIR}/bin
DOC_DIR=${TOOL_DIR}/docs
LOG_DIR=${FLEX_DIR}/log
INIT_DIR=/etc/init.d

#Surely we should stop it if we are about to replace it?
if [ -f ${INIT_DIR}/foundryflexlmserver ]
then
  echo "There appears to be an installation of The Foundry FLEXlm Tools"
  echo "on this machine. Please uninstall it before continuing by"
  echo "running /usr/local/foundry/FLEXlmTools5.0/bin/uninstall.sh"
  exit 1
fi

#Check for the old server
if [ -f ${INIT_DIR}/foundrylmgrd ]
then
  echo "There appears to be an installation of The Foundry FLEXlm Tools"
  echo "on this machine. Please uninstall it before continuing by executing"
  echo "the following commands :"
  echo "/sbin/chkconfig foundrylmgrd off"
  echo "/etc/init.d/foundrylmgrd stop"
  echo "rm -f /etc/rc2.d/S98foundrylmgrd"
  echo "rm -f /etc/init.d/foundrylmgrd"
  exit 1
fi

echo "Installing The Foundry FLEXlm Tools 5.0"

if [ ! -d ${BIN_DIR} ]
then
  mkdir -p ${BIN_DIR}
fi

if [ ! -d ${DOC_DIR} ]
then
  mkdir -p ${DOC_DIR}
fi

if [ ! -d ${LIC_DIR} ]
then
  mkdir -p ${LIC_DIR}
fi

if [ ! -d ${LOG_DIR} ]
then
  mkdir -p ${LOG_DIR}
fi

cp ./FoundryServerUtility ./FoundryLicenseInstaller ${TOOL_DIR}
chown root:root ${TOOL_DIR}/*
chmod 755 ${TOOL_DIR}/*

# TODO: foundry.opt ?
cp ./lmgrd.foundry ./lmutil ./foundry ./uninstall.sh ${BIN_DIR}
chown root:root ${BIN_DIR}/*
chmod 755 ${BIN_DIR}/*

cp ./foundry.log ${LOG_DIR}
chown root:root ${LOG_DIR}/*
chmod -R 777 ${LOG_DIR}

cp ./docs/*.pdf ${DOC_DIR}
chown root:root ${DOC_DIR}/*
chmod 666 ${DOC_DIR}/*

# TODO: Dummy License?
#cp ./foundry.lic ${LIC_DIR}
#chown root:root ${LIC_DIR}/foundry.lic
chmod -R 777 ${LIC_DIR}

cp ./foundryflexlmserver ${INIT_DIR}
chown root:root ${INIT_DIR}/foundryflexlmserver
chmod 755 ${INIT_DIR}/foundryflexlmserver

echo "Setting foundrylmgrd to run at startup"
/sbin/chkconfig -f foundryflexlmserver on
ln -s ${INIT_DIR}/foundryflexlmserver /etc/rc0.d/K05foundryflexlmserver 
ln -s ${INIT_DIR}/foundryflexlmserver /etc/rc2.d/S98foundryflexlmserver 

echo "Done."


