#!/bin/sh
 set -e
+. "${HOME}/.nonpath_bins/plomlib.sh"
 
 PATH_BORGKEYS="${HOME}/.config/borg/keys"
 NAME_REPO=borg
     REPO="${NAME_SERVER}:${NAME_REPO}"
     while true; do
         if [ -z "${BORG_PASSPHRASE}" ]; then
-            stty -echo
-            printf "Borg passphrase: "
-            read passphrase
-            stty echo
-            printf "\n"
-            export BORG_PASSPHRASE="${passphrase}"
+           get_passphrase
+            export BORG_PASSPHRASE="${PASSPHRASE}"
         fi
         set +e
         ARCHIVES=$(borg list "${REPO}")  # separate step so we may fail early on bad passphrase
         elif [ "${RESULT}" != "2" ]; then
             echo "Aborting due to unexpected ${NAME_BORGAPP} error."
             exit 1
-        else
-            export BORG_PASSPHRASE=""
         fi
+        export BORG_PASSPHRASE=""
     done
     ARCHIVE=$(echo "${ARCHIVES}" | grep "${NAME_ARCHIVE}" | tail -1 | cut -f1 -d' ')
     echo "Pulling archive: ${ARCHIVE}"
 
-#!/bin/sh
-set -e
-
 . ../../constants.sh
 . "${PATH_MANY_MISC}"
+. "${HOME}/.nonpath_bins/plomlib.sh"
 
 PATH_REL_SECRETS=to_usb
 PATH_SECRETS=${PATH_USER_HOME}/${PATH_REL_SECRETS}
 
 get_system_name_arg() {
-    thinkpad_names="x220 w530 t490s"
-    legal_system_names="${thinkpad_names} h610m"
-    found=0
-    for system_name_i in $legal_system_names; do
-        if [ "$1" = "$system_name_i" ]; then
-            found=1
-            system_name="${system_name_i}"
+    THINKPAD_NAMES="x220 w530 t490s"
+    LEGAL_SYSTEM_NAMES="${THINKPAD_NAMES} h610m"
+    FOUND=0
+    for SYSTEM_NAME_I in $LEGAL_SYSTEM_NAMES; do
+        if [ "$1" = "$SYSTEM_NAME_I" ]; then
+            FOUND=1
+            SYSTEM_NAME="${SYSTEM_NAME_I}"
            break
         fi
     done
-    if [ "$found" = 0 ]; then
+    if [ "${FOUND}" = 0 ]; then
         echo "Need legal system name."
         false
     fi
-    system_class_name=
-    for thinkpad_name in $thinkpad_names; do
-        if [ "${system_name}" = "${thinkpad_name}" ]; then
-           system_class_name=thinkpad
+    SYSTEM_CLASS_NAME=
+    for THINKPAD_NAME in $THINKPAD_NAMES; do
+        if [ "${SYSTEM_NAME}" = "${THINKPAD_NAME}" ]; then
+           SYSTEM_CLASS_NAME=thinkpad
            break
        fi
     done
 
 abort_if_exists() {
     if [ -e "${1}" ]; then
-        echo "Aborting because ${1} already exists."
+        echo "Aborting because $1 already exists."
         exit 1
     fi
 }
 
 abort_if_not_user() {
     if [ `whoami` != "$1" ]; then
-        echo "Must be run as $1."
+        echo "Must be run as ${1}."
         exit 1
     fi
 }
 
 expect_min_n_args 1 "(system name)" "$@"
 abort_if_offline
 get_system_name_arg "$1"
-MIN_MODULES="all ${system_class_name} ${system_name}"
+MIN_MODULES="all ${SYSTEM_CLASS_NAME} ${SYSTEM_NAME}"
 
 # For upgrading only very selectively prepare /etc/ files.
 PATH_REL_APT=apt
 apt -y autoremove
 
 # Set hostname and FQDN.
-echo "${system_name}" > /etc/hostname
-hostname "${system_name}"
+echo "${SYSTEM_NAME}" > /etc/hostname
+hostname "${SYSTEM_NAME}"
 final_ip="127.0.1.1"
 for ip in $(hostname -I); do
     if [ $(echo "${ip}" | grep ':' | wc -l) -eq 1 ]; then
     final_ip="${ip}"
 done
 echo "127.0.0.1 localhost.localdomain localhost" > /etc/hosts
-echo "${final_ip} ${system_name}" >> /etc/hosts
+echo "${final_ip} ${SYSTEM_NAME}" >> /etc/hosts
 
 # Ensure package installation state as defined by what packages are
 # defined as required by Debian policy and by settings in ./apt-mark/.
 mark_nonrequireds_auto
-if [ "$system_name" = "h610m" ]; then
+if [ "${SYSTEM_NAME}" = "h610m" ]; then
     # Hold kernel known to work with nvidia-drivers.
     apt-mark hold linux-image-amd64 linux-headers-amd64
 fi
 
     sleep 0.1
 done
 while true; do
-    stty -echo
-    printf "\nSecrets passphrase: "
-    read SECRETS_PASS 
-    stty echo
-    echo "" # newline so user knows their input return was accepted
+    get_passphrase
     set +e
-    echo "${SECRETS_PASS}" | pmount "${PATH_DEV}"
+    echo "${PASSPHRASE}" | pmount "${PATH_DEV}"
     RESULT=$?
     set -e
     if [ "${RESULT}" = "0" ]; then
 
 cd ../..
 PATH_REPO=$(pwd)
-CONTAINS_TICK=$(echo "${SECRETS_PASS}" | grep "'" | wc -l)
+CONTAINS_TICK=$(echo "${PASSPHRASE}" | grep "'" | wc -l)
 if [ "${CONTAINS_TICK}" = "1" ]; then
   echo "Cannot pass to user script passphrase with illegal character, aborting."
   exit 1
 fi
-su -l "${USERNAME}" -c "cd ${PATH_REPO}/testing/scripts && ./setup_secrets_user.sh '${SECRETS_PASS}'"
+su -l "${USERNAME}" -c "cd ${PATH_REPO}/testing/scripts && ./setup_secrets_user.sh '${PASSPHRASE}'"
 
 set -e
 . ./_misc.sh
 
-
-BORG_PASSPHRASE="$1"
 PATH_REPOS="${HOME}/repos"
 PATH_BORGKEYS="${HOME}/.config/borg/keys"
 REPOS_SITE_DOMAIN=plomlompom.com
 REMOTE_PATH_REPOS=/var/repos
 NAME_BORGAPP=borgplom
+if [ ! -z "$1" ]; then
+    export BORG_PASSPHRASE="$1"
+fi
 
 abort_if_not_user "${USERNAME}"
 abort_if_offline