#!/usr/bin/env zsh # vi: set ft=zsh tw=80 ts=2 function configureLogging() { local output=tostdout level=info [ -n "${logfile}" ] && output=${logfile} [ "${verbose}" = true ] && level=debug lop setoutput -l ${level} ${output} } function getUsage() { local cmdName=$1 text='' read -r -d '' text <<- USAGE Usage: $cmdName [-v] [-d FILE] --hostname NAME Configure host specific settings. Options: --hostname NAME Set NAME as current host's host name. -d FILE, --logfile FILE Print log message to logfile instead of stdout. -v, --verbose Be more verbose. ---- $cmdName 0.1.0 Copyright (C) 2022 Rezart Qelibari, Astzweig GmbH & Co. KG License EUPL-1.2. There is NO WARRANTY, to the extent permitted by law. USAGE print -- ${text} } function quitSystemPreferences() { ps -A -o pid,comm | grep "MacOS/System Settings" | awk '{print "kill -9 " $1}' | /bin/sh } function setComputerName() { scutil --set ComputerName "${hostname}" scutil --set HostName "${hostname}" scutil --set LocalHostName "${hostname}" systemsetup -setcomputername "${hostname}" systemsetup -setlocalsubnetname "${hostname}" } function configureComputerHostname() { local currentComputerName="`scutil --get ComputerName`" if [[ "${currentComputerName}" != "${hostname}" ]]; then lop -- -i 'Hostname of computer has not been set.' -i "Will set to ${hostname}." indicateActivity -- 'Set computer name' setComputerName else lop -- -i 'Hostname of computer seems to have already been set. Skipping.' -i "Hostname: $currentComputerName" fi } function requireRootPrivileges() { [[ `id -u` -eq 0 ]] || { lop -- -e 'Need root access to change hostname. Aborting.'; return 10 } } function main() { local cmdPath=${1} cmdName=${1:t} shift eval "`getUsage $cmdName | docopts -f -V - -h - : "$@"`" configureLogging requireRootPrivileges || return $? lop -y h1 -- -i 'Configure System Settings' indicateActivity -- 'Quitting System Preferences' quitSystemPreferences configureComputerHostname } if [[ "${ZSH_EVAL_CONTEXT}" == toplevel || "${ZSH_EVAL_CONTEXT}" == cmdarg ]]; then _DIR="${0:A:h}" source autoload-zshlib main $0 "$@" fi