content: -- By: Pico Mitchell
  -- For: MacLand @ Free Geek
  --
  -- MIT License
  --
  -- Copyright (c) 2021 Free Geek
  --
  -- 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.
  --
  
  -- Version: 2022.2.22-1
  
  -- App Icon is “DVD” from Twemoji (https://twemoji.twitter.com/) by Twitter (https://twitter.com)
  -- Licensed under CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)
  
  -- Build Flag: LSUIElement
  
  use AppleScript version "2.7"
  use scripting additions
  
  repeat -- dialogs timeout when screen is asleep or locked (just in case)
  	set isAwake to true
  	try
  		set isAwake to ((do shell script ("bash -c " & (quoted form of "/usr/libexec/PlistBuddy -c 'Print :0:IOPowerManagement:CurrentPowerState' /dev/stdin <<< \"$(ioreg -arc IODisplayWrangler -k IOPowerManagement -d 1)\""))) is equal to "4")
  	end try
  	
  	set isUnlocked to true
  	try
  		set isUnlocked to ((do shell script ("bash -c " & (quoted form of "/usr/libexec/PlistBuddy -c 'Print :IOConsoleUsers:0:CGSSessionScreenIsLocked' /dev/stdin <<< \"$(ioreg -ac IORegistryEntry -k IOConsoleUsers -d 1)\""))) is not equal to "true")
  	end try
  	
  	if (isAwake and isUnlocked) then
  		exit repeat
  	else
  		delay 1
  	end if
  end repeat
  
  try
  	set infoPlistPath to ((POSIX path of (path to me)) & "Contents/Info.plist")
  	((infoPlistPath as POSIX file) as alias)
  	
  	set intendedAppName to "Test DVD" -- Hardcode intended App name because Name or Bundle Identifier changes should not be done lightly or accidentally.
  	
  	try
  		do shell script ("/usr/libexec/PlistBuddy -c 'Print :FGBuiltByMacLandScriptBuilder' " & (quoted form of infoPlistPath))
  		((((POSIX path of (path to me)) & "Contents/MacOS/" & intendedAppName) as POSIX file) as alias)
  	on error
  		try
  			activate
  		end try
  		display alert "
  “" & (name of me) & "” must be built by the “MacLand Script Builder” script." buttons {"Quit"} default button 1 as critical
  		quit
  		delay 10
  	end try
  	
  	set AppleScript's text item delimiters to "-"
  	set intendedBundleIdentifier to ("org.freegeek." & ((words of intendedAppName) as string))
  	set currentBundleIdentifier to ((do shell script ("/usr/libexec/PlistBuddy -c 'Print :CFBundleIdentifier' " & (quoted form of infoPlistPath))) as string)
  	if (currentBundleIdentifier is not equal to intendedBundleIdentifier) then error "“" & (name of me) & "” does not have the correct Bundle Identifier.
  
  
  Current Bundle Identifier:
  	" & currentBundleIdentifier & "
  
  Intended Bundle Identifier:
  	" & intendedBundleIdentifier
  on error checkInfoPlistError
  	if (checkInfoPlistError does not start with "Can’t make file") then
  		try
  			activate
  		end try
  		display alert checkInfoPlistError buttons {"Quit"} default button 1 as critical
  		quit
  		delay 10
  	end if
  end try
  
  try
  	set mainScptPath to ((POSIX path of (path to me)) & "Contents/Resources/Scripts/main.scpt")
  	((mainScptPath as POSIX file) as alias)
  	do shell script "osadecompile " & (quoted form of mainScptPath)
  	error "
  “" & (name of me) & "” must be exported as a Run-Only Script."
  on error checkReadOnlyErrorMessage
  	if ((checkReadOnlyErrorMessage does not contain "errOSASourceNotAvailable") and (checkReadOnlyErrorMessage does not start with "Can’t make file")) then
  		activate
  		display alert checkReadOnlyErrorMessage buttons {"Quit"} default button 1 as critical
  		quit
  		delay 10
  	end if
  end try
  
  
  set systemVersion to (system version of (system info))
  considering numeric strings
  	set isMojaveOrNewer to (systemVersion ≥ "10.14")
  	set isCatalinaOrNewer to (systemVersion ≥ "10.15")
  end considering
  
  
  set adminUsername to "Staff"
  if (isCatalinaOrNewer) then set adminUsername to "staff"
  set adminPassword to "[MACLAND SCRIPT BUILDER WILL REPLACE THIS PLACEHOLDER WITH OBFUSCATED ADMIN PASSWORD]"
  
  set buildInfoPath to ((POSIX path of (path to shared documents folder)) & "Build Info/")
  try
  	(((buildInfoPath & ".fgSetupSkipped") as POSIX file) as alias)
  	
  	try
  		do shell script ("mkdir " & (quoted form of buildInfoPath))
  	end try
  	try
  		set AppleScript's text item delimiters to "-"
  		do shell script ("touch " & (quoted form of (buildInfoPath & ".fgLaunchAfterSetup-org.freegeek." & ((words of (name of me)) as string)))) user name adminUsername password adminPassword with administrator privileges
  	end try
  	
  	try
  		-- For some reason, on Big Sur, apps are not opening unless we specify "-n" to "Open a new instance of the application(s) even if one is already running." All scripts have LSMultipleInstancesProhibited to this will not actually ever open a new instance.
  		do shell script "open -n -a '/Applications/Test Boot Setup.app'"
  	end try
  	
  	quit
  	delay 10
  end try
  
  
  set iTunesOrMusic to "iTunes"
  if (isCatalinaOrNewer) then set iTunesOrMusic to "Music"
  
  try
  	with timeout of 1 second
  		tell application iTunesOrMusic to quit
  	end timeout
  end try
  
  try
  	set volume output volume 75 without output muted
  end try
  try
  	set volume alert volume 100
  end try
  
  try
  	tell application "DVD Player" to activate
  end try
  
  if (not isMojaveOrNewer) then -- DVD Player on Mojave or newer isn't scriptable.
  	try
  		run script "
  			tell application \"DVD Player\"
  				try
  					activate
  				end try
  				repeat while app initializing
  					delay 1
  				end repeat
  				try
  					activate
  				end try
  				set audio volume to 190
  				play dvd
  			end tell
  		" -- Set DVD Player volume to 190 because 255 is max.
  	end try
  end if
  