Advertisement
eibgrad

tomato-installer.sh

May 16th, 2019 (edited)
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.53 KB | None | 0 0
  1. #!/bin/sh
  2. #set -x # uncomment/comment to enable/disable debug mode
  3.  
  4. #     name: tomato-installer.sh
  5. #  version: 3.0.0, 13-oct-2024, by eibgrad
  6. #  purpose: install pastebin script into tomato router
  7.  
  8. # function usage()
  9. usage() {
  10.     echo 'Usage: tomato-installer.sh [options] pastebin-id [ext=sh]'
  11.     echo
  12.     echo '  Install pastebin script into tomato router.'
  13.     echo
  14.     echo '  Installation directory hierarchy:'
  15.     echo '    </path> (from --dir option)'
  16.     echo '    /jffs/etc/config'
  17.     echo '    /opt/etc/config'
  18.     echo '    /tmp'
  19.     echo
  20.     echo '  Options:'
  21.     echo '    --dir PATH  installation directory (created as necessary)'
  22.     echo "    --pre NUM   add prefix to filename (e.g., '01')"
  23.     echo '    --nocom     remove blank lines and non-functional comments'
  24.     echo '    --comp      same as --nocom, plus remove leading whitespace'
  25.     echo '    --noprompt  silently overwrite files and accept defaults'
  26.     echo '    --debug     install script w/ debugging mode enabled'
  27.     echo '    -h,--help   this usage information'
  28.     echo
  29.     echo '  # e.g., install script w/o comments and w/ bash (sh) extension'
  30.     echo '  tomato-installer.sh --nocom nRtRhKf3'
  31.     echo
  32. }
  33.  
  34. # function query( message [default-reply] )
  35. query() {
  36.     local reply
  37.  
  38.     read -p "$1 " reply < /dev/tty
  39.     [ "$reply" ] && echo "$reply" || echo "$2"
  40. }
  41.  
  42. # function is_mounted( mounting-point )
  43. is_mounted() { df | grep -Eq "[[:space:]]+${1}$"; }
  44.  
  45. # function customize()
  46. customize() {
  47.     # script-specific customization (optional)
  48.     case "$pbid" in
  49.         *) ;;
  50.     esac
  51. }
  52.  
  53. # function exit_1( [message] )
  54. exit_1() { [ "$1" ] && echo "$1"; exit 1; }
  55.  
  56. # function exit_0( [message] )
  57. exit_0() { [ "$1" ] && echo "$1"; exit 0; }
  58.  
  59. # handle help/usage requests
  60. for opt; do case $opt in -h|--help) usage; exit 0;; esac; done
  61.  
  62. # try curl, fallback to wget
  63. which curl &>/dev/null && GET_URL='curl -sLk' || GET_URL='wget -qO -'
  64.  
  65. # process command line options/arguments
  66. while [ $# -gt 0 ]; do
  67.     case $1 in
  68.              '--dir') shift; file_dir="${1//[[:space:]]/}";;
  69.              '--pre') shift; file_pre="${1//[[:space:]]/}";;
  70.            '--nocom') nocom=; unset comp;;
  71.             '--comp') comp=; unset nocom;;
  72.         '--noprompt') noprompt=;;
  73.            '--debug') debug=;;
  74.                    *) break 2;;
  75.     esac
  76.     shift
  77. done
  78.  
  79. # set pastebin-id
  80. [ "$1" ] && pbid="$1" || exit_0 'info: nothing to do'
  81.  
  82. # set and verify extension
  83. ext="$([ "$2" ] && echo "${2//[[:space:]]/}" || echo sh)"
  84. echo $ext | grep -q '\.' && exit_1 "error: extension cannot contain '.'"
  85.  
  86. # construct pastebin url for retrieving raw file (obscure from hosting site)
  87. url="$(echo wastebin | sed s/w/p/).com/raw/$pbid"
  88.  
  89. if [ ! "$file_dir" ]; then
  90.     # locate storage
  91.     if is_mounted '/jffs'; then
  92.         file_dir='/jffs/etc/config'
  93.     elif is_mounted '/opt'; then
  94.         file_dir='/opt/etc/config'
  95.     else
  96.         file_dir='/tmp'
  97.         echo 'warning: /jffs and /opt not mounted; using /tmp'
  98.     fi
  99. fi
  100.  
  101. # convert cryptic pastebin id to common name
  102. case "$1" in
  103.     'Yp0ptsjh') file='tomato-ovpn-client-killswitch';;
  104.     'MPnU5WrK') file='tomato-ovpn-client-watchdog';;
  105.     'UUUT8GiW') file='tomato-ovpn-remote-access';;
  106.     'RbdQNmay') file='tomato-ovpn-server-restrict';;
  107.     'tbQ1yAR0') file='tomato-ovpn-server-watchdog';;
  108.     'GMUbEtGj') file='tomato-ovpn-split-advanced';;
  109.     'xEziw8Pq') file='tomato-ovpn-split-basic';;
  110.     'zkDfbTEJ') file='tomato-pia-port-forward';;
  111.     '9csaJiUc') file='tomato-routing-policy-fix-v2';;
  112.     'CnjxdGtU') file='tomato-wol-port-forward';;
  113.              *) file="pastebin-$1";;
  114. esac
  115.  
  116. # check for existing files
  117. efiles="$(echo $file_dir/*$file*)"
  118.  
  119. if [ ! ${noprompt+x} ] && [ "$efiles" != "$file_dir/*$file*" ]; then
  120.     efile_count=0
  121.  
  122.     for efile in $efiles; do
  123.         echo "warning: existing file: $efile"
  124.         let efile_count++
  125.     done
  126.  
  127.     if [ $efile_count -gt 0 ]; then
  128.         # obtain permission to overwrite existing file(s)
  129.         while :; do
  130.             case "$(query 'Overwrite existing file(s) (yes/[no])?' 'no')" in
  131.                 'yes') break;;
  132.                  'no') exit_0 'info: installation aborted';;
  133.             esac
  134.         done
  135.  
  136.         # delete existing files
  137.         for efile in $efiles; do rm -f $efile; done
  138.     fi
  139. fi
  140.  
  141. # option pre: add prefix to filename
  142. [ "$file_pre" ] && file="${file_pre}-$file"
  143.  
  144. # create directory
  145. if ! mkdir -p "$file_dir" 2>/dev/null; then
  146.     exit_1 "error: cannot create directory: $file_dir"
  147. fi
  148.  
  149. # construct full path + filename + extension
  150. file="$file_dir/$file.$ext"
  151.  
  152. # confirm file can be created through initialization
  153. if ! (> "$file") 2>/dev/null; then
  154.     exit_1 "error: cannot create file: $file"
  155. fi
  156.  
  157. # retrieve raw file from pastebin
  158. $GET_URL $url | tr -d '\r' > "$file"; echo >> "$file"
  159.  
  160. # verify file is bash script by locating shebang
  161. if ! head -n1 "$file" | grep -Eq '^#!/bin/sh($|[[:space:]]+)'; then
  162.     rm -f "$file"
  163.     exit_1 "error: file not found: $pbid"
  164. fi
  165.  
  166. # option nocom: remove blank lines and non-functional comments
  167. [ ${nocom+x} ] && sed -ri '/^[[:space:]]*($|#([[:space:]]|#|$))/d' "$file"
  168.  
  169. # option comp: same as --nocom, plus remove leading whitespace
  170. [ ${comp+x}  ] && sed -ri 's/^[[:space:]]*//;/^($|#([[:space:]]|#|$))/d' "$file"
  171.  
  172. # option debug: install script w/ debugging mode enabled
  173. [ ${debug+x} ] && sed -ri '2 s/^#(DEBUG=; )/\1/' "$file"
  174.  
  175. # mark file executable
  176. chmod +x "$file"
  177.  
  178. # check for any script-specific customization
  179. customize
  180.  
  181. exit_0 "installed: $file"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement