mirror of
				https://github.com/billz/raspap-webgui.git
				synced 2025-03-01 10:31:47 +00:00 
			
		
		
		
	Utility functions + param handling
This commit is contained in:
		@@ -4,11 +4,12 @@
 | 
			
		||||
# Author: @billz <billzimmerman@gmail.com>
 | 
			
		||||
# License: GNU General Public License v3.0
 | 
			
		||||
#
 | 
			
		||||
# Usage:
 | 
			
		||||
# Installs an instance of RaspAP.
 | 
			
		||||
#
 | 
			
		||||
# Available options:
 | 
			
		||||
# -y, --yes, --assume-yes
 | 
			
		||||
#    Assume "yes" as answer to all prompts and run non-interactively
 | 
			
		||||
# c, --cert, --certficate
 | 
			
		||||
# -c, --cert, --certficate
 | 
			
		||||
#    Installs mkcert and generates an SSL certificate for lighttpd
 | 
			
		||||
# -o, --openvpn <flag>
 | 
			
		||||
#    Used with -y, --yes, sets OpenVPN install option (0=no install)
 | 
			
		||||
@@ -35,48 +36,65 @@
 | 
			
		||||
# You are not obligated to bundle the LICENSE file with your RaspAP projects as long
 | 
			
		||||
# as you leave these references intact in the header comments of your source files.
 | 
			
		||||
 | 
			
		||||
# Set defaults
 | 
			
		||||
repo="billz/raspap-webgui"
 | 
			
		||||
# Fetch latest release from GitHub API
 | 
			
		||||
repo="billz/raspap-webgui" #override with -r, --repo option
 | 
			
		||||
readonly RASPAP_LATEST=$(curl -s "https://api.github.com/repos/$repo/releases/latest" | grep -Po '"tag_name": "\K.*?(?=")' )
 | 
			
		||||
branch="$RASPAP_LATEST" #override with -b, --branch option
 | 
			
		||||
 | 
			
		||||
function _setup_colors() {
 | 
			
		||||
    ANSI_RED="\033[0;31m"
 | 
			
		||||
    ANSI_GREEN="\033[0;32m"
 | 
			
		||||
    ANSI_YELLOW="\033[0;33m"
 | 
			
		||||
    ANSI_RASPBERRY="\033[0;35m"
 | 
			
		||||
    ANSI_ERROR="\033[1;37;41m"
 | 
			
		||||
    ANSI_RESET="\033[m"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function _log_output() {
 | 
			
		||||
    readonly LOGFILE_PATH="/tmp"
 | 
			
		||||
    exec > >(tee -i $LOGFILE_PATH/raspap_install.log)
 | 
			
		||||
    exec 2>&1
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function _usage() {
 | 
			
		||||
    cat << EOF
 | 
			
		||||
Usage: $(basename "$0") [OPTION]
 | 
			
		||||
 | 
			
		||||
Installs an instance of RaspAP.
 | 
			
		||||
 | 
			
		||||
Available options:
 | 
			
		||||
-y, --yes, --assume-yes
 | 
			
		||||
    Assumes "yes" as an answer to all prompts
 | 
			
		||||
-c, --cert, --certificate
 | 
			
		||||
    Installs an SSL certificate for lighttpd
 | 
			
		||||
-o, --openvpn <flag>
 | 
			
		||||
    Used with -y, --yes, sets OpenVPN install option (0=no install)
 | 
			
		||||
-a, --adblock <flag>
 | 
			
		||||
    Used with -y, --yes, sets Adblock install option (0=no install)
 | 
			
		||||
-r, --repo, --repository <name>
 | 
			
		||||
    Overrides the default GitHub repo (billz/raspap-webgui)
 | 
			
		||||
-b, --branch <name>
 | 
			
		||||
    Overrides the default git branch (latest release)
 | 
			
		||||
-h, --help
 | 
			
		||||
    Outputs usage notes and exits
 | 
			
		||||
-u, --upgrade
 | 
			
		||||
    Upgrades an existing installation to the latest release version
 | 
			
		||||
-v, --version
 | 
			
		||||
    Outputs release info and exits
 | 
			
		||||
 | 
			
		||||
EOF
 | 
			
		||||
    exit
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function _parse_params() {
 | 
			
		||||
    # default flag values
 | 
			
		||||
    assume_yes=0
 | 
			
		||||
    upgrade=0
 | 
			
		||||
    ovpn_option=1
 | 
			
		||||
    adblock_option=1
 | 
			
		||||
 | 
			
		||||
# Define colors
 | 
			
		||||
readonly ANSI_RED="\033[0;31m"
 | 
			
		||||
readonly ANSI_GREEN="\033[0;32m"
 | 
			
		||||
readonly ANSI_YELLOW="\033[0;33m"
 | 
			
		||||
readonly ANSI_RASPBERRY="\033[0;35m"
 | 
			
		||||
readonly ANSI_ERROR="\033[1;37;41m"
 | 
			
		||||
readonly ANSI_RESET="\033[m"
 | 
			
		||||
 | 
			
		||||
# Log output
 | 
			
		||||
readonly LOGFILE_PATH="/tmp"
 | 
			
		||||
exec > >(tee -i $LOGFILE_PATH/raspap_install.log)
 | 
			
		||||
exec 2>&1
 | 
			
		||||
 | 
			
		||||
# Fetch latest release from GitHub API
 | 
			
		||||
readonly RASPAP_LATEST=$(curl -s "https://api.github.com/repos/$repo/releases/latest" | grep -Po '"tag_name": "\K.*?(?=")' )
 | 
			
		||||
branch="$RASPAP_LATEST" #override with the -b, --branch option
 | 
			
		||||
 | 
			
		||||
# Define usage notes
 | 
			
		||||
usage=$(cat << EOF
 | 
			
		||||
Usage: raspbian.sh [OPTION]\n
 | 
			
		||||
-y, --yes, --assume-yes\n\tAssumes "yes" as an answer to all prompts
 | 
			
		||||
-c, --cert, --certificate\n\tInstalls an SSL certificate for lighttpd
 | 
			
		||||
-o, --openvpn <flag>\n\tUsed with -y, --yes, sets OpenVPN install option (0=no install)
 | 
			
		||||
-a, --adblock <flag>\n\tUsed with -y, --yes, sets Adblock install option (0=no install)
 | 
			
		||||
-r, --repo, --repository <name>\n\tOverrides the default GitHub repo (billz/raspap-webgui)
 | 
			
		||||
-b, --branch <name>\n\tOverrides the default git branch (latest release)
 | 
			
		||||
-h, --help\n\tOutputs usage notes and exits
 | 
			
		||||
-u, --upgrade\n\tUpgrades an existing installation to the latest release version
 | 
			
		||||
-v, --version\n\tOutputs release info and exits\n
 | 
			
		||||
EOF
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
# Parse command-line options
 | 
			
		||||
    while :; do
 | 
			
		||||
    case $1 in
 | 
			
		||||
        case ${1} in
 | 
			
		||||
            -y|--yes|--assume-yes)
 | 
			
		||||
            assume_yes=1
 | 
			
		||||
            apt_option="-y"
 | 
			
		||||
@@ -101,19 +119,17 @@ while :; do
 | 
			
		||||
            shift
 | 
			
		||||
            ;;
 | 
			
		||||
            -h|--help)
 | 
			
		||||
        printf "$usage"
 | 
			
		||||
        exit 1
 | 
			
		||||
            _usage
 | 
			
		||||
            ;;
 | 
			
		||||
            -u|--upgrade)
 | 
			
		||||
            upgrade=1
 | 
			
		||||
            ;;
 | 
			
		||||
            -v|--version)
 | 
			
		||||
        printf "RaspAP v${RASPAP_LATEST} - Simple AP setup & WiFi management for Debian-based devices\n"
 | 
			
		||||
        exit 1
 | 
			
		||||
            _version
 | 
			
		||||
            ;;
 | 
			
		||||
            -*|--*)
 | 
			
		||||
            echo "Unknown option: $1"
 | 
			
		||||
        printf "$usage"
 | 
			
		||||
            _usage
 | 
			
		||||
            exit 1
 | 
			
		||||
            ;;
 | 
			
		||||
            *)
 | 
			
		||||
@@ -122,6 +138,16 @@ while :; do
 | 
			
		||||
        esac
 | 
			
		||||
        shift
 | 
			
		||||
    done
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function _version() {
 | 
			
		||||
    echo -e "RaspAP v${RASPAP_LATEST} - Simple AP setup & WiFi management for Debian-based devices"
 | 
			
		||||
    exit
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
_parse_params "$@"
 | 
			
		||||
_setup_colors
 | 
			
		||||
_log_output
 | 
			
		||||
 | 
			
		||||
UPDATE_URL="https://raw.githubusercontent.com/$repo/$branch/"
 | 
			
		||||
 | 
			
		||||
@@ -167,7 +193,7 @@ function _install_status() {
 | 
			
		||||
 | 
			
		||||
function _update_system_packages() {
 | 
			
		||||
    _install_log "Updating sources"
 | 
			
		||||
    sudo apt-get update || _install_error "Unable to update package list"
 | 
			
		||||
    sudo apt-get update || _install_status 1 "Unable to update package list"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Fetch required installer functions
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user