#!/bin/zsh

################################################################################
# Created by Ryan Slater | Solutions Engineering | Kandji, Inc.
################################################################################
# Created on 2024/05/02
# 
#
################################################################################
# Tested macOS Versions
################################################################################
#
#   - macOS Sonoma 14.4 & 14.4.1
#
################################################################################
# Software Information
################################################################################
#
# A simple Audit script to evaluate the optimal conditions to install LucidLink
# v2 as a Custom App in Kandji. It requires a Kernel Extension in order to
# function so it'll check for the ID of the Kernel Extension Profile you define
# before calling for the installer to proceed
#
################################################################################
# License Information
################################################################################
# Copyright 2024 Kandji, Inc.
#
# 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 #####################################
##############################################################################

# Enter the ID of your Kernel Extension Library Item
kernel_ext_id="07ee9b91-13d1-428b-acae-9b03cf984622"

##############################################################################
############################# VARIABLES ######################################
##############################################################################
app_path="/Applications/Lucid.app"
kernel_ext_installed=$(/usr/bin/profiles list | grep $kernel_ext_id)

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

if [[ ! -e $app_path ]] && [[ ! $kernel_ext_installed == "" ]]; then
    echo "LucidLink App could not be found & Kernel Extension Profile exists. Installing"
    exit 1
elif [[ -e $app_path ]] && [[ $kernel_ext_installed == "" ]]; then
    echo "LucidLink App installed, but the Kernel Extension profile is not present. Will not proceed."
    exit 0
elif [[ -e $app_path ]] && [[ ! $kernel_ext_installed == "" ]]; then
    echo "LucidLink App installed, Kernel Extension profile is found. Nothing to do"
    exit 0
elif [[ ! -e $app_path ]] && [[ $kernel_ext_installed == "" ]]; then
    echo "App not found, kernel extension not found. Please install Kernel Extension Profile before attempting to install LucidLink"
    exit 0
  else
  echo "Something else has gone wrong"
  exit 0
fi