#!/usr/bin/env bash build::addLicense() { local target="${1:?}" local licenseText="# # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . " printf '%s' "${licenseText}" >> "${target}" } build::headerRdlink() { local target="${1:?}" local fileHeader="#!/usr/bin/env bash # rdlink.sh [OPTIONS] # [OPTION] # -- : End of options marker # -* : Other options are ignored # # Version: $(git describe --long --tags --always --dirty) # Author: Martin Winkler # Origin: https://winklerfamilie.eu/git/efelon/rdlink.git " printf '%s' "${fileHeader}" > "${target}" build::addLicense "${target}" } build::headerStarter() { local target="${1:?}" local fileHeader="#!/usr/bin/env bash # starter.sh # # Start or source your application at the end to have the # environment variable \$_origin available. # # Version: $(git describe --long --tags --always --dirty) # Author: Martin Winkler # Origin: https://winklerfamilie.eu/git/efelon/rdlink.git " printf '%s' "${fileHeader}" > "${target}" build::addLicense "${target}" } build::rdlink() { readonly build_dir="$(cd "$(dirname -- "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" readonly rdlinkRelease="${build_dir}/release/rdlink.sh" readonly starterRelease="${build_dir}/release/starter.sh" readonly color_green='\033[1;32m' readonly color_red='\033[1;31m' readonly color_less='\033[0m' readonly shellcheck_cmd="$(command -v shellcheck)" local testresult_graphic= testresult_graphic="${color_green}✔${color_less}" # alt. symbol ✓ testresult_graphic_fail="${color_red}✗${color_less}" # alt. symbol ≠ if ! mkdir -p "$(dirname -- "${rdlinkRelease}")" ; then printf ' [ERROR] Cannot create release directory\n' return 1 fi printf '# Building releases based on rdlink.sh\n' build::headerRdlink "${rdlinkRelease}" printf ' (Extracting script part and removing debug information)\n' printf ' Building release/rdlink.sh...' # Get essential script content # * extract script part # * remove script marker # * remove debug function calls # * remove debug options from rdlink # * replace multiple empty lines with one if sed -n '/### Script.*/,/### Script EOF/ p' < "${build_dir}/rdlink.sh" \ | sed '/### Script.*/d; /^[[:blank:]]*info/d; /-d\{1,2\}/,/;;/d' \ | sed '/^$/N; /^\n$/D' \ >> "${rdlinkRelease}" 2>/dev/null ; then printf '%b' "${testresult_graphic}\n" else printf '%b' "${testresult_graphic_fail}\n" return 1 fi { build::headerStarter "${starterRelease}" printf ' Building release/starter.sh...' if sed -n '/### Script.*/,/### Script Starter EOF/ p' < "${build_dir}/rdlink.sh" \ | sed '/### Script.*/d; /^[[:blank:]]*info/d; /-d\{1,2\}/,/;;/d' \ | sed '/^$/N; /^\n$/D' \ >> "${starterRelease}" 2>/dev/null ; then printf '%b' "${testresult_graphic}\n" else printf '%b' "${testresult_graphic_fail}\n" return 1 fi printf '_origin="$(cd "$(dirname -- "$(rl::rdlink "${BASH_SOURCE[0]}")")" >/dev/null 2>&1 && pwd)"\n' >> "${starterRelease}" printf 'export _origin\n\n' >> "${starterRelease}" printf '# Uncomment one of the next lines and replace "tostart.sh" with your script\n' >> "${starterRelease}" printf '# to have _origin available even on platforms not supporting readlink -f.\n\n' >> "${starterRelease}" printf '#bash "${_origin}/tostart.sh"\n' >> "${starterRelease}" printf '#. "${_origin}/tostart.sh"\n' >> "${starterRelease}" printf '#"${_origin}/tostart.sh"\n' >> "${starterRelease}" } if [[ "${shellcheck_cmd}" ]] ; then printf '%b\n' " Running shellcheck for rdlink...\033[1A\033[s\n" if "${shellcheck_cmd}" "${rdlinkRelease}" ; then printf '%b' "\033[u${testresult_graphic}\n" else return 1 fi printf '%b\n' " Running shellcheck for starter...\033[1A\033[s\n" if "${shellcheck_cmd}" "${starterRelease}" ; then printf '%b' "\033[u${testresult_graphic}\n" else return 1 fi fi chmod +x "${rdlinkRelease}" return 0 } build::rdlink