#!/bin/bash

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."
  echo "First stop the server by running :"
  echo "/etc/init.d/foundrylmgrd stop"
  echo "Then turn off the servers automatic start up:"
  if [ -f /etc/redhat-release ]
  then
    echo "/sbin/chkconfig --del foundrylmgrd"
  elif [ -f /etc/debian_version ]
  then
    echo "/usr/sbin/update-rc.d -f foundrylmgrd remove"
  elif [ -f /etc/SuSE-release ]
  then
    echo "/sbin/chkconfig --del foundrylmgrd"
  else
    echo "** I don't know how to stop foundrylmgrd running at startup on this version of Linux. **"
    echo "** Please consult your OS docs. **"
  fi
  echo "Then remove the server:"
  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}

# 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

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

# Should probably use lsb_release here but older dists may not have it
if [ -f /etc/redhat-release ]
  then
    echo "Setting foundrylmgrd to run at startup"
    /sbin/chkconfig --level 345 foundryflexlmserver on
elif [ -f /etc/debian_version ]
  then
    echo "Setting foundrylmgrd to run at startup"
    /usr/sbin/update-rc.d foundryflexlmserver defaults 
elif [ -f /etc/SuSE-release ]
  then
    echo "Setting foundrylmgrd to run at startup"
    /sbin/chkconfig --level 345 foundryflexlmserver on
else
  echo "I don't know how to set foundrylmgrd to run at startup.  Please
  consult your OS documentation."
fi

echo "Done."


