diff --git a/install-ngxblocker b/install-ngxblocker index d7289dc1e..aaf13677a 100755 --- a/install-ngxblocker +++ b/install-ngxblocker @@ -39,22 +39,36 @@ REPO=https://raw.githubusercontent.com/mitchellkrogza/nginx-ultimate-bad-bot-blo ####### end user configuration ########################## usage() { - local script=$(basename $0) - cat </dev/null; then + printf "$msg absolute path.\n" + exit 1 + fi + ;; + url) if ! echo $arg | grep -E ^http[s]?://[0-9a-zA-Z-]+[.]+[/0-9a-zA-Z.]+ 1>/dev/null; then + printf "$msg url => http[s]://the.url\n" + exit 1 + fi + ;; + none) printf "$msg argument.\n"; exit 1;; + esac +} + get_options() { - local options=$(getopt -o b:c:r:hx --long \ - bots:,conf:,repo:,help,exec -- "$@" 2>/dev/null) + local arg= opts= - if [ $? -ne 0 ]; then - usage - exit 1 - fi + while getopts :b:c:r:xvh opts "$@" + do + if [ -n "${OPTARG}" ]; then + case "$opts" in + r) arg=$(sanitize_url ${OPTARG});; + *) arg=$(sanitize_path ${OPTARG});; + esac + fi - eval set -- "$options" - - while :; do - case "$1" in - -h | --help) usage && exit 1;; - -x | --exec) DRY_RUN=N; shift;; - -b | --bots) BOTS_DIR=$2; shift 2;; - -c | --conf) CONF_DIR=$2; shift 2;; - -r | --repo) REPO=$2; shift 2;; - *) break;; - esac - done + case "$opts" in + b) BOTS_DIR=$arg; check_args $opts path $arg ;; + c) CONF_DIR=$arg; check_args $opts path $arg ;; + r) REPO=$arg; check_args $opts url $arg ;; + x) DRY_RUN=N ;; + v) check_version ;; + h) usage ;; + \?) usage ;; + :) check_args $OPTARG none none ;; + esac + done } wget_opts() { diff --git a/setup-ngxblocker b/setup-ngxblocker index 86e341cc4..94124b5bc 100755 --- a/setup-ngxblocker +++ b/setup-ngxblocker @@ -22,21 +22,21 @@ INC_DDOS="Y" ####### end user configuration ########################### usage() { - local script=$(basename $0) - cat </dev/null; then + printf "$msg absolute path.\n" + exit 1 + fi + ;; + none) printf "$msg argument.\n"; exit 1;; + esac +} + get_options() { - local options=$(getopt -o w:e:v:b:c:m:ndhx --long \ - www:,ext:,vhost:,bots:,conf:,main:,names,ddos,help,exec -- "$@" 2>/dev/null) + local arg= opts= - if [ $? -ne 0 ]; then - usage - exit 1 - fi + while getopts :w:e:v:b:c:m:ndxh opts "$@" + do + if [ -n "${OPTARG}" ]; then + case "$opts" in + e) arg=$(sanitize_ext ${OPTARG});; + *) arg=$(sanitize_path ${OPTARG});; + esac + fi - eval set -- "$options" - - while :; do - case "$1" in - -h | --help) usage && exit 1;; - -x | --exec) DRY_RUN=N; shift;; - -w | --www) WWW=$2; shift 2;; - -e | --ext) VHOST_EXT=$2; shift 2;; - -v | --vhost) VHOST_DIR=$2; shift 2;; - -b | --bots) BOTS_DIR=$2; shift 2;; - -c | --conf) CONF_DIR=$2; shift 2;; - -m | --main) MAIN_CONF=$2; shift 2;; - -n | --names) DOT_NAMES=N; shift;; - -d | --ddos) INC_DDOS=N; shift;; - *) break;; + case "$opts" in + w) WWW=$arg; check_args $opts path $arg ;; + e) VHOST_EXT=$arg;; + v) VHOST_DIR=$arg; check_args $opts path $arg ;; + b) BOTS_DIR=$arg; check_args $opts path $arg ;; + c) CONF_DIR=$arg; check_args $opts path $arg ;; + m) MAIN_CONF=$arg; check_args $opts path $arg ;; + n) DOT_NAMES=N ;; + d) INC_DDOS=N ;; + x) DRY_RUN=N ;; + h) usage ;; + \?) usage ;; + :) check_args $OPTARG none none ;; esac done } diff --git a/update-ngxblocker b/update-ngxblocker index cc3e78896..fa62ca648 100755 --- a/update-ngxblocker +++ b/update-ngxblocker @@ -35,22 +35,36 @@ CONF_DIR=/etc/nginx/conf.d ##### end user configuration ############################################################## usage() { - local script=$(basename $0) - cat </dev/null; then + printf "$msg absolute path.\n" + exit 1 + fi + ;; + email) if ! echo $arg | grep -E ^[-_[:alnum:]]+@[-_[:alnum:]]+[\.][\.a-z]+ 1>/dev/null; then + printf "$msg email@domain.com\n" + exit 1 + fi + ;; + url) if ! echo $arg | grep -E ^http[s]?://[0-9a-zA-Z-]+[.]+[/0-9a-zA-Z.]+ 1>/dev/null; then + printf "$msg url => http[s]://the.url\n" + exit 1 + fi + ;; + none) printf "$msg argument.\n"; exit 1;; + esac +} get_options() { - local options=$(getopt -o c:r:e:nh --long \ - bots:,conf:,repo:,email:,no-email,help,exec -- "$@" 2>/dev/null) + local arg= opts= - if [ $? -ne 0 ]; then - usage - exit 1 - fi + while getopts :c:r:e:nvh opts "$@" + do + if [ -n "${OPTARG}" ]; then + case "$opts" in + r) arg=$(sanitize_url ${OPTARG});; + e) arg=$(sanitize_email ${OPTARG});; + *) arg=$(sanitize_path ${OPTARG});; + esac + fi - eval set -- "$options" - - while :; do - case "$1" in - -h | --help) usage && exit 1;; - -c | --conf) CONF_DIR=$2; shift 2;; - -r | --repo) REPO=$2; shift 2;; - -e | --email) EMAIL=$2; shift 2;; - -n | --no-email) SEND_EMAIL=N; shift 2;; - *) break;; - esac - done + case "$opts" in + c) CONF_DIR=$arg; check_args $opts path $arg ;; + r) REPO=$arg; check_args $opts url $arg ;; + e) EMAIL=$arg; check_args $opts email $arg ;; + n) SEND_EMAIL=N ;; + v) check_version ;; + h) usage ;; + \?) usage ;; + :) check_args $OPTARG none none ;; + esac + done } main() {