File: mashin.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2020-2025 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 while true; do
  63     case "$1" in
  64         -d|--d|-dest|--dest|-destination|--destination)
  65             shift
  66             if [ $# -eq 0 ]; then
  67                 printf "\e[38;2;204;0;0mmissing destination folder\e[0m\n" >&2
  68                 exit 1
  69             fi
  70 
  71             dest_folder="$(echo "$1" | sed 's-//*$--')"
  72             if echo "${dest_folder}" | grep -q -v '^\./'; then
  73                 if echo "${dest_folder}" | grep -q -v '^/'; then
  74                     dest_folder="$(echo "${dest_folder}" | sed 's-^-./-')"
  75                 fi
  76             fi
  77             shift
  78         ;;
  79 
  80         -r|--r|-rel|--rel|-relative|--relative)
  81             rel_paths=1
  82             shift
  83         ;;
  84 
  85         -x|--x|-exec|--exec|-executable|--executable)
  86             make_exec=1
  87             shift
  88         ;;
  89 
  90         --)
  91             shift
  92             break
  93         ;;
  94 
  95         *)
  96             break
  97         ;;
  98     esac
  99 done
 100 
 101 if [ $# -eq 0 ]; then
 102     awk '/^# +shin /, /^$/ { gsub(/^# ?/, ""); print }' "$0" >&2
 103     exit 0
 104 fi
 105 
 106 printf "#!/bin/sh\n"
 107 
 108 files=0
 109 
 110 for arg in "$@"; do
 111     if [ ! -f "${arg}" ]; then
 112         printf "\e[38;2;204;0;0m'%s' isn't a file\e[0m\n" "${arg}" >&2
 113         exit 1
 114     fi
 115 
 116     files="$((files + 1))"
 117 
 118     if [ "${rel_paths}" -eq 1 ]; then
 119         name="$(basename ${arg} | sed -E 's-^\.?/--')"
 120     else
 121         name="$(echo ${arg} | sed -E 's-^\.?/--')"
 122     fi
 123 
 124     dest_path="$(printf "%s/%s" "${dest_folder}" "${name}")"
 125     folder="$(dirname "${dest_path}")"
 126 
 127     # dash doesn't support single-line `<<<` redirects
 128     printf "mkdir -p %s && { base64 -d | gzip -d; } > %s << 'EOF'\n" \
 129         "${folder}" "${dest_path}"
 130     cat "${arg}" | gzip | base64 -w 0
 131     printf "\nEOF\n"
 132 
 133     if [ "${make_exec}" -eq 1 ]; then
 134         printf "chmod +x %s\n" "${dest_path}"
 135     fi
 136 done
 137 
 138 if [ "${files}" -eq 0 ]; then
 139     printf "\e[38;2;204;0;0mno files given\e[0m\n" >&2
 140     exit 1
 141 fi