From 9e474ffadeaa01b01e409b7c3a4c1aa15ed14499 Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Thu, 16 Mar 2023 19:09:47 +0100 Subject: [PATCH] feat: specify cfg location; create single executable file --- .gitignore | 1 + Makefile | 14 ++++++++++++++ de-p1st-installer.sh | 32 ++++++++++++++++++++++++-------- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 43f4d1b..f9ed7f7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /.idea/ /arch-installer/ /de-p1st-installer-*-any.pkg.tar.zst +/*-binary.sh \ No newline at end of file diff --git a/Makefile b/Makefile index d6346e0..7f653fa 100644 --- a/Makefile +++ b/Makefile @@ -3,3 +3,17 @@ all: test test: shellcheck --check-sourced --external-sources de-p1st-installer.sh + +SINGLE_FILE_CFG := example-vbox.cfg +SINGLE_FILE_DST := example-vbox-binary.sh +single-file: # Creates a single, executable .sh file a given configuration file. + py-replace --text 'main "' --repl '# main "' --count 1 \ + < de-p1st-installer.sh \ + > single-file-temp.sh + chmod +x single-file-temp.sh + bash -v ./single-file-temp.sh example-vbox.cfg 2>&1 | \ + py-regex-replace --pattern '^\s*source\s+.+$$' --repl ': # sourced file included below:' --count '-1' | \ + py-replace --text '# main "' --repl 'main "' --count 1 \ + > ${SINGLE_FILE_DST} + rm single-file-temp.sh + chmod +x ${SINGLE_FILE_DST} diff --git a/de-p1st-installer.sh b/de-p1st-installer.sh index f8936f1..d636c41 100755 --- a/de-p1st-installer.sh +++ b/de-p1st-installer.sh @@ -4,16 +4,18 @@ set -e # Exit on undefined variable reference. set -u -# Check if run from installation or locally (development setup). -# -> Print commands before they get executed (set -v). -# -> Determine library directory. -script_dir="$(dirname "${BASH_SOURCE[0]}")" || exit 1 -if [ "${script_dir}" = '/usr/bin' ]; then - # This script has been installed. +script_dir="$(dirname "${BASH_SOURCE[0]}")" + +function script_is_installed(){ + # Check if this script is run installed or locally (development setup). + [ "${script_dir}" = '/usr/bin' ] +} + +if script_is_installed; then lib_dir='/usr/lib/de-p1st-installer' else - # This script is run locally (development setup). lib_dir='./lib' + # Print commands before they get executed. set -v fi @@ -25,9 +27,23 @@ source "${lib_dir}"/user-input.sh # shellcheck source=lib/block-device.sh source "${lib_dir}"/block-device.sh +# Get path of configuration file. +if [ $# -eq 0 ]; then + # No arguments given. + if script_is_installed; then + cfg_file=/etc/de-p1st-installer/installer.cfg + else + cfg_file="${script_dir}"/installer.cfg + fi +else + # >= 1 arguments given. + # First argument is the configuration file! + cfg_file="${1}" +fi + # Source the configuration file. # shellcheck source=installer.cfg -source /etc/de-p1st-installer/installer.cfg +source "${cfg_file}" function main() { # @pre