#!/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
###############################################################################
PY_DOT_VERSION=3.9.12
INSTALL_DIR="/opt/salt"
BIN_DIR="$INSTALL_DIR/bin"
CONFIG_DIR="/etc/salt"
TEMP_DIR="/tmp"
SBIN_DIR="/usr/local/sbin"

###############################################################################
# Define Functions
###############################################################################
log () {
    if [ -f "$TEMP_DIR/postinstall.txt" ]; then
        echo "$1" >> "$TEMP_DIR/postinstall.txt"
    else
        echo "$1" > "$TEMP_DIR/postinstall.txt"
    fi
}

quit_on_error() {
    log "$(basename "$0") caught error: $1 on line : $2 command was: $3"
    exit 1
}

###############################################################################
# Set up logging and error handling
###############################################################################
log "Post install script started on: $(date '+%Y/%m/%d %H:%M:%S')"
trap 'quit_on_error $? $LINENO $BASH_COMMAND' ERR

###############################################################################
# Check for existing minion config, copy if it doesn't exist
###############################################################################
if [ ! -f "$CONFIG_DIR/minion" ]; then
    log "Config: Copy Started..."
    cp "$CONFIG_DIR/minion.dist" "$CONFIG_DIR/minion"
    log "Config: Copied Successfully"
fi

###############################################################################
# Create symlinks to pyenv directories
###############################################################################
log "Symlink: Creating symlinks to pyenv directories..."
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"

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

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

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

log "Symlink: Created Successfully"

###############################################################################
# Create symlink to salt-config.sh
###############################################################################
if [ ! -d "$SBIN_DIR" ]; then
    log "Symlink: Creating $SBIN_DIR..."
    mkdir "$SBIN_DIR"
    log "Symlink: Created Successfully"
fi
# This is a special tool to make it easier for the user to get started setting
# up salt
log "Symlink: Creating symlink for salt-config..."
ln -sf "$BIN_DIR/salt-config.sh" "$SBIN_DIR/salt-config"
log "Symlink: Created Successfully"

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

###############################################################################
# Register Salt as a service
###############################################################################
log "Service: Configuring..."

log "Service: Enabling salt-minion..."
launchctl enable system/com.saltstack.salt.minion
log "Service: Enabled Successfully"

log "Service: Bootstrapping salt-minion..."
launchctl bootstrap system /Library/LaunchDaemons/com.saltstack.salt.minion.plist
log "Service: Bootstrapped Successfully"

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

log "Service: Started Successfully"

log "Service: Disabling Master, Syndic, and API services"
launchctl disable system/com.saltstack.salt.master
launchctl disable system/com.saltstack.salt.syndic
launchctl disable system/com.saltstack.salt.api
log "Service: Disabled Successfully"

log "Service: Configured Successfully"

log "Post install completed successfully on: $(date '+%Y/%m/%d %H:%M:%S')"

exit 0
