From 87aad362627137c2cc1a2cd330df532a8948b748 Mon Sep 17 00:00:00 2001 From: Jason Mehring Date: Sun, 15 Mar 2015 14:56:08 -0400 Subject: [PATCH] Added the ability for template files to be found side by side the original file which allows template flavor suffix to be added to package lists as well as most any other file type now. - created a new function to check if a file exists that was refactored out od the templateFile function - Added elementIn function which checks if an element exists within an array --- functions.sh | 54 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/functions.sh b/functions.sh index 548a531..23ae921 100755 --- a/functions.sh +++ b/functions.sh @@ -164,6 +164,19 @@ setArrayAsGlobal() { } +# ------------------------------------------------------------------------------ +# Checks if the passed element exists in passed array +# $1: Element to check for +# $2: Array to check for element in +# +# Returns 0 if True, or 1 if False +# ------------------------------------------------------------------------------ +elementIn () { + local element + for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done + return 1 +} + # ------------------------------------------------------------------------------ # Spilts the path and returns an array of parts # @@ -231,6 +244,15 @@ templateDir() { echo "${dir}" } +exists() { + filename="${1}" + if [ -e "${filename}" ]; then + echo "${filename}" + return 0 + fi + return 1 +} + templateFile() { local file="$1" local suffix="$2" @@ -239,15 +261,35 @@ templateFile() { splitPath "${file}" path_parts - # Append suffix to filename (before extension) + # No template flavor + if [ "X{template_flavor}" == "X" ]; then + if [ -e "${file}" ]; then + echo "${file}" + fi + return + fi + + # Locate file in directory named after flavor if [ "${suffix}" ]; then - file="${template_dir}/${path_parts[base]}_${suffix}${path_parts[dotext]}" + # Append suffix to filename (before extension) + + # (../SCRIPTSDIR/minimal/packages_qubes_suffix.list) + exists "${template_dir}/${path_parts[base]}_${suffix}${path_parts[dotext]}" || true + + # (../SCRIPTSDIR/minimal/packages_qubes_minimal_suffix.list) + exists "${template_dir}/${path_parts[base]}_${suffix}_${template_flavor}${path_parts[dotext]}" || true + + # (../SCRIPTSDIR/packages_qubes_minimal_suffix.list) + exists "${SCRIPTSDIR}/${path_parts[base]}_${suffix}_${template_flavor}${path_parts[dotext]}" || true else - file="${template_dir}/${path_parts[base]}${path_parts[dotext]}" - fi + # (../SCRIPTSDIR/minimal/packages_qubes.list) + exists "${template_dir}/${path_parts[base]}${path_parts[dotext]}" || true + + # (../SCRIPTSDIR/minimal/packages_qubes_minimal.list) + exists "${template_dir}/${path_parts[base]}_${template_flavor}${path_parts[dotext]}" || true - if [ -f "${file}" ]; then - echo "${file}" + # (../SCRIPTSDIR/packages_qubes_minimal.list) + exists "${SCRIPTSDIR}/${path_parts[base]}_${template_flavor}${path_parts[dotext]}" || true fi }