#!/bin/sh # The MIT License (MIT) # # Copyright © 2020-2025 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # mashin [options...] [filepaths...] # # # MAke SHell INstaller makes an installer which creates/updates files and # folders. The output is shell source code which uses base64-encoded gzipped # payloads. # # The options are, available both in single and double-dash versions # # -d change the destination folder with the next argument # -dest change the destination folder with the next argument # -destination change the destination folder with the next argument # # -h show this help message # -help show this help message # # -r use filepaths relative to the destination folder # -rel use filepaths relative to the destination folder # -relative use filepaths relative to the destination folder # # -x make installer enable the executable bit on all results # -exec make installer enable the executable bit on all results # -executable make installer enable the executable bit on all results case "$1" in -h|--h|-help|--help) awk '/^# +mashin /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac rel_paths=0 dest_folder='.' make_exec=0 while true; do case "$1" in -d|--d|-dest|--dest|-destination|--destination) shift if [ $# -eq 0 ]; then printf "\e[38;2;204;0;0mmissing destination folder\e[0m\n" >&2 exit 1 fi dest_folder="$(echo "$1" | sed 's-//*$--')" if echo "${dest_folder}" | grep -q -v '^\./'; then if echo "${dest_folder}" | grep -q -v '^/'; then dest_folder="$(echo "${dest_folder}" | sed 's-^-./-')" fi fi shift ;; -r|--r|-rel|--rel|-relative|--relative) rel_paths=1 shift ;; -x|--x|-exec|--exec|-executable|--executable) make_exec=1 shift ;; --) shift break ;; *) break ;; esac done if [ $# -eq 0 ]; then awk '/^# +shin /, /^$/ { gsub(/^# ?/, ""); print }' "$0" >&2 exit 0 fi printf "#!/bin/sh\n" files=0 for arg in "$@"; do if [ ! -f "${arg}" ]; then printf "\e[38;2;204;0;0m'%s' isn't a file\e[0m\n" "${arg}" >&2 exit 1 fi files="$((files + 1))" if [ "${rel_paths}" -eq 1 ]; then name="$(basename ${arg} | sed -E 's-^\.?/--')" else name="$(echo ${arg} | sed -E 's-^\.?/--')" fi dest_path="$(printf "%s/%s" "${dest_folder}" "${name}")" folder="$(dirname "${dest_path}")" # dash doesn't support single-line `<<<` redirects printf "mkdir -p %s && { base64 -d | gzip -d; } > %s << 'EOF'\n" \ "${folder}" "${dest_path}" cat "${arg}" | gzip | base64 -w 0 printf "\nEOF\n" if [ "${make_exec}" -eq 1 ]; then printf "chmod +x %s\n" "${dest_path}" fi done if [ "${files}" -eq 0 ]; then printf "\e[38;2;204;0;0mno files given\e[0m\n" >&2 exit 1 fi