#!/bin/bash
###############################################################################
#
# Title: Post Script for Salt Installation
# Authors: Shane Lee
# Date: December 2015
#
# Description: This script copies the minion config file and starts the salt
#              service
#
# Requirements:
#    - None
#
# Usage:
#     This script is run as a part of the macOS Salt Installation
#
###############################################################################

###############################################################################
# Define Variables
###############################################################################
# Get Minor Version
OSX_VERSION=$(sw_vers | grep ProductVersion | cut -f 2 -d: | tr -d '[:space:]')
MINOR=$(echo ${OSX_VERSION} | cut -f 2 -d.)
PY_DOT_VERSION=3.9.12
# Path Variables
INSTALL_DIR="/opt/salt"
BIN_DIR="$INSTALL_DIR/bin"
CONFIG_DIR="/etc/salt"
TEMP_DIR="/tmp"
SBIN_DIR="/usr/local/sbin"
PY_DOT_VERSION="3.7.12"

###############################################################################
# Set up logging and error handling
###############################################################################
echo "Post install script started on:" > "$TEMP_DIR/postinstall.txt"
date "+%Y/%m/%d %H:%m:%S" >> "$TEMP_DIR/postinstall.txt"
trap 'quit_on_error $LINENO $BASH_COMMAND' ERR

quit_on_error() {
    echo "$(basename $0) caught error on line : $1 command was: $2" >> "$TEMP_DIR/postinstall.txt"
    exit 1
}

###############################################################################
# Check for existing minion config, copy if it doesn't exist
###############################################################################
if [ ! -f "$CONFIG_DIR/minion" ]; then
    echo "Config: Copy Started..." >> "$TEMP_DIR/postinstall.txt"
    cp "$CONFIG_DIR/minion.dist" "$CONFIG_DIR/minion"
    echo "Config: Copied Successfully" >> "$TEMP_DIR/postinstall.txt"
fi
###############################################################################
# Create symlinks to pyenv directories
###############################################################################
echo "Symlink: Creating symlinks to pyenv directories..." >> "$TEMP_DIR/postinstall.txt"
ln -s "$INSTALL_DIR/.pyenv/versions/$PY_DOT_VERSION/bin" "$INSTALL_DIR/bin"
ln -s "$INSTALL_DIR/.pyenv/versions/$PY_DOT_VERSION/include" "$INSTALL_DIR/include"
ln -s "$INSTALL_DIR/.pyenv/versions/$PY_DOT_VERSION/lib" "$INSTALL_DIR/lib"

echo "Symlink: Creating symlinks to library files..." >> "$TEMP_DIR/postinstall.txt"
find "$INSTALL_DIR/.pyenv/versions/$PY_DOT_VERSION" \
    -name '*.dylib' \
    -type f \
    -exec ln -s {} $INSTALL_DIR/lib/ \;

echo "Symlink: Copying symlinks for openssl..." >> "$TEMP_DIR/postinstall.txt"
find "$INSTALL_DIR/.pyenv/versions/$PY_DOT_VERSION/openssl" \
    -name '*.dylib' \
    -type l \
    -exec cp -R {} $INSTALL_DIR/lib/ \;

echo "Symlink: Copying symlinks for readline..." >> "$TEMP_DIR/postinstall.txt"
find "$INSTALL_DIR/.pyenv/versions/$PY_DOT_VERSION/readline" \
    -name '*.dylib' \
    -type l \
    -exec cp -R {} $INSTALL_DIR/lib/ \;

echo "Symlink: Created Successfully" >> "$TEMP_DIR/postinstall.txt"

###############################################################################
# Create symlink to salt-config.sh
###############################################################################
if [ ! -d "$SBIN_DIR" ]; then
    echo "Symlink: Creating $SBIN_DIR..." >> "$TEMP_DIR/postinstall.txt"
    mkdir "$SBIN_DIR"
    echo "Symlink: Created Successfully" >> "$TEMP_DIR/postinstall.txt"
fi
echo "Symlink: Creating symlink for salt-config..." >> "$TEMP_DIR/postinstall.txt"
ln -sf "$BIN_DIR/salt-config.sh" "$SBIN_DIR/salt-config"
echo "Symlink: Created Successfully" >> "$TEMP_DIR/postinstall.txt"

###############################################################################
# Add salt to paths.d
###############################################################################
if [ ! -d "/etc/paths.d" ]; then
    echo "Path: Creating paths.d directory..." >> "$TEMP_DIR/postinstall.txt"
    mkdir /etc/paths.d
    echo "Path: Created Successfully" >> "$TEMP_DIR/postinstall.txt"
fi
echo "Path: Adding salt to the path..." >> "$TEMP_DIR/postinstall.txt"
sh -c "echo \"$BIN_DIR\" > /etc/paths.d/salt"
sh -c "echo \"$SBIN_DIR\" >> /etc/paths.d/salt"
echo "Path: Added Successfully" >> "$TEMP_DIR/postinstall.txt"

###############################################################################
# Register Salt as a service
###############################################################################
echo "Service: Configuring..." >> "$TEMP_DIR/postinstall.txt"

echo "Service: Enabling salt-minion..." >> "$TEMP_DIR/postinstall.txt"
launchctl enable system/com.saltstack.salt.minion
echo "Service: Enabled Successfully" >> "$TEMP_DIR/postinstall.txt"

echo "Service: Bootstrapping salt-minion..." >> "$TEMP_DIR/postinstall.txt"
launchctl bootstrap system /Library/LaunchDaemons/com.saltstack.salt.minion.plist
echo "Service: Bootstrapped Successfully" >> "$TEMP_DIR/postinstall.txt"

if /bin/launchctl list "com.saltstack.salt.minion" &> /dev/null; then
    echo "Service: Service Running" >> "$TEMP_DIR/postinstall.txt"
else
    echo "Service: Kickstarting Service..." >> "$TEMP_DIR/postinstall.txt"
    launchctl kickstart -kp system/com.saltstack.salt.minion
    echo "Service: Kickstarted Successfully" >> "$TEMP_DIR/postinstall.txt"
fi

echo "Service: Started Successfully" >> "$TEMP_DIR/postinstall.txt"

echo "Service: Disabling Master, Syndic, and API services" >> "$TEMP_DIR/postinstall.txt"
launchctl disable system/com.saltstack.salt.master
launchctl disable system/com.saltstack.salt.syndic
launchctl disable system/com.saltstack.salt.api
echo "Service: Disabled Successfully" >> "$TEMP_DIR/postinstall.txt"

echo "Service: Configured Successfully" >> "$TEMP_DIR/postinstall.txt"

echo "Post install completed successfully on:" >> "$TEMP_DIR/postinstall.txt"
date "+%Y/%m/%d %H:%m:%S" >> "$TEMP_DIR/postinstall.txt"

exit 0
