#!/bin/bash

    << CHANGELOG
    2019-01-20:
        > Reformat spacing
        > Uploaded copy to Repo "Public-Scripts"
    2021-03-10: Template Created & Added to GIT
    2021-04-27: Adapted legacy script to Template format
    2021-04-28: Add support for status flag; Also now runs License Server service after install
CHANGELOG

#################################
## >> USER VARIABLES
#################################
ScriptFolder=~/Scripts
ScriptName=Autostart_IntelliJServer
AppName=IntelliJIDEALicenseServer_linux_amd64
LogFile=${ScriptFolder}/Timeline_IntelliJ.log
LogComment1='IntelliJServer - Start'
LogComment2='IntelliJServer - Running'
LogComment3='IntelliJServer - Stopped'
SourceBinaries=/mnt/Public/debian-svc-01/IntelliJIDEALicenseServer_linux_amd64

#################################
## /END OF USER MODIFIABLE SETTINGS
#################################


if [[ "$EUID" -ne 0 ]]; then
	echo "This installer needs to be run with superuser privileges."
	exit
fi




echo -e "
$(tput setaf 3; tput setab 0)
    >> # [IntelliJ_Deploy-01] - Basic Dependencies - Create Folder
$(tput sgr 0)
"

mkdir -p ${ScriptFolder} && \
echo "Folder: ${ScriptFolder}"




echo -e "
$(tput setaf 3; tput setab 0)
    >> # [IntelliJ_Deploy-02] - Create Init.d Script
$(tput sgr 0)
"


{
cat << 'EOF' > ${ScriptFolder}/${ScriptName}.sh
#! /bin/bash

### BEGIN INIT INFO
# Provides:          REPLACE_ME_NAME
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts the IntelliJIDEALicenseServer on boot-up
# Description:       Starts the IntelliJIDEALicenseServer on boot-up (port 1017)
### END INIT INFO

# >> This should be located in /etc/init.d/<script name>

# >> Source: https://gist.github.com/drmalex07/298ab26c06ecf401f66c
# https://gist.github.com/naholyr/4275302


# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting IntelliJ server..."
    echo "A $(date) - REPLACE_ME_COMMENT1" >> REPLACE_ME_LOG_FILE
    sleep 10
    sudo -u root bash -c 'REPLACE_ME_FOLDER/REPLACE_ME_APP_NAME &'
    echo "B $(date) - REPLACE_ME_COMMENT2" >> REPLACE_ME_LOG_FILE
    ;;
  stop)
    echo "Trying to find PID of IntelliJ..."
    pid=$(netstat -plant | grep IntelliJ | awk -F '[ /]+' '{print $7}')
    if [ -z "$pid" ]; then  echo "Either IntelliJ not already running, or cannot get PID"; else kill -15 $pid; fi
    echo "Z $(date) - REPLACE_ME_COMMENT3" >> REPLACE_ME_LOG_FILE
    sleep 1
    ;;
  status)
    echo "Trying to find PID of IntelliJ..."
    pid=$(netstat -plant | grep IntelliJ | awk -F '[ /]+' '{print $7}')
    if [ -z "$pid" ]; 
    then 
        echo -e "$(tput setaf 0; tput setab 6)" && \
        echo "IntelliJ License Server INACTIVE" && \
        echo -e "$(tput sgr 0)"
    else
        echo -e "$(tput setaf 0; tput setab 2)" && \
        echo "IntelliJ License Server RUNNING. PID: ${pid}" && \
        echo -e "$(tput sgr 0)"
    fi
    ;;
  *)
    echo "Usage: /etc/init.d/REPLACE_ME_NAME {start|stop}"
    exit 1
    ;;
esac

exit 0
EOF
} && \
sed -i "s@REPLACE_ME_NAME@${ScriptName}@g" ${ScriptFolder}/${ScriptName}.sh && \
sed -i "s@REPLACE_ME_COMMENT1@${LogComment1}@g" ${ScriptFolder}/${ScriptName}.sh && \
sed -i "s@REPLACE_ME_COMMENT2@${LogComment2}@g" ${ScriptFolder}/${ScriptName}.sh && \
sed -i "s@REPLACE_ME_COMMENT3@${LogComment3}@g" ${ScriptFolder}/${ScriptName}.sh && \
sed -i "s@REPLACE_ME_LOG_FILE@${LogFile}@g" ${ScriptFolder}/${ScriptName}.sh && \
sed -i "s@REPLACE_ME_FOLDER@${ScriptFolder}@g" ${ScriptFolder}/${ScriptName}.sh && \
sed -i "s@REPLACE_ME_APP_NAME@${AppName}@g" ${ScriptFolder}/${ScriptName}.sh && \
chmod +x ${ScriptFolder}/${ScriptName}.sh && \
echo -e "$(tput setaf 7; tput setab 0)" && \
echo ">> Script Path: ${ScriptFolder}/${ScriptName}.sh" && \
echo && \
cat ${ScriptFolder}/${ScriptName}.sh && \
echo -e "$(tput sgr 0)"




echo -e "
$(tput setaf 3; tput setab 0)
    >> # [IntelliJ_Deploy-03] - Create Symlink
$(tput sgr 0)
"

    : '
        # Flipping this around so when I do "ls -la" I can remember where this goes...
        # ln -s ${ScriptFolder}/${ScriptName} /etc/init.d/${ScriptName}
    '

mv ${ScriptFolder}/${ScriptName}.sh /etc/init.d/${ScriptName} && \
ln -s /etc/init.d/${ScriptName} ${ScriptFolder}/${ScriptName}.sh && \
update-rc.d ${ScriptName} defaults && \
echo -e "$(tput setaf 7; tput setab 0)" && \
echo "NOTE: This command was intentionally flipped, so that 'ls -la' shows the path." && \
echo && \
ls -la ${ScriptFolder}/*.sh && \
echo -e "$(tput sgr 0)"




echo -e "
$(tput setaf 3; tput setab 0)
    >> # [IntelliJ_Deploy-04] - Copy in LicenseServer Binaries
$(tput sgr 0)
"

{
if [ -f "${SourceBinaries}" ]; then
    cp ${SourceBinaries} ${ScriptFolder}/${AppName} && \
    chmod +x ${ScriptFolder}/${AppName} && \
    echo -e "$(tput setaf 0; tput setab 2)" && \
    echo "Success! Binaries copied over." && \
    echo -e "$(tput sgr 0)"
else
    echo -e "$(tput setaf 3; tput setab 1)" && \
    echo -e "** ${AppName} will need to be copied to this server MANUALLY to the folder: **\n\t${ScriptFolder}" && \
    echo -e "$(tput sgr 0)"
fi
} && \
echo -e "$(tput setaf 7; tput setab 0)" && \
ls -la ${ScriptFolder}/${AppName}* && \
echo -e "$(tput sgr 0)"




echo -e "
$(tput setaf 3; tput setab 0)
    >> # [IntelliJ_Deploy-05] - Start License Server
$(tput sgr 0)
"

/etc/init.d/${ScriptName} start && \
echo -e "$(tput setaf 7; tput setab 0)" && \
/etc/init.d/${ScriptName} status && \
echo -e "$(tput sgr 0)"




echo -e "
$(tput setaf 3; tput setab 0)
    >> # [IntelliJ_Deploy-12] - Script Cleanup
$(tput sgr 0)
"

if ! command -v realpath &> /dev/null
then
    echo -e "COMMAND 'realpath' could not be found. $(tput setaf 3; tput setab 0)Please move script manually.$(tput sgr 0)"
else
    SCRIPT=`realpath -s $0` && \
    DESTINATION=~/Deploy && \
    mkdir -p ${DESTINATION} && \
    mv ${SCRIPT} ${DESTINATION} && \
    ls -laR ${DESTINATION}
fi
