#!/usr/bin/env bash # This is a pre-allocated string of 39 chars of whitespace # DO NOT CHANGE THIS without great care - it will break header length # calculations LENS=( ) export SELF # Absolute path to the installer script (me!) export TMP # Path to tmp install staging directory export PAYLOAD # Path to the extracted payload directory export NOCLEANUP=0 # Do not clean up deploy resources. Useful for debugging export EXTRACTONLY=0 # Execute extract, skipping deployment and cleanup export DECOMPRESS='{{ DECOMPRESS }}' # Command to decompress data # Header: 0 # runscript: 1 # payload: 2 extract_chunk_index() { local src="${1}" local dest="${2}" local index="${3}" local skip=0 # Byte count to skip into source local of # Output path with 'of=' prefix. Only used if dest is not '-' for (( i = 0; i < index; i++ )); do skip=$(( skip + LENS[$i] )) done [ "${dest}" != '-' ] && of="of=${dest}" # Extract the requested chunk index dd bs=${LENS[$index]} count=1 iflag=skip_bytes skip=${skip} \ if=${src} ${of} 2>/dev/null } extract_header() { extract_chunk_index "${SELF}" "${TMP}/header.sh" 0 } extract_libinstall() { extract_chunk_index "${SELF}" '-' 1 | ${DECOMPRESS} | tar -C "${TMP}" -x } extract_runscript() { extract_chunk_index "${SELF}" '-' 2 | ${DECOMPRESS} > "${TMP}/run.sh" } extract_payload() { extract_chunk_index "${SELF}" '-' 3 | ${DECOMPRESS} | tar -C "${TMP}" -x export PAYLOAD="${TMP}/pkg" } main() { local args=(${@}) SELFDIR="$(cd $(dirname ${0}) && pwd)" SELF="${SELFDIR}/$(basename ${0})" TMP="$(mktemp -d /tmp/installer-XXXXXXX)" extract_header extract_libinstall extract_runscript cd "${TMP}" source libinstall/template.sh source libinstall/ensure.sh source libinstall/colorize.sh source run.sh for (( i = 0; i < ${#args[@]}; i++ )); do # NOTE: Do not support short switches here to reduce the probability of # conflicting with a switch provided by the downstream run.sh if [ "${args[$i]}" = '--extract-only' ]; then export EXTRACTONLY=1 export NOCLEANUP=1 fi done if [ "${EXTRACTONLY}" = '1' ]; then printf 'Extracting...\n' extract_payload printf 'Installation payload available at "%s"\n' "${TMP}/pkg" printf 'Skipping cleanup\n' return 0 elif [ "$(type -t deploy)" == 'function' ]; then # Function 'deploy' is provided by run.sh deploy ${@} else printf "ERROR: Deploy function not found.\n" fi # Cleanup if [ "${NOCLEANUP}" -ne 0 ]; then rm -rf "${TMP}" fi } main ${@} exit 0