Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- #set -x # uncomment/comment to enable/disable debug mode
- # name: tomato-installer.sh
- # version: 3.0.0, 13-oct-2024, by eibgrad
- # purpose: install pastebin script into tomato router
- # function usage()
- usage() {
- echo 'Usage: tomato-installer.sh [options] pastebin-id [ext=sh]'
- echo
- echo ' Install pastebin script into tomato router.'
- echo
- echo ' Installation directory hierarchy:'
- echo ' </path> (from --dir option)'
- echo ' /jffs/etc/config'
- echo ' /opt/etc/config'
- echo ' /tmp'
- echo
- echo ' Options:'
- echo ' --dir PATH installation directory (created as necessary)'
- echo " --pre NUM add prefix to filename (e.g., '01')"
- echo ' --nocom remove blank lines and non-functional comments'
- echo ' --comp same as --nocom, plus remove leading whitespace'
- echo ' --noprompt silently overwrite files and accept defaults'
- echo ' --debug install script w/ debugging mode enabled'
- echo ' -h,--help this usage information'
- echo
- echo ' # e.g., install script w/o comments and w/ bash (sh) extension'
- echo ' tomato-installer.sh --nocom nRtRhKf3'
- echo
- }
- # function query( message [default-reply] )
- query() {
- local reply
- read -p "$1 " reply < /dev/tty
- [ "$reply" ] && echo "$reply" || echo "$2"
- }
- # function is_mounted( mounting-point )
- is_mounted() { df | grep -Eq "[[:space:]]+${1}$"; }
- # function customize()
- customize() {
- # script-specific customization (optional)
- case "$pbid" in
- *) ;;
- esac
- }
- # function exit_1( [message] )
- exit_1() { [ "$1" ] && echo "$1"; exit 1; }
- # function exit_0( [message] )
- exit_0() { [ "$1" ] && echo "$1"; exit 0; }
- # handle help/usage requests
- for opt; do case $opt in -h|--help) usage; exit 0;; esac; done
- # try curl, fallback to wget
- which curl &>/dev/null && GET_URL='curl -sLk' || GET_URL='wget -qO -'
- # process command line options/arguments
- while [ $# -gt 0 ]; do
- case $1 in
- '--dir') shift; file_dir="${1//[[:space:]]/}";;
- '--pre') shift; file_pre="${1//[[:space:]]/}";;
- '--nocom') nocom=; unset comp;;
- '--comp') comp=; unset nocom;;
- '--noprompt') noprompt=;;
- '--debug') debug=;;
- *) break 2;;
- esac
- shift
- done
- # set pastebin-id
- [ "$1" ] && pbid="$1" || exit_0 'info: nothing to do'
- # set and verify extension
- ext="$([ "$2" ] && echo "${2//[[:space:]]/}" || echo sh)"
- echo $ext | grep -q '\.' && exit_1 "error: extension cannot contain '.'"
- # construct pastebin url for retrieving raw file (obscure from hosting site)
- url="$(echo wastebin | sed s/w/p/).com/raw/$pbid"
- if [ ! "$file_dir" ]; then
- # locate storage
- if is_mounted '/jffs'; then
- file_dir='/jffs/etc/config'
- elif is_mounted '/opt'; then
- file_dir='/opt/etc/config'
- else
- file_dir='/tmp'
- echo 'warning: /jffs and /opt not mounted; using /tmp'
- fi
- fi
- # convert cryptic pastebin id to common name
- case "$1" in
- 'Yp0ptsjh') file='tomato-ovpn-client-killswitch';;
- 'MPnU5WrK') file='tomato-ovpn-client-watchdog';;
- 'UUUT8GiW') file='tomato-ovpn-remote-access';;
- 'RbdQNmay') file='tomato-ovpn-server-restrict';;
- 'tbQ1yAR0') file='tomato-ovpn-server-watchdog';;
- 'GMUbEtGj') file='tomato-ovpn-split-advanced';;
- 'xEziw8Pq') file='tomato-ovpn-split-basic';;
- 'zkDfbTEJ') file='tomato-pia-port-forward';;
- '9csaJiUc') file='tomato-routing-policy-fix-v2';;
- 'CnjxdGtU') file='tomato-wol-port-forward';;
- *) file="pastebin-$1";;
- esac
- # check for existing files
- efiles="$(echo $file_dir/*$file*)"
- if [ ! ${noprompt+x} ] && [ "$efiles" != "$file_dir/*$file*" ]; then
- efile_count=0
- for efile in $efiles; do
- echo "warning: existing file: $efile"
- let efile_count++
- done
- if [ $efile_count -gt 0 ]; then
- # obtain permission to overwrite existing file(s)
- while :; do
- case "$(query 'Overwrite existing file(s) (yes/[no])?' 'no')" in
- 'yes') break;;
- 'no') exit_0 'info: installation aborted';;
- esac
- done
- # delete existing files
- for efile in $efiles; do rm -f $efile; done
- fi
- fi
- # option pre: add prefix to filename
- [ "$file_pre" ] && file="${file_pre}-$file"
- # create directory
- if ! mkdir -p "$file_dir" 2>/dev/null; then
- exit_1 "error: cannot create directory: $file_dir"
- fi
- # construct full path + filename + extension
- file="$file_dir/$file.$ext"
- # confirm file can be created through initialization
- if ! (> "$file") 2>/dev/null; then
- exit_1 "error: cannot create file: $file"
- fi
- # retrieve raw file from pastebin
- $GET_URL $url | tr -d '\r' > "$file"; echo >> "$file"
- # verify file is bash script by locating shebang
- if ! head -n1 "$file" | grep -Eq '^#!/bin/sh($|[[:space:]]+)'; then
- rm -f "$file"
- exit_1 "error: file not found: $pbid"
- fi
- # option nocom: remove blank lines and non-functional comments
- [ ${nocom+x} ] && sed -ri '/^[[:space:]]*($|#([[:space:]]|#|$))/d' "$file"
- # option comp: same as --nocom, plus remove leading whitespace
- [ ${comp+x} ] && sed -ri 's/^[[:space:]]*//;/^($|#([[:space:]]|#|$))/d' "$file"
- # option debug: install script w/ debugging mode enabled
- [ ${debug+x} ] && sed -ri '2 s/^#(DEBUG=; )/\1/' "$file"
- # mark file executable
- chmod +x "$file"
- # check for any script-specific customization
- customize
- exit_0 "installed: $file"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement