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...] [filepaths...]
  27 #
  28 #
  29 # MAke SHell INstaller makes an installer which creates/updates files and
  30 # folders. The output is shell source code which uses base64-encoded gzipped
  31 # payloads.
  32 #
  33 # The options are, available both in single and double-dash versions
  34 #
  35 #   -d              change the destination folder with the next argument
  36 #   -dest           change the destination folder with the next argument
  37 #   -destination    change the destination folder with the next argument
  38 #
  39 #   -h              show this help message
  40 #   -help           show this help message
  41 #
  42 #   -r              use filepaths relative to the destination folder
  43 #   -rel            use filepaths relative to the destination folder
  44 #   -relative       use filepaths relative to the destination folder
  45 #
  46 #   -x              make installer enable the executable bit on all results
  47 #   -exec           make installer enable the executable bit on all results
  48 #   -executable     make installer enable the executable bit on all results
  49 
  50 
  51 case "$1" in
  52     -h|--h|-help|--help)
  53         awk '/^# +mashin /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  54         exit 0
  55     ;;
  56 esac
  57 
  58 rel_paths=0
  59 dest_folder='.'
  60 make_exec=0
  61 
  62 for arg in "$@"; do
  63     if [ "${arg}" = '--' ]; then
  64         shift
  65         break
  66     fi
  67 
  68     case "${arg}" in
  69         -d|--d|-dest|--dest|-destination|--destination)
  70             shift
  71             if [ $# -eq 0 ]; then
  72                 printf "\e[38;2;204;0;0mmissing destination folder\e[0m\n" >&2
  73                 exit 1
  74             fi
  75 
  76             dest_folder="$(echo "${arg}" | sed 's-//*$--')"
  77             if echo "${dest_folder}" | grep -q -v '^\./'; then
  78                 if echo "${dest_folder}" | grep -q -v '^/'; then
  79                     dest_folder="$(echo "${dest_folder}" | sed 's-^-./-')"
  80                 fi
  81             fi
  82 
  83             shift
  84             continue
  85         ;;
  86 
  87         -r|--r|-rel|--rel|-relative|--relative)
  88             rel_paths=1
  89             shift
  90             continue
  91         ;;
  92 
  93         -x|--x|-exec|--exec|-executable|--executable)
  94             make_exec=1
  95             shift
  96             continue
  97         ;;
  98     esac
  99 
 100     break
 101 done
 102 
 103 if [ $# -eq 0 ]; then
 104     awk '/^# +shin /, /^$/ { gsub(/^# ?/, ""); print }' "$0" >&2
 105     exit 0
 106 fi
 107 
 108 printf "#!/bin/sh\n"
 109 
 110 files=0
 111 
 112 for arg in "$@"; do
 113     if [ ! -f "${arg}" ]; then
 114         printf "\e[38;2;204;0;0m'%s' isn't a file\e[0m\n" "${arg}" >&2
 115         exit 1
 116     fi
 117 
 118     files="$((files + 1))"
 119 
 120     if [ "${rel_paths}" -eq 1 ]; then
 121         name="$(basename ${arg} | sed -E 's-^\.?/--')"
 122     else
 123         name="$(echo ${arg} | sed -E 's-^\.?/--')"
 124     fi
 125 
 126     dest_path="$(printf "%s/%s" "${dest_folder}" "${name}")"
 127     folder="$(dirname "${dest_path}")"
 128 
 129     # dash doesn't support single-line `<<<` redirects
 130     printf "mkdir -p %s && { base64 -d | gzip -d; } > %s << 'EOF'\n" \
 131         "${folder}" "${dest_path}"
 132     cat "${arg}" | gzip | base64 -w 0
 133     printf "\nEOF\n"
 134 
 135     if [ "${make_exec}" -eq 1 ]; then
 136         printf "chmod +x %s\n" "${dest_path}"
 137     fi
 138 done
 139 
 140 if [ "${files}" -eq 0 ]; then
 141     printf "\e[38;2;204;0;0mno files given\e[0m\n" >&2
 142     exit 1
 143 fi