From 83e2ae691d68ece73986e329a2c531fc1ceca488 Mon Sep 17 00:00:00 2001 From: Michael <30936296+mtan93@users.noreply.github.com> Date: Tue, 20 Sep 2022 17:34:11 +0100 Subject: [PATCH] Create zt-dialog.sh --- zt-dialog.sh | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 zt-dialog.sh diff --git a/zt-dialog.sh b/zt-dialog.sh new file mode 100644 index 0000000..a31550a --- /dev/null +++ b/zt-dialog.sh @@ -0,0 +1,102 @@ +!/bin/bash +# +NETNAME=$@ +# List of apps to process +# Provide the display name as you prefer and the path to the app/file. ex: "Google Chrome,/Applications/Google Chrome.app" +# Tip: Check for something like print drivers using the pkg receipt, ex: "Konica-Minolta drivers,/var/db/receipts/jp.konicaminolta.print.package.C759.plist" +apps=( + "Joining,/tmp/ztnetjoined.log" + "Authorising,/tmp/ztnetauthed.log" + "Ready to connect,/tmp/ztnetready.log" +) + +# Dialog display settings, change as desired +title="Joining $NETNAME's ZeroTier Network" +message="Please wait whilst we join the $NET network for you." + +# location of dialog and dialog command file +dialogApp="/usr/local/bin/dialog" +dialog_command_file="/var/tmp/dialog.log" + +# check we are running as root +if [[ $(id -u) -ne 0 ]]; then + echo "This script should be run as root" + exit 1 +fi + +# *** functions + +# execute a dialog command +function dialog_command(){ + echo "$1" + echo "$1" >> $dialog_command_file +} + +function finalise(){ + dialog_command "progresstext: Successfully Joined the $NETNAME Network" + dialog_command "progress: complete" + dialog_command "button1text: Done" + dialog_command "button1: enable" + exit 0 +} + +function appCheck(){ +dialog_command "listitem: $(echo "$app" | cut -d ',' -f1): wait" +while [ ! -e "$(echo "$app" | cut -d ',' -f2)" ] +do + sleep 2 +done +dialog_command "progresstext: \"$(echo "$app" | cut -d ',' -f1)\" network" +dialog_command "listitem: $(echo "$app" | cut -d ',' -f1): ✅" +progress_index=$(( progress_index + 1 )) +echo "at item number $progress_index" +} + +# *** end functions + +# set progress total to the number of apps in the list +progress_total=${#apps[@]} + +# set icon based on whether computer is a desktop or laptop +hwType=$(/usr/sbin/system_profiler SPHardwareDataType | grep "Model Identifier" | grep "Book") +if [ "$hwType" != "" ]; then + icon="SF=laptopcomputer.and.arrow.down,weight=thin,colour1=#51a3ef,colour2=#5154ef" + else + icon="SF=desktopcomputer.and.arrow.down,weight=thin,colour1=#51a3ef,colour2=#5154ef" +fi + +dialogCMD="$dialogApp -p --title \"$title\" \ +--message \"$message\" \ +--icon \"$icon\" \ +--progress $progress_total \ +--button1text \"Please Wait\" \ +--button1disabled" + +# create the list of apps +listitems="" +for app in "${apps[@]}"; do + listitems="$listitems --listitem '$(echo "$app" | cut -d ',' -f1)'" +done + +# final command to execute +dialogCMD="$dialogCMD $listitems" + +echo "$dialogCMD" + +# Launch dialog and run it in the background sleep for a second to let thing initialise +eval "$dialogCMD" & +sleep 2 + +progress_index=0 + +(for app in "${apps[@]}"; do + step_progress=$(( 1 + progress_index )) + dialog_command "progress: $step_progress" + appCheck & +done + +wait) + +# all done. close off processing and enable the "Done" button +finalise +rm -rf /tmp/ztnetauthed.log /tmp/ztnetready.log /tmp/ztnetjoined.log