#!/bin/zsh

################################################################################
# Created by LucidLink Support | Technical Support | LucidLink
################################################################################
#
# Created on 2024/07/01
#
################################################################################
# Tested macOS Versions
################################################################################
#
#
################################################################################
# Software Information
################################################################################
#
# To create the necessary .lucid-keys directory as needed, move the extracted 
# .fskey and set all permissions correctly.
#
################################################################################
# License Information
################################################################################
# Copyright 2024 LucidLink
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is furnished
# to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
################################################################################

# Script Version
VERSION="1.0.0"

##############################################################################
############################# USER INPUT #####################################
##############################################################################
# Variables that should be set by the admin using the script. Typically
# these will be unique to a specific customer.

##############################################################################
############################# VARIABLES ######################################
##############################################################################

# Get the currently logged in username and form path for dir
loggedInUser=$(/usr/sbin/scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ {print $3}')
lucidKeysDir="/Users/${loggedInUser}/.lucid-keys"
lucidGroup="staff"

##############################################################################
################## FUNCTIONS - DO NOT MODIFY BELOW ###########################
##############################################################################

# Set logging - Send logs to stdout as well as Unified Log
# Usage: logging "LEVEL" "Message..."
# Use 'log show --process "logger"'to view logs activity.
logging(){
  script_id="LucidLink-SSO-Keys"
  timestamp=$(/bin/date +"%m-%d-%Y %H:%M:%S")
  
  echo "${timestamp} ${1}: ${2}"
  /usr/bin/logger "${script_id}: [${1}] ${2}"
}

##############################################################################
################### MAIN LOGIC - DO NOT MODIFY BELOW #########################
##############################################################################
# Do not modify the below. Modify at your own risk.

logging "INFO" "Executing script v${VERSION}..."
logging "INFO" "Logged in User is ${loggedInUser}"

#Create .lucid-keys dir if needed
logging "INFO" "Creating .lucid-keys directory"
if [[ ! -d ${lucidKeysDir} ]]; then
    mkdir ${lucidKeysDir}
fi

#Move fskey to ~/.lucid-keys
mv /private/tmp/.lucid-keys-tmp/*.fskey "${lucidKeysDir}/"

#Set dir and fskey permissions
chown ${loggedInUser}:${lucidGroup} $lucidKeysDir
chmod 755 ${lucidKeysDir}
chmod 644 ${lucidKeysDir}/*.fskey

exit 0