From 66f2dd25f2636330a9ef459afc1a64b72a8c3803 Mon Sep 17 00:00:00 2001 From: Dan Puttick Date: Wed, 9 Aug 2017 18:03:52 -0400 Subject: [PATCH] Refactor init.sh to use functions --- .../root_partition/opt/groomer/config.sh | 1 + .../root_partition/opt/groomer/init.sh | 60 +++++++++++-------- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/circlean_fs/root_partition/opt/groomer/config.sh b/circlean_fs/root_partition/opt/groomer/config.sh index 4ef51d9..d172c5f 100755 --- a/circlean_fs/root_partition/opt/groomer/config.sh +++ b/circlean_fs/root_partition/opt/groomer/config.sh @@ -28,3 +28,4 @@ readonly PUMOUNT="/usr/bin/pumount" # Config flags readonly DEBUG=false +readonly MUSIC=true diff --git a/circlean_fs/root_partition/opt/groomer/init.sh b/circlean_fs/root_partition/opt/groomer/init.sh index 5c68def..5d3568d 100755 --- a/circlean_fs/root_partition/opt/groomer/init.sh +++ b/circlean_fs/root_partition/opt/groomer/init.sh @@ -1,37 +1,49 @@ #!/bin/bash -# set -e (exit when a line returns non-0 status) and -x (xtrace) flags -set -e -set -x - -# Import constants from config file -source ./config.sh - -if [ ${ID} -ne 0 ]; then - echo "GROOMER: This script has to be run as root." - exit -fi - clean(){ - if [ ${DEBUG} = true ]; then + if [ "${DEBUG}" = true ]; then sleep 20 fi echo "GROOMER: cleaning up after init.sh." - ${SYNC} + "${SYNC}" # Stop the music from playing - kill -9 $(cat /tmp/music.pid) + kill -9 "$(cat /tmp/music.pid)" rm -f /tmp/music.pid } -trap clean EXIT TERM INT +check_is_root() { + if [ "${ID}" -ne 0 ]; then + echo "GROOMER: This script has to be run as root." + exit + fi +} -# Start music -./music.sh & -echo $! > /tmp/music.pid +start_music() { + ./music.sh & + echo $! > /tmp/music.pid +} -# List block storage devices for debugging -if [ ${DEBUG} = true ]; then - lsblk |& tee -a ${DEBUG_LOG} -fi +run_groomer() { + if [ "${DEBUG}" = true ]; then + lsblk |& tee -a "${DEBUG_LOG}" # list block storage devices for debugging + su "${USERNAME}" -c ./mount_dest.sh |& tee -a "${DEBUG_LOG}" + else + su "${USERNAME}" -c ./mount_dest.sh + fi +} -su ${USERNAME} -c ./mount_dest.sh |& tee -a ${DEBUG_LOG} +main() { + set -eu # exit when a line returns non-0 status, treat unset variables as errors + trap clean EXIT TERM INT # run clean when the script ends or is interrupted + check_is_root + source ./config.sh # get config values + if [ "${DEBUG}" = true ]; then + set -x + fi + if [ "${MUSIC}" = true ]; then + start_music + fi + run_groomer +} + +main