#!/bin/bash
#
# RLM Service Installer
#

clear

echo "======== Installing the License Server on macOS ========"

echo "Starting the RLM license server"

sudo tar -xf ./rlm.tar -C /Users/Shared/

echo "Successful starting the RLM license server."

echo -e ""


echo "Setup the license server permanently..."

echo -e ""

echo "Copy the RLM license server daemon ..."

sudo cp ./com.RLMLicenseServer.plist /Library/LaunchDaemons/


echo "Load the RLM license server daemon..."

sudo chown root /Library/LaunchDaemons/com.RLMLicenseServer.plist

sudo launchctl load -w /Library/LaunchDaemons/com.RLMLicenseServer.plist

echo -e ""

sudo chmod -R 755 /Users/Shared/rlm


#foundry License File

FOUNDRY_LIC_PATH="/Library/Application\ Support/TheFoundry/RLM/"

if [ -e "$FOUNDRY_LIC_PATH" ];

then

	cd /Users/Shared/rlm	

	sudo cp ./foundry.lic /Library/Application\ Support/TheFoundry/RLM/

	sudo cp ./foundry.set /Library/Application\ Support/TheFoundry/RLM/

	sudo chmod -R 777 /Library/Application\ Support/TheFoundry/RLM/

else

	cd /Users/Shared/rlm 
	
	sudo mkdir -p /Library/Application\ Support/TheFoundry/RLM/

	sudo mkdir -p /Library/Application\ Support/TheFoundry/RLM/log

	sudo cp ./foundry.lic /Library/Application\ Support/TheFoundry/RLM/
	
	sudo cp ./foundry.set /Library/Application\ Support/TheFoundry/RLM/

	sudo chmod -R 777 /Library/Application\ Support/TheFoundry/RLM/

fi

#Genarts License File

GENARTS_LIC_PATH="/Library/Application\ Support/GenArts/rlm/"

if [ -e "$GENARTS_LIC_PATH" ];

then

	sudo rm /Library/Application\ Support/GenArts/rlm/*

	cd /Users/Shared/rlm 

	sudo cp ./genarts.lic /Library/Application\ Support/GenArts/rlm/

	sudo cp ./genarts.set /Library/Application\ Support/GenArts/rlm/

	sudo cp ./genarts.lic /Library/Application\ Support/GenArts/rlm/

	sudo chmod -R 777 /Library/Application\ Support/GenArts/rlm/

else

	cd /Users/Shared/rlm
	 
	sudo mkdir -p /Library/Application\ Support/GenArts/rlm

	sudo cp ./genarts.lic /Library/Application\ Support/GenArts/rlm/

	sudo cp ./genarts.set /Library/Application\ Support/GenArts/rlm/

	sudo cp ./genarts.lic /Library/Application\ Support/GenArts/rlm/

	sudo chmod -R 777 /Library/Application\ Support/GenArts/rlm/

fi

#PeregrineLabs Lic

PEREGRINELABS_LIC_PATH="/Library/Application\ Support/PeregrineLabs/rlm/"

if [ -e "$PEREGRINELABS_LIC_PATH" ];

then

	cd /Users/Shared/rlm 
	
	sudo cp ./peregrinel.lic /Library/Application\ Support/PeregrineLabs/rlm/
	
	sudo cp ./peregrinel.set /Library/Application\ Support/PeregrineLabs/rlm/
	
	sudo chmod -R 777 /Library/Application\ Support/PeregrineLabs/rlm/

else

	cd /Users/Shared/rlm 
	
	sudo mkdir -p /Library/Application\ Support/PeregrineLabs/rlm/
	
	sudo cp ./peregrinel.lic /Library/Application\ Support/PeregrineLabs/rlm/
	
	sudo cp ./peregrinel.set /Library/Application\ Support/PeregrineLabs/rlm/
	
	sudo chmod -R 777 /Library/Application\ Support/PeregrineLabs/rlm/

fi


#Maxwell License File

MAXWELL_LIC_PATH="/Users/Shared/NextLimit/rlm_nl/licenses"

if [ -e "$PEREGRINELABS_LIC_PATH" ];

then

	cd /Users/Shared/rlm 
	
	sudo cp ./nextlimit.set /Users/Shared/NextLimit/rlm_nl/licenses
	
	sudo cp ./nextlimit.lic /Users/Shared/NextLimit/rlm_nl/licenses
	
	sudo chmod -R 777 /Users/Shared/NextLimit/rlm_nl/licenses

else

	cd /Users/Shared/rlm 
	
	sudo mkdir -p /Users/Shared/NextLimit/rlm_nl/licenses
	
	sudo cp ./nextlimit.set /Users/Shared/NextLimit/rlm_nl/licenses
	
	sudo cp ./nextlimit.lic /Users/Shared/NextLimit/rlm_nl/licenses
	
	sudo chmod -R 777 /Users/Shared/NextLimit/rlm_nl/licenses

fi


echo "Starting the RLM License Manager..."
sudo ./rlm -dlog +"solidangle.dlog" &
echo -e ""
echo "The RLM License Server Successfully Installed."
cd $HOME
