diff --git a/builder_fix_filenames b/builder_fix_filenames index 6d3848f..284cbbc 100755 --- a/builder_fix_filenames +++ b/builder_fix_filenames @@ -3,7 +3,7 @@ # Check template name length and fix if not under 32 characters # Return the result -. ./functions.sh > /dev/null +. ./functions-name.sh > /dev/null # Check for custom template name templateNameDist "${TEMPLATE_NAME}" diff --git a/builder_setup b/builder_setup index b1cefb2..ba265b9 100755 --- a/builder_setup +++ b/builder_setup @@ -2,8 +2,6 @@ # Setup env variables -. ./functions.sh > /dev/null - case "$DIST" in fc*) DISTRIBUTION=fedora diff --git a/functions-name.sh b/functions-name.sh new file mode 100644 index 0000000..b2c3b0c --- /dev/null +++ b/functions-name.sh @@ -0,0 +1,81 @@ +#!/bin/bash +# vim: set ts=4 sw=4 sts=4 et : + +set -e + +VERBOSE=${VERBOSE:-1} +DEBUG=${DEBUG:-0} + +templateFlavorPrefix() { + local template_flavor=${1-${TEMPLATE_FLAVOR}} + + # If TEMPLATE_FLAVOR_PREFIX is not already an array, make it one + if ! [[ "$(declare -p TEMPLATE_FLAVOR_PREFIX 2>/dev/null)" =~ ^declare\ -a.* ]] ; then + TEMPLATE_FLAVOR_PREFIX=( ${TEMPLATE_FLAVOR_PREFIX} ) + fi + + for element in "${TEMPLATE_FLAVOR_PREFIX[@]}" + do + if [ "${element%:*}" == "${DIST}+${template_flavor}" ]; then + echo ${element#*:} + return + fi + done + + echo "${DIST}${template_flavor:++}" +} + +templateNameDist() { + local dist_name="${1}" + template_name="$(templateName)" && dist_name="${template_name}" + + # XXX: Temp hack to shorten name + if [ ${#dist_name} -ge 32 ]; then + if [ ${#template_name} -lt 32 ]; then + dist_name="${template_name}" + else + dist_name="${dist_name:0:31}" + fi + fi + + # Remove and '+' characters from name since they are invalid for name + dist_name="${dist_name//+/-}" + echo ${dist_name} +} + +templateName() { + local template_flavor=${1-${TEMPLATE_FLAVOR}} + retval=1 # Default is 1; mean no replace happened + + # Only apply options if $1 was not passed + if [ -n "${1}" ]; then + local template_options= + else + local template_options="${TEMPLATE_OPTIONS// /+}" + fi + + local template_name="$(templateFlavorPrefix ${template_flavor})${template_flavor}${template_options:++}${template_options}" + + # If TEMPLATE_LABEL is not already an array, make it one + if ! [[ "$(declare -p TEMPLATE_LABEL 2>/dev/null)" =~ ^declare\ -a.* ]] ; then + TEMPLATE_LABEL=( ${TEMPLATE_LABEL} ) + fi + + for element in "${TEMPLATE_LABEL[@]}"; do + if [ "${element%:*}" == "${template_name}" ]; then + template_name="${element#*:}" + retval=0 + break + fi + done + + if [ ${#template_name} -ge 32 ]; then + error "Template name is greater than 31 characters: ${template_name}" + error "Please set an alias" + error "Exiting!!!" + exit 1 + fi + + echo ${template_name} + return $retval +} diff --git a/functions.sh b/functions.sh index 52ae51c..37b7d8b 100755 --- a/functions.sh +++ b/functions.sh @@ -6,6 +6,8 @@ set -e VERBOSE=${VERBOSE:-1} DEBUG=${DEBUG:-0} +. ./functions-name.sh + ################################################################################ # Global functions ################################################################################ @@ -152,86 +154,8 @@ splitPath() { setArrayAsGlobal PARTS $return_global_var } -templateFlavor() { - echo ${TEMPLATE_FLAVOR} -} - -templateFlavorPrefix() { - local template_flavor=${1-$(templateFlavor)} - - # If TEMPLATE_FLAVOR_PREFIX is not already an array, make it one - if ! [[ "$(declare -p TEMPLATE_FLAVOR_PREFIX 2>/dev/null)" =~ ^declare\ -a.* ]] ; then - TEMPLATE_FLAVOR_PREFIX=( ${TEMPLATE_FLAVOR_PREFIX} ) - fi - - for element in "${TEMPLATE_FLAVOR_PREFIX[@]}" - do - if [ "${element%:*}" == "${DIST}+${template_flavor}" ]; then - echo ${element#*:} - return - fi - done - - echo "${DIST}${template_flavor:++}" -} - -templateNameDist() { - local dist_name="${1}" - template_name="$(templateName)" && dist_name="${template_name}" - - # XXX: Temp hack to shorten name - if [ ${#dist_name} -ge 32 ]; then - if [ ${#template_name} -lt 32 ]; then - dist_name="${template_name}" - else - dist_name="${dist_name:0:31}" - fi - fi - - # Remove and '+' characters from name since they are invalid for name - dist_name="${dist_name//+/-}" - echo ${dist_name} -} - -templateName() { - local template_flavor=${1-$(templateFlavor)} - retval=1 # Default is 1; mean no replace happened - - # Only apply options if $1 was not passed - if [ -n "${1}" ]; then - local template_options= - else - local template_options="${TEMPLATE_OPTIONS// /+}" - fi - - local template_name="$(templateFlavorPrefix ${template_flavor})${template_flavor}${template_options:++}${template_options}" - - # If TEMPLATE_LABEL is not already an array, make it one - if ! [[ "$(declare -p TEMPLATE_LABEL 2>/dev/null)" =~ ^declare\ -a.* ]] ; then - TEMPLATE_LABEL=( ${TEMPLATE_LABEL} ) - fi - - for element in "${TEMPLATE_LABEL[@]}"; do - if [ "${element%:*}" == "${template_name}" ]; then - template_name="${element#*:}" - retval=0 - break - fi - done - - if [ ${#template_name} -ge 32 ]; then - error "Template name is greater than 31 characters: ${template_name}" - error "Please set an alias" - error "Exiting!!!" - exit 1 - fi - - echo ${template_name} - return $retval -} - templateDir() { - local template_flavor=${1-$(templateFlavor)} + local template_flavor=${1-${TEMPLATE_FLAVOR}} # If TEMPLATE_FLAVOR_DIR is not already an array, make it one if ! [[ "$(declare -p TEMPLATE_FLAVOR_DIR 2>/dev/null)" =~ ^declare\ -a.* ]] ; then @@ -341,7 +265,7 @@ callTemplateFunction() { local calling_script="$1" local calling_arg="$2" local functionExec="$3" - local template_flavor="$(templateFlavor)" + local template_flavor="${TEMPLATE_FLAVOR}" ${functionExec} "${calling_script}" \ "${calling_arg}" \ @@ -352,7 +276,7 @@ callTemplateFunction() { # Long name (wheezy+whonix-gateway+proxy) ${functionExec} "${calling_script}" \ "${calling_arg}" \ - "$(templateFlavor)+${option}" + "${TEMPLATE_FLAVOR}+${option}" # Short name (wheezy+proxy) ${functionExec} "${calling_script}" \