spiegel-keyman/mac/installer/scripts/postinstall
2026-10-05 09:51:09 -04:00

81 lines
2.9 KiB
Bash
Executable file

#!/usr/bin/env bash
# Keyman is copyright (C) SIL Global. MIT License.
# input parameters to this script
# $1 - full path of installer package
# $2 - path to the target installation folder
# $3 - root mountpoint of the destination drive receiving the installation ("/" for current startup disk)
# $4 - path to the root directory of the currently booted OS
echo "Keyman for Mac: postinstall script"
# Get the actual user currently logged into the console
# This works even if the installer is run with sudo or as root
CONSOLE_USER=$(stat -f "%Su" /dev/console)
echo "console user is: $CONSOLE_USER"
# Get the home directory of the console user
USER_HOME=$(eval echo "~$CONSOLE_USER")
echo "user home directory is: $USER_HOME"
# Define the source file path within the package payload
# the input method, Keyman.app, is added to /tmp when building the installer
# $3 is the target volume mount point, e.g., "/"
SOURCE_FILE="$3tmp/Keyman.app"
echo "source file is: $SOURCE_FILE"
# Define the path to the Input Methods directory in the user's Library
DEST_DIR="$USER_HOME/Library/Input Methods"
echo "destination directory is: $DEST_DIR"
# Create the /Library/Input Methods directory if it doesn't exist
if [ ! -d "$DEST_DIR" ]; then
mkdir -p "$DEST_DIR"
# Set the user as owner of the Input Methods directory
chown "$CONSOLE_USER":staff "$DEST_DIR"
fi
# Copy the input method to the destination directory
# must use cp -R to maintain symbolic links and preserve permissions
# if we use -r, the input method's code signature will be invalid and the system will not load it
cp -R "$SOURCE_FILE" "$DEST_DIR/"
# Set the user as owner
chown -R "$CONSOLE_USER":staff "$DEST_DIR/Keyman.app"
# TODO: add code here to extract keyboard name from package path
echo "The installer package name is: $PACKAGE_PATH"
# define path of tmp file to write keyboard info
KEYBOARD_FILE_PATH="$3/var/tmp/keyboard-to-install.txt"
# write keyboard file
/bin/echo "sil_euro_latin" > "${KEYBOARD_FILE_PATH}"
# get the Process ID of the parent installer framework that called this script
PARENT_PID=$PPID
# run background process that launches Keyman Configuration app
(
# # wait up to 15 seconds for the installer processes to completely exit
# for i in {1..15}; do
# # ps -p checks if the specific parent installer process is still alive.
# # If it's dead, ps returns an error code, meaning the installer closed!
# if ! ps -p "$PARENT_PID" > /dev/null; then
# break
# fi
# sleep 1
# done
# give the file system a second to commit changes
sleep 1
# check if there is a current user and we are not sitting at the login window, then
# launch the Keyman Configuration under the current user with the custom install argument
if [ -n "$CONSOLE_USER" ] && [ "$CONSOLE_USER" != "loginwindow" ]; then
sudo -u "$CONSOLE_USER" open "/Applications/Keyman Configuration.app" --args --installation
fi
) &
exit 0