File: mashin.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright (c) 2026 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the "Software"), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 # mashin [options...] [files...]
  27 #
  28 # MAke SHell INstaller makes an installer which creates/updates files and
  29 # folders. The output is shell source code which uses base64-encoded gzipped
  30 # payloads.
  31 #
  32 # The options are, available both in single and double-dash versions
  33 #
  34 #   -d, -dest, -destination    make the next argument the destination folder
  35 #   -h, -help                  show this help message
  36 #   -r, -rel, -relative        use filepaths relative to the destination folder
  37 #   -x, -exec, -executable     make installer enable the exec bit on all files
  38 
  39 
  40 case "$1" in
  41     -h|--h|-help|--help)
  42         awk '/^# +mashin /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  43         exit 0
  44     ;;
  45 esac
  46 
  47 rel_paths=0
  48 dest_folder='.'
  49 make_exec=0
  50 
  51 while [ $# -gt 0 ]; do
  52     case "$1" in
  53         -d|--d|-dest|--dest|-destination|--destination)
  54             shift
  55             if [ $# -eq 0 ]; then
  56                 printf "missing destination folder\n" >&2
  57                 exit 1
  58             fi
  59 
  60             dest_folder="$(echo "$1" | sed 's-//*$--')"
  61             if echo "${dest_folder}" | grep -q -v '^\./'; then
  62                 if echo "${dest_folder}" | grep -q -v '^/'; then
  63                     dest_folder="$(echo "${dest_folder}" | sed 's-^-./-')"
  64                 fi
  65             fi
  66 
  67             shift
  68             continue
  69         ;;
  70 
  71         -r|--r|-rel|--rel|-relative|--relative)
  72             rel_paths=1
  73             shift
  74             continue
  75         ;;
  76 
  77         -x|--x|-exec|--exec|-executable|--executable)
  78             make_exec=1
  79             shift
  80             continue
  81         ;;
  82 
  83         -) break ;;
  84 
  85         --) shift; break ;;
  86 
  87         -*)
  88             printf "unsupported option '%s'\n" "$1" >&2
  89             exit 1
  90         ;;
  91     esac
  92 
  93     break
  94 done
  95 
  96 if [ $# -eq 0 ]; then
  97     awk '/^# +shin /, /^$/ { gsub(/^# ?/, ""); print }' "$0" >&2
  98     exit 0
  99 fi
 100 
 101 printf "#!/bin/sh\n"
 102 
 103 files=0
 104 
 105 for arg in "$@"; do
 106     if [ ! -f "${arg}" ]; then
 107         printf "'%s' isn't a file\n" "${arg}" >&2
 108         exit 1
 109     fi
 110 
 111     files="$((files + 1))"
 112 
 113     if [ "${rel_paths}" -eq 1 ]; then
 114         name="$(basename "${arg}" | sed -E 's-^\.?/--')"
 115     else
 116         name="$(echo "${arg}" | sed -E 's-^\.?/--')"
 117     fi
 118 
 119     dest_path="$(printf "%s/%s" "${dest_folder}" "${name}")"
 120     folder="$(dirname "${dest_path}")"
 121 
 122     # dash doesn't support single-line `<<<` redirects
 123     printf "mkdir -p %s && { base64 -d | gzip -d; } > %s << 'EOF'\n" \
 124         "${folder}" "${dest_path}"
 125     gzip < "${arg}" | base64 -w 0
 126     printf "\nEOF\n"
 127 
 128     if [ "${make_exec}" -eq 1 ]; then
 129         printf "chmod +x %s\n" "${dest_path}"
 130     fi
 131 done
 132 
 133 if [ "${files}" -eq 0 ]; then
 134     printf "no files given\n" >&2
 135     exit 1
 136 fi