+ PKG_CACHE=/var/lib/apt/lists/
UPDATE_PKG_CACHE="${PKG_MANAGER} update"
+ UPDATE_PKG_CACHE='apt-get update'
PKG_INSTALL="${PKG_MANAGER} --yes --no-install-recommends install"
+ PKG_INSTALL='apt-get --yes --no-install-recommends install'
PKG_COUNT="${PKG_MANAGER} -s -o Debug::NoLocking=true upgrade | grep -c ^Inst || true"
+ PKG_COUNT='apt-get -s -o Debug::NoLocking=true upgrade | grep -c ^Inst || true'
PIVPN_DEPS=(openvpn git tar wget grep iptables-persistent dnsutils expect whiptail net-tools)
+ PIVPN_DEPS=(openvpn git tar wget grep iptables-persistent dnsutils expect whiptail net-tools)
###          ###

pivpnGitUrl="https://github.com/pivpn/pivpn.git"
+ pivpnGitUrl=https://github.com/pivpn/pivpn.git
pivpnFilesDir="/etc/.pivpn"
+ pivpnFilesDir=/etc/.pivpn
easyrsaVer="3.0.6"
+ easyrsaVer=3.0.6
easyrsaRel="https://github.com/OpenVPN/easy-rsa/releases/download/v${easyrsaVer}/EasyRSA-unix-v${easyrsaVer}.tgz"
+ easyrsaRel=https://github.com/OpenVPN/easy-rsa/releases/download/v3.0.6/EasyRSA-unix-v3.0.6.tgz

# Raspbian's unattended-upgrades package downloads Debian's config, so this is the link for the proper config
UNATTUPG_RELEASE="1.9"
+ UNATTUPG_RELEASE=1.9
UNATTUPG_CONFIG="https://github.com/mvo5/unattended-upgrades/archive/${UNATTUPG_RELEASE}.tar.gz"
+ UNATTUPG_CONFIG=https://github.com/mvo5/unattended-upgrades/archive/1.9.tar.gz

# Find the rows and columns. Will default to 80x24 if it can not be detected.
screen_size=$(stty size 2>/dev/null || echo 24 80)
stty size 2>/dev/null || echo 24 80
++ stty size
+ screen_size='42 80'
rows=$(echo $screen_size | awk '{print $1}')
echo $screen_size | awk '{print $1}'
++ echo 42 80
++ awk '{print $1}'
+ rows=42
columns=$(echo $screen_size | awk '{print $2}')
echo $screen_size | awk '{print $2}'
++ echo 42 80
++ awk '{print $2}'
+ columns=80

# Divide by two so the dialogs take up half of the screen, which looks nice.
r=$(( rows / 2 ))
+ r=21
c=$(( columns / 2 ))
+ c=40
# Unless the screen is tiny
r=$(( r < 20 ? 20 : r ))
+ r=21
c=$(( c < 70 ? 70 : c ))
+ c=70

######## Undocumented Flags. Shhh ########
skipSpaceCheck=false
+ skipSpaceCheck=false
reconfigure=false
+ reconfigure=false
runUnattended=false
+ runUnattended=false

# Find IP used to route to outside world

IPv4dev=$(ip route get <IP_ADDRESS> | awk '{for(i=1;i<=NF;i++)if($i~/dev/)print $(i+1)}')
ip route get <IP_ADDRESS> | awk '{for(i=1;i<=NF;i++)if($i~/dev/)print $(i+1)}'
++ ip route get <IP_ADDRESS>
++ awk '{for(i=1;i<=NF;i++)if($i~/dev/)print $(i+1)}'
+ IPv4dev=wlan0
IPv4addr=$(ip route get <IP_ADDRESS>| awk '{print $7}')
ip route get <IP_ADDRESS>| awk '{print $7}'
++ ip route get <IP_ADDRESS>
++ awk '{print $7}'
+ IPv4addr=<IP_ADDRESS>
IPv4gw=$(ip route get <IP_ADDRESS> | awk '{print $3}')
ip route get <IP_ADDRESS> | awk '{print $3}'
++ ip route get <IP_ADDRESS>
++ awk '{print $3}'
+ IPv4gw=<IP_ADDRESS>

availableInterfaces=$(ip -o link | grep "state UP" | awk '{print $2}' | cut -d':' -f1 | cut -d'@' -f1)
ip -o link | grep "state UP" | awk '{print $2}' | cut -d':' -f1 | cut -d'@' -f1
++ ip -o link
++ grep 'state UP'
++ awk '{print $2}'
++ cut -d@ -f1
++ cut -d: -f1
+ availableInterfaces='eth0
wlan0'
dhcpcdFile=/etc/dhcpcd.conf
+ dhcpcdFile=/etc/dhcpcd.conf

# Next see if we are on a tested and supported OS
function noOS_Support() {
    whiptail --msgbox --backtitle "INVALID OS DETECTED" --title "Invalid OS" "We have not been able to detect a supported OS.
Currently this installer supports Raspbian and Debian (Jessie and Stretch), Devuan (Jessie) and Ubuntu from 14.04 (trusty) to 17.04 (zesty).
If you think you received this message in error, you can post an issue on the GitHub at https://github.com/pivpn/pivpn/issues." ${r} ${c}
    exit 1
}

function maybeOS_Support() {
    if (whiptail --backtitle "Not Supported OS" --title "Not Supported OS" --yesno "You are on an OS that we have not tested but MAY work.
Currently this installer supports Raspbian and Debian (Jessie and Stretch), Devuan (Jessie) and Ubuntu from 14.04 (trusty) to 17.04 (zesty).
Would you like to continue anyway?" ${r} ${c}) then
        echo "::: Did not detect perfectly supported OS but,"
        echo "::: Continuing installation at user's own risk..."
    else
        echo "::: Exiting due to unsupported OS"
        exit 1
    fi
}

# Compatibility
distro_check() {
    # if lsb_release command is on their system
    if hash lsb_release 2>/dev/null; then

        PLAT=$(lsb_release -si)
        OSCN=$(lsb_release -sc) # We want this to be trusty xenial or jessie

    else # else get info from os-release

        source /etc/os-release
        PLAT=$(awk '{print $1}' <<< "$NAME")
        VER="$VERSION_ID"
        declare -A VER_MAP=(["9"]="stretch" ["8"]="jessie" ["18.04"]="bionic" ["16.04"]="xenial" ["14.04"]="trusty")
        OSCN=${VER_MAP["${VER}"]}
    fi

    if [[ ${OSCN} != "bionic" ]]; then
        PIVPN_DEPS+=(dhcpcd5)
    fi

    case ${PLAT} in
        Ubuntu|Raspbian|Debian|Devuan)
        case ${OSCN} in
            trusty|xenial|jessie|stretch)
            ;;
            *)
            maybeOS_Support
            ;;
        esac
        ;;
        *)
        noOS_Support
        ;;
    esac

    echo "${PLAT}" > /tmp/DET_PLATFORM
}

####### FUNCTIONS ##########
spinner()
{
    local pid=$1
    local delay=0.50
    local spinstr='/-\|'
    while [ "$(ps a | awk '{print $1}' | grep "${pid}")" ]; do
        local temp=${spinstr#?}
        printf " [%c]  " "${spinstr}"
        local spinstr=${temp}${spinstr%"$temp"}
        sleep ${delay}
        printf "\b\b\b\b\b\b"
    done
    printf "    \b\b\b\b"
}

welcomeDialogs() {
    # Display the welcome dialog
    whiptail --msgbox --backtitle "Welcome" --title "PiVPN Automated Installer" "This installer will transform your Raspberry Pi into an OpenVPN server!" ${r} ${c}

    # Explain the need for a static address
    whiptail --msgbox --backtitle "Initiating network interface" --title "Static IP Needed" "The PiVPN is a SERVER so it needs a STATIC IP ADDRESS to function properly.

In the next section, you can choose to use your current network settings (DHCP) or to manually edit them." ${r} ${c}
}

chooseUser() {
    # Explain the local user
    whiptail --msgbox --backtitle "Parsing User List" --title "Local Users" "Choose a local user that will hold your ovpn configurations." ${r} ${c}
    # First, let's check if there is a user available.
    numUsers=$(awk -F':' 'BEGIN {count=0} $3>=1000 && $3<=60000 { count++ } END{ print count }' /etc/passwd)
    if [ "$numUsers" -eq 0 ]
    then
        # We don't have a user, let's ask to add one.
        if userToAdd=$(whiptail --title "Choose A User" --inputbox "No non-root user account was found. Please type a new username." ${r} ${c} 3>&1 1>&2 2>&3)
        then
            # See http://askubuntu.com/a/667842/459815
            PASSWORD=$(whiptail  --title "password dialog" --passwordbox "Please enter the new user password" ${r} ${c} 3>&1 1>&2 2>&3)
            CRYPT=$(perl -e 'printf("%s\n", crypt($ARGV[0], "password"))' "${PASSWORD}")
            $SUDO useradd -m -p "${CRYPT}" -s /bin/bash "${userToAdd}"
            if [[ $? = 0 ]]; then
                echo "Succeeded"
                ((numUsers+=1))
            else
                exit 1
            fi
        else
            exit 1
        fi
    fi
    availableUsers=$(awk -F':' '$3>=1000 && $3<=60000 {print $1}' /etc/passwd)
    local userArray=()
    local firstloop=1

    while read -r line
    do
        mode="OFF"
        if [[ $firstloop -eq 1 ]]; then
            firstloop=0
            mode="ON"
        fi
        userArray+=("${line}" "" "${mode}")
    done <<< "${availableUsers}"
    chooseUserCmd=(whiptail --title "Choose A User" --separate-output --radiolist "Choose (press space to select):" ${r} ${c} ${numUsers})
    chooseUserOptions=$("${chooseUserCmd[@]}" "${userArray[@]}" 2>&1 >/dev/tty)
    if [[ $? = 0 ]]; then
        for desiredUser in ${chooseUserOptions}; do
            pivpnUser=${desiredUser}
            echo "::: Using User: $pivpnUser"
            echo "${pivpnUser}" > /tmp/pivpnUSR
        done
    else
        echo "::: Cancel selected, exiting...."
        exit 1
    fi
}

verifyFreeDiskSpace() {
    # If user installs unattended-upgrades we'd need about 60MB so will check for 75MB free
    echo "::: Verifying free disk space..."
    local required_free_kilobytes=76800
    local existing_free_kilobytes=$(df -Pk | grep -m1 '\/$' | awk '{print $4}')

    # - Unknown free disk space , not a integer
    if ! [[ "${existing_free_kilobytes}" =~ ^([0-9])+$ ]]; then
        echo "::: Unknown free disk space!"
        echo "::: We were unable to determine available free disk space on this system."
        echo "::: You may continue with the installation, however, it is not recommended."
        read -r -p "::: If you are sure you want to continue, type YES and press enter :: " response
        case $response in
            [Y][E][S])
                ;;
            *)
                echo "::: Confirmation not received, exiting..."
                exit 1
                ;;
        esac
    # - Insufficient free disk space
    elif [[ ${existing_free_kilobytes} -lt ${required_free_kilobytes} ]]; then
        echo "::: Insufficient Disk Space!"
        echo "::: Your system appears to be low on disk space. PiVPN recommends a minimum of $required_free_kilobytes KiloBytes."
        echo "::: You only have ${existing_free_kilobytes} KiloBytes free."
        echo "::: If this is a new install on a Raspberry Pi you may need to expand your disk."
        echo "::: Try running 'sudo raspi-config', and choose the 'expand file system option'"
        echo "::: After rebooting, run this installation again. (curl -L https://install.pivpn.io | bash)"

        echo "Insufficient free space, exiting..."
        exit 1
    fi
}

chooseInterface() {
    # Turn the available interfaces into an array so it can be used with a whiptail dialog
    local interfacesArray=()
    # Number of available interfaces
    local interfaceCount
    # Whiptail variable storage
    local chooseInterfaceCmd
    # Temporary Whiptail options storage
    local chooseInterfaceOptions
    # Loop sentinel variable
    local firstloop=1

    if [[ $(echo "${availableInterfaces}" | wc -l) -eq 1 ]]; then
      pivpnInterface="${availableInterfaces}"
      echo "${pivpnInterface}" > /tmp/pivpnINT
      return
    fi

    while read -r line; do
        mode="OFF"
        if [[ ${firstloop} -eq 1 ]]; then
            firstloop=0
            mode="ON"
        fi
        interfacesArray+=("${line}" "available" "${mode}")
    done <<< "${availableInterfaces}"

    # Find out how many interfaces are available to choose from
    interfaceCount=$(echo "${availableInterfaces}" | wc -l)
    chooseInterfaceCmd=(whiptail --separate-output --radiolist "Choose An Interface (press space to select):" ${r} ${c} ${interfaceCount})
    chooseInterfaceOptions=$("${chooseInterfaceCmd[@]}" "${interfacesArray[@]}" 2>&1 >/dev/tty)
    if [[ $? = 0 ]]; then
        for desiredInterface in ${chooseInterfaceOptions}; do
            pivpnInterface=${desiredInterface}
            echo "::: Using interface: $pivpnInterface"
            echo "${pivpnInterface}" > /tmp/pivpnINT
        done
    else
        echo "::: Cancel selected, exiting...."
        exit 1
    fi
}

avoidStaticIPv4Ubuntu() {
    # If we are in Ubuntu then they need to have previously set their network, so just use what you have.
    whiptail --msgbox --backtitle "IP Information" --title "IP Information" "Since we think you are not using Raspbian, we will not configure a static IP for you.
If you are in Amazon then you can not configure a static IP anyway. Just ensure before this installer started you had set an elastic IP on your instance." ${r} ${c}
}

getStaticIPv4Settings() {
    local ipSettingsCorrect
    # Grab their current DNS Server
    IPv4dns=$(nslookup <IP_ADDRESS> | grep Server: | awk '{print $2}')
    # Ask if the user wants to use DHCP settings as their static IP
    if (whiptail --backtitle "Calibrating network interface" --title "Static IP Address" --yesno "Do you want to use your current network settings as a static address?
                    IP address:    ${IPv4addr}
                    Gateway:       ${IPv4gw}" ${r} ${c}); then
        # If they choose yes, let the user know that the IP address will not be available via DHCP and may cause a conflict.
        whiptail --msgbox --backtitle "IP information" --title "FYI: IP Conflict" "It is possible your router could still try to assign this IP to a device, which would cause a conflict.  But in most cases the router is smart enough to not do that.
If you are worried, either manually set the address, or modify the DHCP reservation pool so it does not include the IP you want.
It is also possible to use a DHCP reservation, but if you are going to do that, you might as well set a static address." ${r} ${c}
        # Nothing else to do since the variables are already set above
    else
        # Otherwise, we need to ask the user to input their desired settings.
        # Start by getting the IPv4 address (pre-filling it with info gathered from DHCP)
        # Start a loop to let the user enter their information with the chance to go back and edit it if necessary
        until [[ ${ipSettingsCorrect} = True ]]; do
            # Ask for the IPv4 address
            IPv4addr=$(whiptail --backtitle "Calibrating network interface" --title "IPv4 address" --inputbox "Enter your desired IPv4 address" ${r} ${c} "${IPv4addr}" 3>&1 1>&2 2>&3)
            if [[ $? = 0 ]]; then
            echo "::: Your static IPv4 address:    ${IPv4addr}"
            # Ask for the gateway
            IPv4gw=$(whiptail --backtitle "Calibrating network interface" --title "IPv4 gateway (router)" --inputbox "Enter your desired IPv4 default gateway" ${r} ${c} "${IPv4gw}" 3>&1 1>&2 2>&3)
            if [[ $? = 0 ]]; then
                echo "::: Your static IPv4 gateway:    ${IPv4gw}"
                # Give the user a chance to review their settings before moving on
                if (whiptail --backtitle "Calibrating network interface" --title "Static IP Address" --yesno "Are these settings correct?
                    IP address:    ${IPv4addr}
                    Gateway:       ${IPv4gw}" ${r} ${c}); then
                    # If the settings are correct, then we need to set the pivpnIP
                    echo "${IPv4addr%/*}" > /tmp/pivpnIP
                    echo "$pivpnInterface" > /tmp/pivpnINT
                    # After that's done, the loop ends and we move on
                    ipSettingsCorrect=True
                else
                    # If the settings are wrong, the loop continues
                    ipSettingsCorrect=False
                fi
            else
                # Cancelling gateway settings window
                ipSettingsCorrect=False
                echo "::: Cancel selected. Exiting..."
                exit 1
            fi
        else
            # Cancelling IPv4 settings window
            ipSettingsCorrect=False
            echo "::: Cancel selected. Exiting..."
            exit 1
        fi
        done
        # End the if statement for DHCP vs. static
    fi
}

setDHCPCD() {
    # Append these lines to dhcpcd.conf to enable a static IP
    echo "interface ${pivpnInterface}
    static ip_address=${IPv4addr}
    static routers=${IPv4gw}
    static domain_name_servers=${IPv4dns}" | $SUDO tee -a ${dhcpcdFile} >/dev/null
}

setStaticIPv4() {
    # Tries to set the IPv4 address
    if [[ -f /etc/dhcpcd.conf ]]; then
        if grep -q "${IPv4addr}" ${dhcpcdFile}; then
            echo "::: Static IP already configured."
        else
            setDHCPCD
            $SUDO ip addr replace dev "${pivpnInterface}" "${IPv4addr}"
            echo ":::"
            echo "::: Setting IP to ${IPv4addr}.  You may need to restart after the install is complete."
            echo ":::"
        fi
    else
        echo "::: Critical: Unable to locate configuration file to set static IPv4 address!"
        exit 1
    fi
}

function valid_ip()
{
    local  ip=$1
    local  stat=1

    if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
        OIFS=$IFS
        IFS='.'
        ip=($ip)
        IFS=$OIFS
        [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
        && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
        stat=$?
    fi
    return $stat
}

#Call this function to use a regex to check user input for a valid custom domain
function valid_domain()
{
  local domain=$1
  local stat=1

  if [[ $domain =~ ^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}\.[a-zA-Z]{2,}$ ]]; then
    stat=$?
  fi
  return $stat
}

installScripts() {
    # Install the scripts from /etc/.pivpn to their various locations
    $SUDO echo ":::"
    $SUDO echo -n "::: Installing scripts to /opt/pivpn..."
    if [ ! -d /opt/pivpn ]; then
        $SUDO mkdir /opt/pivpn
        $SUDO chown "$pivpnUser":root /opt/pivpn
        $SUDO chmod u+srwx /opt/pivpn
    fi
    $SUDO cp /etc/.pivpn/scripts/makeOVPN.sh /opt/pivpn/makeOVPN.sh
    $SUDO cp /etc/.pivpn/scripts/clientStat.sh /opt/pivpn/clientStat.sh
    $SUDO cp /etc/.pivpn/scripts/listOVPN.sh /opt/pivpn/listOVPN.sh
    $SUDO cp /etc/.pivpn/scripts/removeOVPN.sh /opt/pivpn/removeOVPN.sh
    $SUDO cp /etc/.pivpn/scripts/uninstall.sh /opt/pivpn/uninstall.sh
    $SUDO cp /etc/.pivpn/scripts/pivpnDebug.sh /opt/pivpn/pivpnDebug.sh
    $SUDO chmod 0755 /opt/pivpn/{makeOVPN,clientStat,listOVPN,removeOVPN,uninstall,pivpnDebug}.sh
    $SUDO cp /etc/.pivpn/pivpn /usr/local/bin/pivpn
    $SUDO chmod 0755 /usr/local/bin/pivpn
    $SUDO cp /etc/.pivpn/scripts/bash-completion /etc/bash_completion.d/pivpn
    . /etc/bash_completion.d/pivpn
    # Copy interface setting for debug
    $SUDO cp /tmp/pivpnINT /etc/pivpn/pivpnINTERFACE

    $SUDO echo " done."
}

package_check_install() {
    dpkg-query -W -f='${Status}' "${1}" 2>/dev/null | grep -c "ok installed" || ${PKG_INSTALL} "${1}"
}

addSoftwareRepo() {
  # Add the official OpenVPN repo for distros that don't have the latest version in their default repos
  case ${PLAT} in
    Ubuntu|Debian|Devuan)
      case ${OSCN} in
        trusty|xenial|wheezy|jessie)
          wget -qO- https://swupdate.openvpn.net/repos/repo-public.gpg | $SUDO apt-key add -
          echo "deb http://build.openvpn.net/debian/openvpn/stable $OSCN main" | $SUDO tee /etc/apt/sources.list.d/swupdate.openvpn.net.list > /dev/null
          echo -n "::: Adding OpenVPN repo for $PLAT $OSCN ..."
          $SUDO apt-get -qq update & spinner $!
          echo " done!"
          ;;
      esac
      ;;
  esac
}

update_package_cache() {
  #Running apt-get update/upgrade with minimal output can cause some issues with
  #requiring user input

  #Check to see if apt-get update has already been run today
  #it needs to have been run at least once on new installs!
  timestamp=$(stat -c %Y ${PKG_CACHE})
  timestampAsDate=$(date -d @"${timestamp}" "+%b %e")
  today=$(date "+%b %e")

  if [ ! "${today}" == "${timestampAsDate}" ]; then
    #update package lists
    echo ":::"
    echo -n "::: ${PKG_MANAGER} update has not been run today. Running now..."
    $SUDO ${UPDATE_PKG_CACHE} &> /dev/null
    echo " done!"
  fi
}

notify_package_updates_available() {
  # Let user know if they have outdated packages on their system and
  # advise them to run a package update at soonest possible.
  echo ":::"
  echo -n "::: Checking ${PKG_MANAGER} for upgraded packages...."
  updatesToInstall=$(eval "${PKG_COUNT}")
  echo " done!"
  echo ":::"
  if [[ ${updatesToInstall} -eq "0" ]]; then
    echo "::: Your system is up to date! Continuing with PiVPN installation..."
  else
    echo "::: There are ${updatesToInstall} updates available for your system!"
    echo "::: We recommend you update your OS after installing PiVPN! "
    echo ":::"
  fi
}

install_dependent_packages() {
    # Install packages passed in via argument array
    # No spinner - conflicts with set -e
    declare -a argArray1=("${!1}")

    echo iptables-persistent iptables-persistent/autosave_v4 boolean true | $SUDO debconf-set-selections
    echo iptables-persistent iptables-persistent/autosave_v6 boolean false | $SUDO debconf-set-selections

    # Add support for https repositories if there are any that use it otherwise the installation will silently fail
    if grep -q https /etc/apt/sources.list; then
        PIVPN_DEPS+=("apt-transport-https")
    fi

    if command -v debconf-apt-progress &> /dev/null; then
        $SUDO debconf-apt-progress -- ${PKG_INSTALL} "${argArray1[@]}"
    else
        for i in "${argArray1[@]}"; do
            echo -n ":::    Checking for $i..."
            $SUDO package_check_install "${i}" &> /dev/null
            echo " installed!"
        done
    fi
}

unattendedUpgrades() {
    whiptail --msgbox --backtitle "Security Updates" --title "Unattended Upgrades" "Since this server will have at least one port open to the internet, it is recommended you enable unattended-upgrades.\nThis feature will check daily for security package updates only and apply them when necessary.\nIt will NOT automatically reboot the server so to fully apply some updates you should periodically reboot." ${r} ${c}

    if (whiptail --backtitle "Security Updates" --title "Unattended Upgrades" --yesno "Do you want to enable unattended upgrades of security patches to this server?" ${r} ${c}) then
        UNATTUPG="unattended-upgrades"
    else
        UNATTUPG=""
    fi
}

stopServices() {
    # Stop openvpn
    $SUDO echo ":::"
    $SUDO echo -n "::: Stopping OpenVPN service..."
    case ${PLAT} in
        Ubuntu|Debian|*vuan)
            $SUDO service openvpn stop || true
            ;;
        *)
            $SUDO systemctl stop openvpn.service || true
            ;;
    esac
    $SUDO echo " done."
}

getGitFiles() {
    # Setup git repos for base files
    echo ":::"
    echo "::: Checking for existing base files..."
    if is_repo "${1}"; then
        update_repo "${1}"
    else
        make_repo "${1}" "${2}"
    fi
}

is_repo() {
    # If the directory does not have a .git folder it is not a repo
    echo -n ":::    Checking $1 is a repo..."
    cd "${1}" &> /dev/null || return 1
    $SUDO git status &> /dev/null && echo " OK!"; return 0 || echo " not found!"; return 1
}

make_repo() {
    # Remove the non-repos interface and clone the interface
    echo -n ":::    Cloning $2 into $1..."
    $SUDO rm -rf "${1}"
    $SUDO git clone -q --depth 1 --no-single-branch "${2}" "${1}" > /dev/null & spinner $!
    if [ -z "${TESTING+x}" ]; then
        :
    else
        $SUDO git -C "${1}" checkout test
    fi
    echo " done!"
}

update_repo() {
    if [[ "${reconfigure}" == true ]]; then
          echo "::: --reconfigure passed to install script. Not downloading/updating local repos"
    else
        # Pull the latest commits
        echo -n ":::     Updating repo in $1..."
        $SUDO rm -rf "${1}"
        $SUDO git clone -q --depth 1 --no-single-branch "${2}" "${1}" > /dev/null & spinner $!
        cd "${1}" || exit 1
        if [ -z "${TESTING+x}" ]; then
            :
        else
            ${SUDOE} git checkout test
        fi
        echo " done!"
    fi
}

setCustomProto() {
  # Set the available protocols into an array so it can be used with a whiptail dialog
  if protocol=$(whiptail --title "Protocol" --radiolist \
  "Choose a protocol (press space to select). Please only choose TCP if you know why you need TCP." ${r} ${c} 2 \
  "UDP" "" ON \
  "TCP" "" OFF 3>&1 1>&2 2>&3)
  then
      # Convert option into lowercase (UDP->udp)
      pivpnProto="${protocol,,}"
      echo "::: Using protocol: $pivpnProto"
      echo "${pivpnProto}" > /tmp/pivpnPROTO
  else
      echo "::: Cancel selected, exiting...."
      exit 1
  fi
    # write out the PROTO
    PROTO=$pivpnProto
    $SUDO cp /tmp/pivpnPROTO /etc/pivpn/INSTALL_PROTO
}

setCustomPort() {
    until [[ $PORTNumCorrect = True ]]
        do
            portInvalid="Invalid"

            PROTO=$(cat /etc/pivpn/INSTALL_PROTO)
            if [ "$PROTO" = "udp" ]; then
              DEFAULT_PORT=1194
            else
              DEFAULT_PORT=443
            fi
            if PORT=$(whiptail --title "Default OpenVPN Port" --inputbox "You can modify the default OpenVPN port. \nEnter a new value or hit 'Enter' to retain the default" ${r} ${c} $DEFAULT_PORT 3>&1 1>&2 2>&3)
            then
                if [[ "$PORT" =~ ^[0-9]+$ ]] && [ "$PORT" -ge 1 ] && [ "$PORT" -le 65535 ]; then
                    :
                else
                    PORT=$portInvalid
                fi
            else
                echo "::: Cancel selected, exiting...."
                exit 1
            fi

            if [[ $PORT == "$portInvalid" ]]; then
                whiptail --msgbox --backtitle "Invalid Port" --title "Invalid Port" "You entered an invalid Port number.\n    Please enter a number from 1 - 65535.\n    If you are not sure, please just keep the default." ${r} ${c}
                PORTNumCorrect=False
            else
                if (whiptail --backtitle "Specify Custom Port" --title "Confirm Custom Port Number" --yesno "Are these settings correct?\n    PORT:   $PORT" ${r} ${c}) then
                    PORTNumCorrect=True
                else
                    # If the settings are wrong, the loop continues
                    PORTNumCorrect=False
                fi
            fi
        done
    # write out the port
    echo ${PORT} > /tmp/INSTALL_PORT
    $SUDO cp /tmp/INSTALL_PORT /etc/pivpn/INSTALL_PORT
}

setClientDNS() {
    DNSChoseCmd=(whiptail --separate-output --radiolist "Select the DNS Provider for your VPN Clients (press space to select). To use your own, select Custom."
        declare -A DNS_MAP=(["Google"]="<IP_ADDRESS> <IP_ADDRESS>" ["OpenDNS"]="<IP_ADDRESS> <IP_ADDRESS>" ["Level3"]="<IP_ADDRESS> <IP_ADDRESS>" ["DNS.WATCH"]="<IP_ADDRESS> <IP_ADDRESS>" ["Norton"]="<IP_ADDRESS> <IP_ADDRESS>" ["FamilyShield"]="<IP_ADDRESS> <IP_ADDRESS>" ["CloudFlare"]="<IP_ADDRESS> <IP_ADDRESS>")

        OVPNDNS1=$(awk '{print $1}' <<< "${DNS_MAP["${DNSchoices}"]}")
        OVPNDNS2=$(awk '{print $2}' <<< "${DNS_MAP["${DNSchoices}"]}")

        $SUDO sed -i '0,/\(dhcp-option DNS \)/ s/\(dhcp-option DNS \).*/\1'${OVPNDNS1}'\"/' /etc/openvpn/server.conf
        $SUDO sed -i '0,/\(dhcp-option DNS \)/! s/\(dhcp-option DNS \).*/\1'${OVPNDNS2}'\"/' /etc/openvpn/server.conf

      else

          until [[ $DNSSettingsCorrect = True ]]
          do
              strInvalid="Invalid"

              if OVPNDNS=$(whiptail --backtitle "Specify Upstream DNS Provider(s)"  --inputbox "Enter your desired upstream DNS provider(s), seperated by a comma.\n\nFor example '<IP_ADDRESS>, <IP_ADDRESS>'" ${r} ${c} "" 3>&1 1>&2 2>&3)
              then
                    OVPNDNS1=$(echo "$OVPNDNS" | sed 's/[, \t]\+/,/g' | awk -F, '{print$1}')
                    OVPNDNS2=$(echo "$OVPNDNS" | sed 's/[, \t]\+/,/g' | awk -F, '{print$2}')
                    if ! valid_ip "$OVPNDNS1" || [ ! "$OVPNDNS1" ]; then
                        OVPNDNS1=$strInvalid
                    fi
                    if ! valid_ip "$OVPNDNS2" && [ "$OVPNDNS2" ]; then
                        OVPNDNS2=$strInvalid
                    fi
              else
                    echo "::: Cancel selected, exiting...."
                    exit 1
                fi
              if [[ $OVPNDNS1 == "$strInvalid" ]] || [[ $OVPNDNS2 == "$strInvalid" ]]; then
                    whiptail --msgbox --backtitle "Invalid IP" --title "Invalid IP" "One or both entered IP addresses were invalid. Please try again.\n\n    DNS Server 1:   $OVPNDNS1\n    DNS Server 2:   $OVPNDNS2" ${r} ${c}
                    if [[ $OVPNDNS1 == "$strInvalid" ]]; then
                        OVPNDNS1=""
                    fi
                    if [[ $OVPNDNS2 == "$strInvalid" ]]; then
                        OVPNDNS2=""
                    fi
                    DNSSettingsCorrect=False
              else
                    if (whiptail --backtitle "Specify Upstream DNS Provider(s)" --title "Upstream DNS Provider(s)" --yesno "Are these settings correct?\n    DNS Server 1:   $OVPNDNS1\n    DNS Server 2:   $OVPNDNS2" ${r} ${c}) then
                        DNSSettingsCorrect=True
                        $SUDO sed -i '0,/\(dhcp-option DNS \)/ s/\(dhcp-option DNS \).*/\1'${OVPNDNS1}'\"/' /etc/openvpn/server.conf
                        if [ -z ${OVPNDNS2} ]; then
                            $SUDO sed -i '/\(dhcp-option DNS \)/{n;N;d}' /etc/openvpn/server.conf
                        else
                            $SUDO sed -i '0,/\(dhcp-option DNS \)/! s/\(dhcp-option DNS \).*/\1'${OVPNDNS2}'\"/' /etc/openvpn/server.conf
                        fi
                    else
                        # If the settings are wrong, the loop continues
                        DNSSettingsCorrect=False
                    fi
                fi
          done
      fi

    else
      echo "::: Cancel selected. Exiting..."
      exit 1
    fi
}

#This procedure allows a user to specify a custom search domain if they have one.
setCustomDomain() {
  DomainSettingsCorrect=False

  if (whiptail --backtitle "Custom Search Domain" --title "Custom Search Domain" --yesno "Would you like to add a custom search domain? \n (This is only for advanced users who have their own domain)\n" ${r} ${c}); then

    until [[ $DomainSettingsCorrect = True ]]
    do
      if CUSTOMDomain=$(whiptail --inputbox "Enter Custom Domain\nFormat: mydomain.com" ${r} ${c} --title "Custom Domain" 3>&1 1>&2 2>&3); then
          if valid_domain "$CUSTOMDomain"; then
            if (whiptail --backtitle "Custom Search Domain" --title "Custom Search Domain" --yesno "Are these settings correct?\n    Custom Search Domain: $CUSTOMDomain" ${r} ${c}); then
                DomainSettingsCorrect=True

                $SUDO sed -i '0,/\(.*dhcp-option.*\)/s//\push "dhcp-option DOMAIN '${CUSTOMDomain}'" \n&/' /etc/openvpn/server.conf

            else
                # If the settings are wrong, the loop continues
                DomainSettingsCorrect=False
            fi
          else
            whiptail --msgbox --backtitle "Invalid Domain" --title "Invalid Domain" "Domain is invalid. Please try again.\n\n    DOMAIN:   $CUSTOMDomain\n" ${r} ${c}
            DomainSettingsCorrect=False
          fi
      else
        echo "::: Cancel selected. Exiting..."
        exit 1
      fi
    done

  else
    echo sleep 0.1
  fi
}

confOpenVPN() {
    # Generate a random, alphanumeric identifier of 16 characters for this server so that we can use verify-x509-name later that is unique for this server installation. Source: Earthgecko (https://gist.github.com/earthgecko/3089509)
    NEW_UUID=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1)
    SERVER_NAME="server_${NEW_UUID}"

    if [[ ${useUpdateVars} == false ]]; then
        # Ask user for desired level of encryption

        if [[ ${useUpdateVars} == false ]]; then
            if [[ ${PLAT} == "Raspbian" ]] && [[ ${OSCN} != "stretch" ]]; then
                APPLY_TWO_POINT_FOUR=false
            else
                if (whiptail --backtitle "Setup OpenVPN" --title "Installation mode" --yesno "OpenVPN 2.4 brings support for stronger authentication and key exchange using Elliptic Curves, along with encrypted control channel.\n\nIf your clients do run OpenVPN 2.4 or later you can enable these features, otherwise choose 'No' for best compatibility.\n\nNOTE: Current mobile app, that is OpenVPN connect, is supported." ${r} ${c}); then
                    APPLY_TWO_POINT_FOUR=true
                    $SUDO touch /etc/pivpn/TWO_POINT_FOUR
                else
                    APPLY_TWO_POINT_FOUR=false
                fi
            fi
        fi

        if [[ ${runUnattended} == true ]] && [[ ${APPLY_TWO_POINT_FOUR} == true ]]; then
            $SUDO touch /etc/pivpn/TWO_POINT_FOUR
        fi

        if [[ ${APPLY_TWO_POINT_FOUR} == false ]]; then

            ENCRYPT=$(whiptail --backtitle "Setup OpenVPN" --title "RSA certificate size" --radiolist \
            "Choose the desired size of your certificate (press space to select):\n   This is a certificate that will be generated on your system.  The larger the certificate, the more time this will take.  For most applications, it is recommended to use 2048 bits.  If you are testing, you can use 1024 bits to speed things up, but do not use this for normal use!  If you are paranoid about ... things... then grab a cup of joe and pick 4096 bits."   The larger the certificate, the more time this will take.  For most applications, it is recommended to use 256 bits.  You can increase the number of bits if you care about, however, consider that 256 bits are already as secure as 3072 bit RSA."  Exiting..."
            exit 1
        fi
    fi

    # If easy-rsa exists, remove it
    if [[ -d /etc/openvpn/easy-rsa/ ]]; then
        $SUDO rm -rf /etc/openvpn/easy-rsa/
    fi

    # Get easy-rsa
    wget -q -O - "${easyrsaRel}" | $SUDO tar xz -C /etc/openvpn && $SUDO mv /etc/openvpn/EasyRSA-v${easyrsaVer} /etc/openvpn/easy-rsa
    # fix ownership
    $SUDO chown -R root:root /etc/openvpn/easy-rsa
    $SUDO mkdir /etc/openvpn/easy-rsa/pki

    cd /etc/openvpn/easy-rsa || exit

    # Write out new vars file
    set +e
    IFS= read -d '' String <<"EOF"
if [ -z "$EASYRSA_CALLER" ]; then
    echo "Nope." >&2
    return 1
fi
set_var EASYRSA            "/etc/openvpn/easy-rsa"
set_var EASYRSA_PKI        "$EASYRSA/pki"
set_var EASYRSA_CRL_DAYS   3650
EOF
    echo "${String}" | $SUDO tee vars >/dev/null
    set -e

    # Set certificate type
    if [[ ${APPLY_TWO_POINT_FOUR} == false ]]; then
        echo "set_var EASYRSA_ALGO       rsa" | $SUDO tee -a vars
        echo "set_var EASYRSA_KEY_SIZE   ${ENCRYPT}" | $SUDO tee -a vars
    else
        echo "set_var EASYRSA_ALGO       ec" | $SUDO tee -a vars
        echo "set_var EASYRSA_CURVE      ${ECDSA_MAP["${ENCRYPT}"]}" | $SUDO tee -a vars
    fi

    # Remove any previous keys
    ${SUDOE} ./easyrsa --batch init-pki

    # Build the certificate authority
    printf "::: Building CA...\n"
    ${SUDOE} ./easyrsa --batch build-ca nopass
    printf "\n::: CA Complete.\n"

    if [[ ${useUpdateVars} == false ]]; then
        if [[ ${APPLY_TWO_POINT_FOUR} == false ]]; then
            whiptail --msgbox --backtitle "Setup OpenVPN" --title "Server Information" "The server key, Diffie-Hellman parameters, and HMAC key will now be generated." ${r} ${c}
        fi
    fi

    # Build the server
    ${SUDOE} ./easyrsa build-server-full ${SERVER_NAME} nopass

    if [[ ${useUpdateVars} == false ]]; then
      if [[ ${APPLY_TWO_POINT_FOUR} == false ]]; then
        if ([ "$ENCRYPT" -ge "4096" ] && whiptail --backtitle "Setup OpenVPN" --title "Download Diffie-Hellman Parameters" --yesno --defaultno "Download Diffie-Hellman parameters from a public DH parameter generation service?\n\nGenerating DH parameters for a $ENCRYPT-bit key can take many hours on a Raspberry Pi. You can instead download DH parameters from \"2 Ton Digital\" that are generated at regular intervals as part of a public service. Downloaded DH parameters will be randomly selected from their database.\nMore information about this service can be found here: https://2ton.com.au/safeprimes/\n\nIf you're paranoid, choose 'No' and Diffie-Hellman parameters will be generated on your device." ${r} ${c}); then
          DOWNLOAD_DH_PARAM=true
        else
          DOWNLOAD_DH_PARAM=false
        fi
      else
        DOWNLOAD_DH_PARAM=false
      fi
    fi

    if [[ ${APPLY_TWO_POINT_FOUR} == false ]]; then
      if [ "$ENCRYPT" -ge "4096" ] && [[ ${DOWNLOAD_DH_PARAM} == true ]]; then
        # Downloading parameters
        ${SUDOE} curl "https://2ton.com.au/getprimes/random/dhparam/${ENCRYPT}" -o "/etc/openvpn/easy-rsa/pki/dh${ENCRYPT}.pem"
      else
        # Generate Diffie-Hellman key exchange
        ${SUDOE} ./easyrsa gen-dh
        ${SUDOE} mv pki/dh.pem pki/dh${ENCRYPT}.pem
      fi
    fi

    # Generate static HMAC key to defend against DDoS
    ${SUDOE} openvpn --genkey --secret pki/ta.key

    # Generate an empty Certificate Revocation List
    ${SUDOE} ./easyrsa gen-crl
    ${SUDOE} cp pki/crl.pem /etc/openvpn/crl.pem
    ${SUDOE} chown nobody:nogroup /etc/openvpn/crl.pem

    # Write config file for server using the template.txt file
    $SUDO cp /etc/.pivpn/server_config.txt /etc/openvpn/server.conf

    if [[ ${APPLY_TWO_POINT_FOUR} == true ]]; then
      #If they enabled 2.4 use tls-crypt instead of tls-auth to encrypt control channel
      $SUDO sed -i "s/tls-auth \/etc\/openvpn\/easy-rsa\/pki\/ta.key 0/tls-crypt \/etc\/openvpn\/easy-rsa\/pki\/ta.key/" /etc/openvpn/server.conf
    fi

    if [[ ${APPLY_TWO_POINT_FOUR} == true ]]; then
      #If they enabled 2.4 disable dh parameters since the key exchange will use the matching curve from the ECDSA certificate
      $SUDO sed -i "s/\(dh \/etc\/openvpn\/easy-rsa\/pki\/dh\).*/dh none/" /etc/openvpn/server.conf
    else
      # Otherwise set the user encryption key size
      $SUDO sed -i "s/\(dh \/etc\/openvpn\/easy-rsa\/pki\/dh\).*/\1${ENCRYPT}.pem/" /etc/openvpn/server.conf
    fi

    # if they modified port put value in server.conf
    if [ $PORT != 1194 ]; then
        $SUDO sed -i "s/1194/${PORT}/g" /etc/openvpn/server.conf
    fi

    # if they modified protocol put value in server.conf
    if [ "$PROTO" != "udp" ]; then
        $SUDO sed -i "s/proto udp/proto tcp/g" /etc/openvpn/server.conf
    fi

    # write out server certs to conf file
    $SUDO sed -i "s/\(key \/etc\/openvpn\/easy-rsa\/pki\/private\/\).*/\1${SERVER_NAME}.key/" /etc/openvpn/server.conf
    $SUDO sed -i "s/\(cert \/etc\/openvpn\/easy-rsa\/pki\/issued\/\).*/\1${SERVER_NAME}.crt/" /etc/openvpn/server.conf
}

confUnattendedUpgrades() {
    cd /etc/apt/apt.conf.d

    if [[ $UNATTUPG == "unattended-upgrades" ]]; then
        $SUDO apt-get --yes --quiet --no-install-recommends install "$UNATTUPG" > /dev/null & spinner $!
        if [[ $PLAT == "Ubuntu" ]]; then
            # Ubuntu 50unattended-upgrades should already just have security enabled
            # so we just need to configure the 10periodic file
            cat << EOT | $SUDO tee 10periodic >/dev/null
    APT::Periodic::Update-Package-Lists "1";
    APT::Periodic::Download-Upgradeable-Packages "1";
    APT::Periodic::AutocleanInterval "5";
    APT::Periodic::Unattended-Upgrade "1";
EOT
        else
            # Fix Raspbian config
            if [[ $PLAT == "Raspbian" ]]; then
                wget -q -O - "$UNATTUPG_CONFIG" | $SUDO tar xz
                $SUDO cp "unattended-upgrades-$UNATTUPG_RELEASE/data/50unattended-upgrades.Raspbian" 50unattended-upgrades
                $SUDO rm -rf "unattended-upgrades-$UNATTUPG_RELEASE"
            fi

            # Add the remaining settings for all other distributions
            cat << EOT | $SUDO tee 02periodic >/dev/null
    APT::Periodic::Enable "1";
    APT::Periodic::Update-Package-Lists "1";
    APT::Periodic::Download-Upgradeable-Packages "1";
    APT::Periodic::Unattended-Upgrade "1";
    APT::Periodic::AutocleanInterval "7";
    APT::Periodic::Verbose "0";
EOT
        fi
    fi

}

confNetwork() {
    # Enable forwarding of internet traffic
    $SUDO sed -i '/net.ipv4.ip_forward=1/s/^#//g' /etc/sysctl.conf
    $SUDO sysctl -p

    # if ufw enabled, configure that (running as root because sometimes the executable is not in the user's $PATH, on Debian for example)
    if $SUDO bash -c 'hash ufw' 2>/dev/null; then
        if LANG=en_US.UTF-8 $SUDO ufw status | grep -q inactive
        then
            noUFW=1
        else
            echo "::: Detected UFW is enabled."
            echo "::: Adding UFW rules..."
            $SUDO sed "/delete these required/i *nat\n:POSTROUTING ACCEPT [0:0]\n-I POSTROUTING -s <IP_ADDRESS>/24 -o $IPv4dev -j MASQUERADE\nCOMMIT\n" -i /etc/ufw/before.rules
            # Insert rules at the beginning of the chain (in case there are other rules that may drop the traffic)
            $SUDO ufw insert 1 allow "$PORT"/"$PROTO" >/dev/null
            # Don't forward everything, just the traffic originated from the VPN subnet
            $SUDO ufw route insert 1 allow in on tun0 from <IP_ADDRESS>/24 out on "$IPv4dev" to any >/dev/null
            $SUDO ufw reload >/dev/null
            echo "::: UFW configuration completed."
        fi
    else
        noUFW=1
    fi
    # else configure iptables
    if [[ $noUFW -eq 1 ]]; then
        echo 1 > /tmp/noUFW
        $SUDO iptables -t nat -I POSTROUTING -s <IP_ADDRESS>/24 -o "$IPv4dev" -j MASQUERADE
        case ${PLAT} in
            Ubuntu|Debian|Devuan)
                $SUDO iptables-save | $SUDO tee /etc/iptables/rules.v4 > /dev/null
                ;;
            *)
                $SUDO netfilter-persistent save
                ;;
        esac
    else
        echo 0 > /tmp/noUFW
    fi

    $SUDO cp /tmp/noUFW /etc/pivpn/NO_UFW
}

confOVPN() {
    if ! IPv4pub=$(dig +short myip.opendns.com @<IP_ADDRESS>) || ! valid_ip "$IPv4pub"; then
        echo "dig failed, now trying to curl checkip.amazonaws.com"
        if ! IPv4pub=$(curl -s https://checkip.amazonaws.com) || ! valid_ip "$IPv4pub"; then
            echo "checkip.amazonaws.com failed, please check your internet connection/DNS"
            exit 1
        fi
    fi
    $SUDO cp /tmp/pivpnUSR /etc/pivpn/INSTALL_USER
    $SUDO cp /tmp/DET_PLATFORM /etc/pivpn/DET_PLATFORM

    $SUDO cp /etc/.pivpn/Default.txt /etc/openvpn/easy-rsa/pki/Default.txt

    if [[ ${APPLY_TWO_POINT_FOUR} == true ]]; then
      #If they enabled 2.4 remove key-direction options since it's not required
      $SUDO sed -i "/key-direction 1/d" /etc/openvpn/easy-rsa/pki/Default.txt
    fi

    if [[ ${useUpdateVars} == false ]]; then
        METH=$(whiptail --title "Public IP or DNS" --radiolist "Will clients use a Public IP or DNS Name to connect to your server (press space to select)?" ${r} ${c} 2 \
        "$IPv4pub" "Use this public IP" "ON" \
        "DNS Entry" "Use a public DNS" "OFF" 3>&1 1>&2 2>&3)

        exitstatus=$?
        if [ $exitstatus != 0 ]; then
            echo "::: Cancel selected. Exiting..."
            exit 1
        fi

        if [ "$METH" == "$IPv4pub" ]; then
            $SUDO sed -i 's/IPv4pub/'"$IPv4pub"'/' /etc/openvpn/easy-rsa/pki/Default.txt
        else
            until [[ $publicDNSCorrect = True ]]
            do
                PUBLICDNS=$(whiptail --title "PiVPN Setup" --inputbox "What is the public DNS name of this Server?" ${r} ${c} 3>&1 1>&2 2>&3)
                exitstatus=$?
                if [ $exitstatus != 0 ]; then
                echo "::: Cancel selected. Exiting..."
                exit 1
                fi
                if (whiptail --backtitle "Confirm DNS Name" --title "Confirm DNS Name" --yesno "Is this correct?\n\n Public DNS Name:  $PUBLICDNS" ${r} ${c}) then
                    publicDNSCorrect=True
                    $SUDO sed -i 's/IPv4pub/'"$PUBLICDNS"'/' /etc/openvpn/easy-rsa/pki/Default.txt
                else
                    publicDNSCorrect=False
                fi
            done
        fi
    else
        $SUDO sed -i 's/IPv4pub/'"$PUBLICDNS"'/' /etc/openvpn/easy-rsa/pki/Default.txt
    fi

    # if they modified port put value in Default.txt for clients to use
    if [ $PORT != 1194 ]; then
        $SUDO sed -i -e "s/1194/${PORT}/g" /etc/openvpn/easy-rsa/pki/Default.txt
    fi

    # if they modified protocol put value in Default.txt for clients to use
    if [ "$PROTO" != "udp" ]; then
        $SUDO sed -i -e "s/proto udp/proto tcp/g" /etc/openvpn/easy-rsa/pki/Default.txt
    fi

    # verify server name to strengthen security
    $SUDO sed -i "s/SRVRNAME/${SERVER_NAME}/" /etc/openvpn/easy-rsa/pki/Default.txt

    if [ ! -d "/home/$pivpnUser/ovpns" ]; then
        $SUDO mkdir "/home/$pivpnUser/ovpns"
    fi
    $SUDO chmod 0777 -R "/home/$pivpnUser/ovpns"
}

confLogging() {
  echo "if \$programname == 'ovpn-server' then /var/log/openvpn.log
if \$programname == 'ovpn-server' then stop" | $SUDO tee /etc/rsyslog.d/30-openvpn.conf > /dev/null

  echo "/var/log/openvpn.log
{
    rotate 4
    weekly
    missingok
    notifempty
    compress
    delaycompress
    sharedscripts
    postrotate
        invoke-rc.d rsyslog rotate >/dev/null 2>&1 || true
    endscript
}" | $SUDO tee /etc/logrotate.d/openvpn > /dev/null

  # Restart the logging service
  case ${PLAT} in
    Ubuntu|Debian|*vuan)
      $SUDO service rsyslog restart || true
      ;;
    *)
      $SUDO systemctl restart rsyslog.service || true
      ;;
  esac
}

finalExports() {
    # Update variables in setupVars.conf file
    if [ -e "${setupVars}" ]; then
        $SUDO sed -i.update.bak '/pivpnUser/d;/UNATTUPG/d;/pivpnInterface/d;/IPv4dns/d;/IPv4addr/d;/IPv4gw/d;/pivpnProto/d;/PORT/d;/ENCRYPT/d;/DOWNLOAD_DH_PARAM/d;/PUBLICDNS/d;/OVPNDNS1/d;/OVPNDNS2/d;' "${setupVars}"
    fi
    {
        echo "pivpnUser=${pivpnUser}"
        echo "UNATTUPG=${UNATTUPG}"
        echo "pivpnInterface=${pivpnInterface}"
        echo "IPv4dns=${IPv4dns}"
        echo "IPv4addr=${IPv4addr}"
        echo "IPv4gw=${IPv4gw}"
        echo "pivpnProto=${pivpnProto}"
        echo "PORT=${PORT}"
        echo "ENCRYPT=${ENCRYPT}"
        echo "APPLY_TWO_POINT_FOUR=${APPLY_TWO_POINT_FOUR}"
        echo "DOWNLOAD_DH_PARAM=${DOWNLOAD_DH_PARAM}"
        echo "PUBLICDNS=${PUBLICDNS}"
        echo "OVPNDNS1=${OVPNDNS1}"
        echo "OVPNDNS2=${OVPNDNS2}"
    } | $SUDO tee "${setupVars}" > /dev/null
}

# I suggest replacing some of these names.

#accountForRefactor() {
#    # At some point in the future this list can be pruned, for now we'll need it to ensure updates don't break.
#
#    # Refactoring of install script has changed the name of a couple of variables.  Is this always OK? Also if you only select one DNS server ??
    $SUDO sed -i '0,/\(dhcp-option DNS \)/ s/\(dhcp-option DNS \).*/\1'${OVPNDNS1}'\"/' /etc/openvpn/server.conf
    $SUDO sed -i '0,/\(dhcp-option DNS \)/! s/\(dhcp-option DNS \).*/\1'${OVPNDNS2}'\"/' /etc/openvpn/server.conf

    finalExports #re-export setupVars.conf to account for any new vars added in new versions
}

displayFinalMessage() {
    # Final completion message to user
    whiptail --msgbox --backtitle "Make it so." --title "Installation Complete!" "Now run 'pivpn add' to create the ovpn profiles.
Run 'pivpn help' to see what else you can do!" ${r} ${c}
    if (whiptail --title "Reboot" --yesno --defaultno "It is strongly recommended you reboot after installation.  Would you like to reboot now?" ${r} ${c}); then
        whiptail --title "Rebooting" --msgbox "The system will now reboot." ${r} ${c}
        printf "\nRebooting system...\n"
        $SUDO sleep 3
        $SUDO shutdown -r now
    fi
}

update_dialogs() {
    # reconfigure
    if [ "${reconfigure}" = true ]; then
        opt1a="Repair"
        opt1b="This will retain existing settings"
        strAdd="You will remain on the same version"
    else
        opt1a="Update"
        opt1b="This will retain existing settings."
        strAdd="You will be updated to the latest version."
    fi
    opt2a="Reconfigure"
    opt2b="This will allow you to enter new settings"

    UpdateCmd=$(whiptail --title "Existing Install Detected!" --menu "\n\nWe have detected an existing install.\n\nPlease choose from the following options: \n($strAdd)" ${r} ${c} 2 \
    "${opt1a}"  "${opt1b}" \
    "${opt2a}"  "${opt2b}" 3>&2 2>&1 1>&3) || \
    { echo "::: Cancel selected. Exiting"; exit 1; }

    case ${UpdateCmd} in
        ${opt1a})
            echo "::: ${opt1a} option selected."
            useUpdateVars=true
            ;;
        ${opt2a})
            echo "::: ${opt2a} option selected"
            useUpdateVars=false
            ;;
    esac
}

clone_or_update_repos() {
    if [[ "${reconfigure}" == true ]]; then
        echo "::: --reconfigure passed to install script. Not downloading/updating local repos"
    else
        # Get Git files
        getGitFiles ${pivpnFilesDir} ${pivpnGitUrl} || \
        { echo "!!! Unable to clone ${pivpnGitUrl} into ${pivpnFilesDir}, unable to continue."; \
            exit 1; \
        }
    fi
}

######## SCRIPT ############

main() {

    ######## FIRST CHECK ########
    # Must be root to install
    echo ":::"
    if [[ $EUID -eq 0 ]];then
        echo "::: You are root."
    else
        echo "::: sudo will be used for the install."
        # Check if it is actually installed
        # If it isn't, exit because the install cannot complete
        if [[ $(dpkg-query -s sudo) ]];then
            export SUDO="sudo"
            export SUDOE="sudo -E"
        else
            echo "::: Please install sudo or run this as root."
            exit 1
        fi
    fi

    # Check for supported distribution
    distro_check

    # Check arguments for the undocumented flags
    for var in "$@"; do
        case "$var" in
            "--reconfigure"  ) reconfigure=true;;
            "--i_do_not_follow_recommendations"   ) skipSpaceCheck=false;;
            "--unattended"     ) runUnattended=true;;
        esac
    done

    if [[ -f ${setupVars} ]]; then
        if [[ "${runUnattended}" == true ]]; then
            echo "::: --unattended passed to install script, no whiptail dialogs will be displayed"
            useUpdateVars=true
        else
            update_dialogs
        fi
    fi

    # Start the installer
    # Verify there is enough disk space for the install
    if [[ "${skipSpaceCheck}" == true ]]; then
        echo "::: --i_do_not_follow_recommendations passed to script, skipping free disk space verification!"
    else
        verifyFreeDiskSpace
    fi

    # Install the packages (we do this first because we need whiptail)
    addSoftwareRepo

    update_package_cache

    # Notify user of package availability
    notify_package_updates_available

    # Install packages used by this installation script
    install_dependent_packages PIVPN_DEPS[@]

    if [[ ${useUpdateVars} == false ]]; then
        # Display welcome dialogs
        welcomeDialogs

        # Find interfaces and let the user choose one
        chooseInterface

        # Only try to set static on Raspbian, otherwise let user do it
        if [[ $PLAT != "Raspbian" ]]; then
            avoidStaticIPv4Ubuntu
        else
            getStaticIPv4Settings
            setStaticIPv4
        fi

        # Choose the user for the ovpns
        chooseUser

        # Ask if unattended-upgrades will be enabled
        unattendedUpgrades

        # Clone/Update the repos
        clone_or_update_repos

        # Install
        if installPiVPN; then
            echo "::: Install Complete..."
        else
            exit 1
        fi
    else
        # Source ${setupVars} for use in the rest of the functions.
        source ${setupVars}

        echo "::: Using IP address: $IPv4addr"
        echo "${IPv4addr%/*}" > /tmp/pivpnIP
        echo "::: Using interface: $pivpnInterface"
        echo "${pivpnInterface}" > /tmp/pivpnINT
        echo "::: Using User: $pivpnUser"
        echo "${pivpnUser}" > /tmp/pivpnUSR
        echo "::: Using protocol: $pivpnProto"
        echo "${pivpnProto}" > /tmp/pivpnPROTO
        echo "::: Using port: $PORT"
        echo ${PORT} > /tmp/INSTALL_PORT
        echo ":::"

        # Only try to set static on Raspbian
        case ${PLAT} in
          Rasp*)
            setStaticIPv4 # This might be a problem if a user tries to modify the ip in the config file and then runs an update because of the way we check for previous configuration in /etc/dhcpcd.conf
            ;;
          *)
            echo "::: IP Information"
            echo "::: Since we think you are not using Raspbian, we will not configure a static IP for you."
            echo "::: If you are in Amazon then you can not configure a static IP anyway."
            echo "::: Just ensure before this installer started you had set an elastic IP on your instance."
            ;;
          esac

        # Clone/Update the repos
        clone_or_update_repos

        updatePiVPN
    fi

    echo "::: Restarting services..."
    # Start services
    case ${PLAT} in
        Ubuntu|Debian|*vuan)
            $SUDO service openvpn start
            ;;
        *)
            $SUDO systemctl enable openvpn.service
            $SUDO systemctl start openvpn.service
            ;;
    esac

    # Ensure that cached writes reach persistent storage
    echo "::: Flushing writes to disk..."
    sync

    echo "::: done."

    if [[ "${useUpdateVars}" == false ]]; then
        displayFinalMessage
    fi

    echo ":::"
    if [[ "${useUpdateVars}" == false ]]; then
        echo "::: Installation Complete!"
        echo "::: Now run 'pivpn add' to create an ovpn profile for each of your devices."
        echo "::: Run 'pivpn help' to see what else you can do!"
        echo "::: It is strongly recommended you reboot after installation."
    else
        echo "::: Update complete!"
    fi

    echo ":::"
}

if [[ "${PIVPN_TEST}" != true ]] ; then
  main "$@"
fi
+ [[ '' != true ]]
+ main
+ echo :::
:::
+ [[ 0 -eq 0 ]]
+ echo '::: You are root.'
::: You are root.
+ distro_check
+ hash lsb_release
lsb_release -si
++ lsb_release -si
+ PLAT=Raspbian
lsb_release -sc
++ lsb_release -sc
+ OSCN=jessie
+ [[ jessie != \b\i\o\n\i\c ]]
+ PIVPN_DEPS+=(dhcpcd5)
+ case ${PLAT} in
+ case ${OSCN} in
+ echo Raspbian
+ [[ -f /etc/pivpn/setupVars.conf ]]
+ [[ false == true ]]
+ update_dialogs
+ '[' false = true ']'
+ opt1a=Update
+ opt1b='This will retain existing settings.'
+ strAdd='You will be updated to the latest version.'
+ opt2a=Reconfigure
+ opt2b='This will allow you to enter new settings'
whiptail --title "Existing Install Detected!" --menu "\n\nWe have detected an existing install.\n\nPlease choose from the following options: \n($strAdd)" ${r} ${c} 2     "${opt1a}"  "${opt1b}"     "${opt2a}"  "${opt2b}" 3>&2 2>&1 1>&3
++ whiptail --title 'Existing Install Detected!' --menu '\n\nWe have detected an existing install.\n\nPlease choose from the following options: \n(You will be updated to the latest version.)' 21 70 2 Update 'This will retain existing settings.' Reconfigure 'This will allow you to enter new settings'
+ UpdateCmd=Update
+ case ${UpdateCmd} in
+ echo '::: Update option selected.'
::: Update option selected.
+ useUpdateVars=true
+ [[ false == true ]]
+ verifyFreeDiskSpace
+ echo '::: Verifying free disk space...'
::: Verifying free disk space...
+ local required_free_kilobytes=76800
df -Pk | grep -m1 '\/$' | awk '{print $4}'
++ df -Pk
++ awk '{print $4}'
++ grep -m1 '\/$'
+ local existing_free_kilobytes=2817592
+ [[ 2817592 =~ ^([0-9])+$ ]]
+ [[ 2817592 -lt 76800 ]]
+ addSoftwareRepo
+ case ${PLAT} in
+ update_package_cache
stat -c %Y ${PKG_CACHE}
++ stat -c %Y /var/lib/apt/lists/
+ timestamp=1558009551
date -d @"${timestamp}" "+%b %e"
++ date -d @1558009551 '+%b %e'
+ timestampAsDate='May 16'
date "+%b %e"
++ date '+%b %e'
+ today='May 16'
+ '[' '!' 'May 16' == 'May 16' ']'
+ notify_package_updates_available
+ echo :::
:::
+ echo -n '::: Checking apt-get for upgraded packages....'
::: Checking apt-get for upgraded packages....eval "${PKG_COUNT}"
++ eval 'apt-get -s -o Debug::NoLocking=true upgrade | grep -c ^Inst || true'
apt-get -s -o Debug::NoLocking=true upgrade | grep -c ^Inst || true
+++ apt-get -s -o Debug::NoLocking=true upgrade
+++ grep -c '^Inst'
+++ true
+ updatesToInstall=0
+ echo ' done!'
 done!
+ echo :::
:::
+ [[ 0 -eq 0 ]]
+ echo '::: Your system is up to date! Continuing with PiVPN installation...'
::: Your system is up to date! Continuing with PiVPN installation...
+ install_dependent_packages 'PIVPN_DEPS[@]'
+ argArray1=("${!1}")
+ declare -a argArray1
+ echo iptables-persistent iptables-persistent/autosave_v4 boolean true
+ debconf-set-selections
+ echo iptables-persistent iptables-persistent/autosave_v6 boolean false
+ debconf-set-selections
+ grep -q https /etc/apt/sources.list
+ command -v debconf-apt-progress
+ debconf-apt-progress -- apt-get --yes --no-install-recommends install openvpn git tar wget grep iptables-persistent dnsutils expect whiptail net-tools dhcpcd5
+ [[ true == false ]]
+ source /etc/pivpn/setupVars.conf
pivpnUser=pi
++ pivpnUser=pi
UNATTUPG=unattended-upgrades
++ UNATTUPG=unattended-upgrades
pivpnInterface=wlan0
++ pivpnInterface=wlan0
IPv4dns=<IP_ADDRESS>
++ IPv4dns=<IP_ADDRESS>
IPv4addr=<IP_ADDRESS>
++ IPv4addr=<IP_ADDRESS>
IPv4gw=<IP_ADDRESS>
++ IPv4gw=<IP_ADDRESS>
pivpnProto=tcp
++ pivpnProto=tcp
PORT=4430
++ PORT=4430
ENCRYPT=2048
++ ENCRYPT=2048
APPLY_TWO_POINT_FOUR=false
++ APPLY_TWO_POINT_FOUR=false
DOWNLOAD_DH_PARAM=false
++ DOWNLOAD_DH_PARAM=false
PUBLICDNS=
++ PUBLICDNS=
OVPNDNS1=<IP_ADDRESS>
++ OVPNDNS1=<IP_ADDRESS>
OVPNDNS2=<IP_ADDRESS>
++ OVPNDNS2=<IP_ADDRESS>
+ echo '::: Using IP address: <IP_ADDRESS>'
::: Using IP address: <IP_ADDRESS>
+ echo <IP_ADDRESS>
+ echo '::: Using interface: wlan0'
::: Using interface: wlan0
+ echo wlan0
+ echo '::: Using User: pi'
::: Using User: pi
+ echo pi
+ echo '::: Using protocol: tcp'
::: Using protocol: tcp
+ echo tcp
+ echo '::: Using port: 4430'
::: Using port: 4430
+ echo 4430
+ echo :::
:::
+ case ${PLAT} in
+ setStaticIPv4
+ [[ -f /etc/dhcpcd.conf ]]
+ grep -q <IP_ADDRESS> /etc/dhcpcd.conf
+ echo '::: Static IP already configured.'
::: Static IP already configured.
+ clone_or_update_repos
+ [[ false == true ]]
+ getGitFiles /etc/.pivpn https://github.com/pivpn/pivpn.git
+ echo :::
:::
+ echo '::: Checking for existing base files...'
::: Checking for existing base files...
+ is_repo /etc/.pivpn
+ echo -n ':::    Checking /etc/.pivpn is a repo...'
:::    Checking /etc/.pivpn is a repo...+ cd /etc/.pivpn
+ git status
+ echo ' OK!'
 OK!
+ return 0
+ update_repo /etc/.pivpn
+ [[ false == true ]]
+ echo -n ':::     Updating repo in /etc/.pivpn...'
:::     Updating repo in /etc/.pivpn...+ rm -rf /etc/.pivpn
+ spinner 4397
+ local pid=4397
+ git clone -q --depth 1 --no-single-branch '' /etc/.pivpn
+ local delay=0.50
+ local 'spinstr=/-\|'
ps a | awk '{print $1}' | grep "${pid}"
++ ps a
++ awk '{print $1}'
fatal: repository '' does not exist
++ grep 4397
+ '[' '' ']'
+ printf '    \b\b\b\b'
+ cd /etc/.pivpn
./install.sh: line 566: cd: /etc/.pivpn: No such file or directory
+ exit 1

You posted the same output several times, it seems interesting because it could indicate that the update feature may have some problems, so keep the last message and delete the duplicates.
However, post it again without updating/reconfiguring.

This output seems interesting because it could indicate that the update feature may have some problems, so keep the last message and delete the duplicates.
However, post it again without updating/reconfiguring.

That's not an option.
I have to choose one.

Ok sorry, reconfigure then.

Ok sorry, reconfigure then.

I lost the previous lines...
  #Running apt-get update/upgrade with minimal output can cause some issues with
  #requiring user input

  #Check to see if apt-get update has already been run today
  #it needs to have been run at least once on new installs!
  timestamp=$(stat -c %Y ${PKG_CACHE})
  timestampAsDate=$(date -d @"${timestamp}" "+%b %e")
  today=$(date "+%b %e")

  if [ ! "${today}" == "${timestampAsDate}" ]; then
    #update package lists
    echo ":::"
    echo -n "::: ${PKG_MANAGER} update has not been run today. Running now..."
    $SUDO ${UPDATE_PKG_CACHE} &> /dev/null
    echo " done!"
  fi
}

notify_package_updates_available() {
  # Let user know if they have outdated packages on their system and
  # advise them to run a package update at soonest possible.
  echo ":::"
  echo -n "::: Checking ${PKG_MANAGER} for upgraded packages...."
  updatesToInstall=$(eval "${PKG_COUNT}")
  echo " done!"
  echo ":::"
  if [[ ${updatesToInstall} -eq "0" ]]; then
    echo "::: Your system is up to date! Continuing with PiVPN installation..."
  else
    echo "::: There are ${updatesToInstall} updates available for your system!"
    echo "::: We recommend you update your OS after installing PiVPN! "
    echo ":::"
  fi
}

install_dependent_packages() {
    # Install packages passed in via argument array
    # No spinner - conflicts with set -e
    declare -a argArray1=("${!1}")

    echo iptables-persistent iptables-persistent/autosave_v4 boolean true | $SUDO debconf-set-selections
    echo iptables-persistent iptables-persistent/autosave_v6 boolean false | $SUDO debconf-set-selections

    # Add support for https repositories if there are any that use it otherwise the installation will silently fail
    if grep -q https /etc/apt/sources.list; then
        PIVPN_DEPS+=("apt-transport-https")
    fi

    if command -v debconf-apt-progress &> /dev/null; then
        $SUDO debconf-apt-progress -- ${PKG_INSTALL} "${argArray1[@]}"
    else
        for i in "${argArray1[@]}"; do
            echo -n ":::    Checking for $i..."
            $SUDO package_check_install "${i}" &> /dev/null
            echo " installed!"
        done
    fi
}

unattendedUpgrades() {
    whiptail --msgbox --backtitle "Security Updates" --title "Unattended Upgrades" "Since this server will have at least one port open to the internet, it is recommended you enable unattended-upgrades.\nThis feature will check daily for security package updates only and apply them when necessary.\nIt will NOT automatically reboot the server so to fully apply some updates you should periodically reboot." ${r} ${c}

    if (whiptail --backtitle "Security Updates" --title "Unattended Upgrades" --yesno "Do you want to enable unattended upgrades of security patches to this server?" ${r} ${c}) then
        UNATTUPG="unattended-upgrades"
    else
        UNATTUPG=""
    fi
}

stopServices() {
    # Stop openvpn
    $SUDO echo ":::"
    $SUDO echo -n "::: Stopping OpenVPN service..."
    case ${PLAT} in
        Ubuntu|Debian|*vuan)
            $SUDO service openvpn stop || true
            ;;
        *)
            $SUDO systemctl stop openvpn.service || true
            ;;
    esac
    $SUDO echo " done."
}

getGitFiles() {
    # Setup git repos for base files
    echo ":::"
    echo "::: Checking for existing base files..."
    if is_repo "${1}"; then
        update_repo "${1}"
    else
        make_repo "${1}" "${2}"
    fi
}

is_repo() {
    # If the directory does not have a .git folder it is not a repo
    echo -n ":::    Checking $1 is a repo..."
    cd "${1}" &> /dev/null || return 1
    $SUDO git status &> /dev/null && echo " OK!"; return 0 || echo " not found!"; return 1
}

make_repo() {
    # Remove the non-repos interface and clone the interface
    echo -n ":::    Cloning $2 into $1..."
    $SUDO rm -rf "${1}"
    $SUDO git clone -q --depth 1 --no-single-branch "${2}" "${1}" > /dev/null & spinner $!
    if [ -z "${TESTING+x}" ]; then
        :
    else
        $SUDO git -C "${1}" checkout test
    fi
    echo " done!"
}

update_repo() {
    if [[ "${reconfigure}" == true ]]; then
          echo "::: --reconfigure passed to install script. Not downloading/updating local repos"
    else
        # Pull the latest commits
        echo -n ":::     Updating repo in $1..."
        $SUDO rm -rf "${1}"
        $SUDO git clone -q --depth 1 --no-single-branch "${2}" "${1}" > /dev/null & spinner $!
        cd "${1}" || exit 1
        if [ -z "${TESTING+x}" ]; then
            :
        else
            ${SUDOE} git checkout test
        fi
        echo " done!"
    fi
}

setCustomProto() {
  # Set the available protocols into an array so it can be used with a whiptail dialog
  if protocol=$(whiptail --title "Protocol" --radiolist \
  "Choose a protocol (press space to select). Please only choose TCP if you know why you need TCP." ${r} ${c} 2 \
  "UDP" "" ON \
  "TCP" "" OFF 3>&1 1>&2 2>&3)
  then
      # Convert option into lowercase (UDP->udp)
      pivpnProto="${protocol,,}"
      echo "::: Using protocol: $pivpnProto"
      echo "${pivpnProto}" > /tmp/pivpnPROTO
  else
      echo "::: Cancel selected, exiting...."
      exit 1
  fi
    # write out the PROTO
    PROTO=$pivpnProto
    $SUDO cp /tmp/pivpnPROTO /etc/pivpn/INSTALL_PROTO
}

setCustomPort() {
    until [[ $PORTNumCorrect = True ]]
        do
            portInvalid="Invalid"

            PROTO=$(cat /etc/pivpn/INSTALL_PROTO)
            if [ "$PROTO" = "udp" ]; then
              DEFAULT_PORT=1194
            else
              DEFAULT_PORT=443
            fi
            if PORT=$(whiptail --title "Default OpenVPN Port" --inputbox "You can modify the default OpenVPN port. \nEnter a new value or hit 'Enter' to retain the default" ${r} ${c} $DEFAULT_PORT 3>&1 1>&2 2>&3)
            then
                if [[ "$PORT" =~ ^[0-9]+$ ]] && [ "$PORT" -ge 1 ] && [ "$PORT" -le 65535 ]; then
                    :
                else
                    PORT=$portInvalid
                fi
            else
                echo "::: Cancel selected, exiting...."
                exit 1
            fi

            if [[ $PORT == "$portInvalid" ]]; then
                whiptail --msgbox --backtitle "Invalid Port" --title "Invalid Port" "You entered an invalid Port number.\n    Please enter a number from 1 - 65535.\n    If you are not sure, please just keep the default." ${r} ${c}
                PORTNumCorrect=False
            else
                if (whiptail --backtitle "Specify Custom Port" --title "Confirm Custom Port Number" --yesno "Are these settings correct?\n    PORT:   $PORT" ${r} ${c}) then
                    PORTNumCorrect=True
                else
                    # If the settings are wrong, the loop continues
                    PORTNumCorrect=False
                fi
            fi
        done
    # write out the port
    echo ${PORT} > /tmp/INSTALL_PORT
    $SUDO cp /tmp/INSTALL_PORT /etc/pivpn/INSTALL_PORT
}

setClientDNS() {
    DNSChoseCmd=(whiptail --separate-output --radiolist "Select the DNS Provider for your VPN Clients (press space to select). To use your own, select Custom."
        declare -A DNS_MAP=(["Google"]="<IP_ADDRESS> <IP_ADDRESS>" ["OpenDNS"]="<IP_ADDRESS> <IP_ADDRESS>" ["Level3"]="<IP_ADDRESS> <IP_ADDRESS>" ["DNS.WATCH"]="<IP_ADDRESS> <IP_ADDRESS>" ["Norton"]="<IP_ADDRESS> <IP_ADDRESS>" ["FamilyShield"]="<IP_ADDRESS> <IP_ADDRESS>" ["CloudFlare"]="<IP_ADDRESS> <IP_ADDRESS>")

        OVPNDNS1=$(awk '{print $1}' <<< "${DNS_MAP["${DNSchoices}"]}")
        OVPNDNS2=$(awk '{print $2}' <<< "${DNS_MAP["${DNSchoices}"]}")

        $SUDO sed -i '0,/\(dhcp-option DNS \)/ s/\(dhcp-option DNS \).*/\1'${OVPNDNS1}'\"/' /etc/openvpn/server.conf
        $SUDO sed -i '0,/\(dhcp-option DNS \)/! s/\(dhcp-option DNS \).*/\1'${OVPNDNS2}'\"/' /etc/openvpn/server.conf

      else

          until [[ $DNSSettingsCorrect = True ]]
          do
              strInvalid="Invalid"

              if OVPNDNS=$(whiptail --backtitle "Specify Upstream DNS Provider(s)"  --inputbox "Enter your desired upstream DNS provider(s), seperated by a comma.\n\nFor example '<IP_ADDRESS>, <IP_ADDRESS>'" ${r} ${c} "" 3>&1 1>&2 2>&3)
              then
                    OVPNDNS1=$(echo "$OVPNDNS" | sed 's/[, \t]\+/,/g' | awk -F, '{print$1}')
                    OVPNDNS2=$(echo "$OVPNDNS" | sed 's/[, \t]\+/,/g' | awk -F, '{print$2}')
                    if ! valid_ip "$OVPNDNS1" || [ ! "$OVPNDNS1" ]; then
                        OVPNDNS1=$strInvalid
                    fi
                    if ! valid_ip "$OVPNDNS2" && [ "$OVPNDNS2" ]; then
                        OVPNDNS2=$strInvalid
                    fi
              else
                    echo "::: Cancel selected, exiting...."
                    exit 1
                fi
              if [[ $OVPNDNS1 == "$strInvalid" ]] || [[ $OVPNDNS2 == "$strInvalid" ]]; then
                    whiptail --msgbox --backtitle "Invalid IP" --title "Invalid IP" "One or both entered IP addresses were invalid. Please try again.\n\n    DNS Server 1:   $OVPNDNS1\n    DNS Server 2:   $OVPNDNS2" ${r} ${c}
                    if [[ $OVPNDNS1 == "$strInvalid" ]]; then
                        OVPNDNS1=""
                    fi
                    if [[ $OVPNDNS2 == "$strInvalid" ]]; then
                        OVPNDNS2=""
                    fi
                    DNSSettingsCorrect=False
              else
                    if (whiptail --backtitle "Specify Upstream DNS Provider(s)" --title "Upstream DNS Provider(s)" --yesno "Are these settings correct?\n    DNS Server 1:   $OVPNDNS1\n    DNS Server 2:   $OVPNDNS2" ${r} ${c}) then
                        DNSSettingsCorrect=True
                        $SUDO sed -i '0,/\(dhcp-option DNS \)/ s/\(dhcp-option DNS \).*/\1'${OVPNDNS1}'\"/' /etc/openvpn/server.conf
                        if [ -z ${OVPNDNS2} ]; then
                            $SUDO sed -i '/\(dhcp-option DNS \)/{n;N;d}' /etc/openvpn/server.conf
                        else
                            $SUDO sed -i '0,/\(dhcp-option DNS \)/! s/\(dhcp-option DNS \).*/\1'${OVPNDNS2}'\"/' /etc/openvpn/server.conf
                        fi
                    else
                        # If the settings are wrong, the loop continues
                        DNSSettingsCorrect=False
                    fi
                fi
          done
      fi

    else
      echo "::: Cancel selected. Exiting..."
      exit 1
    fi
}

#This procedure allows a user to specify a custom search domain if they have one.
setCustomDomain() {
  DomainSettingsCorrect=False

  if (whiptail --backtitle "Custom Search Domain" --title "Custom Search Domain" --yesno "Would you like to add a custom search domain? \n (This is only for advanced users who have their own domain)\n" ${r} ${c}); then

    until [[ $DomainSettingsCorrect = True ]]
    do
      if CUSTOMDomain=$(whiptail --inputbox "Enter Custom Domain\nFormat: mydomain.com" ${r} ${c} --title "Custom Domain" 3>&1 1>&2 2>&3); then
          if valid_domain "$CUSTOMDomain"; then
            if (whiptail --backtitle "Custom Search Domain" --title "Custom Search Domain" --yesno "Are these settings correct?\n    Custom Search Domain: $CUSTOMDomain" ${r} ${c}); then
                DomainSettingsCorrect=True

                $SUDO sed -i '0,/\(.*dhcp-option.*\)/s//\push "dhcp-option DOMAIN '${CUSTOMDomain}'" \n&/' /etc/openvpn/server.conf

            else
                # If the settings are wrong, the loop continues
                DomainSettingsCorrect=False
            fi
          else
            whiptail --msgbox --backtitle "Invalid Domain" --title "Invalid Domain" "Domain is invalid. Please try again.\n\n    DOMAIN:   $CUSTOMDomain\n" ${r} ${c}
            DomainSettingsCorrect=False
          fi
      else
        echo "::: Cancel selected. Exiting..."
        exit 1
      fi
    done

  else
    echo sleep 0.1
  fi
}

confOpenVPN() {
    # Generate a random, alphanumeric identifier of 16 characters for this server so that we can use verify-x509-name later that is unique for this server installation. Source: Earthgecko (https://gist.github.com/earthgecko/3089509)
    NEW_UUID=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1)
    SERVER_NAME="server_${NEW_UUID}"

    if [[ ${useUpdateVars} == false ]]; then
        # Ask user for desired level of encryption

        if [[ ${useUpdateVars} == false ]]; then
            if [[ ${PLAT} == "Raspbian" ]] && [[ ${OSCN} != "stretch" ]]; then
                APPLY_TWO_POINT_FOUR=false
            else
                if (whiptail --backtitle "Setup OpenVPN" --title "Installation mode" --yesno "OpenVPN 2.4 brings support for stronger authentication and key exchange using Elliptic Curves, along with encrypted control channel.\n\nIf your clients do run OpenVPN 2.4 or later you can enable these features, otherwise choose 'No' for best compatibility.\n\nNOTE: Current mobile app, that is OpenVPN connect, is supported." ${r} ${c}); then
                    APPLY_TWO_POINT_FOUR=true
                    $SUDO touch /etc/pivpn/TWO_POINT_FOUR
                else
                    APPLY_TWO_POINT_FOUR=false
                fi
            fi
        fi

        if [[ ${runUnattended} == true ]] && [[ ${APPLY_TWO_POINT_FOUR} == true ]]; then
            $SUDO touch /etc/pivpn/TWO_POINT_FOUR
        fi

        if [[ ${APPLY_TWO_POINT_FOUR} == false ]]; then

            ENCRYPT=$(whiptail --backtitle "Setup OpenVPN" --title "RSA certificate size" --radiolist \
            "Choose the desired size of your certificate (press space to select):\n   This is a certificate that will be generated on your system.  The larger the certificate, the more time this will take.  For most applications, it is recommended to use 2048 bits.  If you are testing, you can use 1024 bits to speed things up, but do not use this for normal use!  If you are paranoid about ... things... then grab a cup of joe and pick 4096 bits."   The larger the certificate, the more time this will take.  For most applications, it is recommended to use 256 bits.  You can increase the number of bits if you care about, however, consider that 256 bits are already as secure as 3072 bit RSA."  Exiting..."
            exit 1
        fi
    fi

    # If easy-rsa exists, remove it
    if [[ -d /etc/openvpn/easy-rsa/ ]]; then
        $SUDO rm -rf /etc/openvpn/easy-rsa/
    fi

    # Get easy-rsa
    wget -q -O - "${easyrsaRel}" | $SUDO tar xz -C /etc/openvpn && $SUDO mv /etc/openvpn/EasyRSA-v${easyrsaVer} /etc/openvpn/easy-rsa
    # fix ownership
    $SUDO chown -R root:root /etc/openvpn/easy-rsa
    $SUDO mkdir /etc/openvpn/easy-rsa/pki

    cd /etc/openvpn/easy-rsa || exit

    # Write out new vars file
    set +e
    IFS= read -d '' String <<"EOF"
if [ -z "$EASYRSA_CALLER" ]; then
    echo "Nope." >&2
    return 1
fi
set_var EASYRSA            "/etc/openvpn/easy-rsa"
set_var EASYRSA_PKI        "$EASYRSA/pki"
set_var EASYRSA_CRL_DAYS   3650
EOF
    echo "${String}" | $SUDO tee vars >/dev/null
    set -e

    # Set certificate type
    if [[ ${APPLY_TWO_POINT_FOUR} == false ]]; then
        echo "set_var EASYRSA_ALGO       rsa" | $SUDO tee -a vars
        echo "set_var EASYRSA_KEY_SIZE   ${ENCRYPT}" | $SUDO tee -a vars
    else
        echo "set_var EASYRSA_ALGO       ec" | $SUDO tee -a vars
        echo "set_var EASYRSA_CURVE      ${ECDSA_MAP["${ENCRYPT}"]}" | $SUDO tee -a vars
    fi

    # Remove any previous keys
    ${SUDOE} ./easyrsa --batch init-pki

    # Build the certificate authority
    printf "::: Building CA...\n"
    ${SUDOE} ./easyrsa --batch build-ca nopass
    printf "\n::: CA Complete.\n"

    if [[ ${useUpdateVars} == false ]]; then
        if [[ ${APPLY_TWO_POINT_FOUR} == false ]]; then
            whiptail --msgbox --backtitle "Setup OpenVPN" --title "Server Information" "The server key, Diffie-Hellman parameters, and HMAC key will now be generated." ${r} ${c}
        fi
    fi

    # Build the server
    ${SUDOE} ./easyrsa build-server-full ${SERVER_NAME} nopass

    if [[ ${useUpdateVars} == false ]]; then
      if [[ ${APPLY_TWO_POINT_FOUR} == false ]]; then
        if ([ "$ENCRYPT" -ge "4096" ] && whiptail --backtitle "Setup OpenVPN" --title "Download Diffie-Hellman Parameters" --yesno --defaultno "Download Diffie-Hellman parameters from a public DH parameter generation service?\n\nGenerating DH parameters for a $ENCRYPT-bit key can take many hours on a Raspberry Pi. You can instead download DH parameters from \"2 Ton Digital\" that are generated at regular intervals as part of a public service. Downloaded DH parameters will be randomly selected from their database.\nMore information about this service can be found here: https://2ton.com.au/safeprimes/\n\nIf you're paranoid, choose 'No' and Diffie-Hellman parameters will be generated on your device." ${r} ${c}); then
          DOWNLOAD_DH_PARAM=true
        else
          DOWNLOAD_DH_PARAM=false
        fi
      else
        DOWNLOAD_DH_PARAM=false
      fi
    fi

    if [[ ${APPLY_TWO_POINT_FOUR} == false ]]; then
      if [ "$ENCRYPT" -ge "4096" ] && [[ ${DOWNLOAD_DH_PARAM} == true ]]; then
        # Downloading parameters
        ${SUDOE} curl "https://2ton.com.au/getprimes/random/dhparam/${ENCRYPT}" -o "/etc/openvpn/easy-rsa/pki/dh${ENCRYPT}.pem"
      else
        # Generate Diffie-Hellman key exchange
        ${SUDOE} ./easyrsa gen-dh
        ${SUDOE} mv pki/dh.pem pki/dh${ENCRYPT}.pem
      fi
    fi

    # Generate static HMAC key to defend against DDoS
    ${SUDOE} openvpn --genkey --secret pki/ta.key

    # Generate an empty Certificate Revocation List
    ${SUDOE} ./easyrsa gen-crl
    ${SUDOE} cp pki/crl.pem /etc/openvpn/crl.pem
    ${SUDOE} chown nobody:nogroup /etc/openvpn/crl.pem

    # Write config file for server using the template.txt file
    $SUDO cp /etc/.pivpn/server_config.txt /etc/openvpn/server.conf

    if [[ ${APPLY_TWO_POINT_FOUR} == true ]]; then
      #If they enabled 2.4 use tls-crypt instead of tls-auth to encrypt control channel
      $SUDO sed -i "s/tls-auth \/etc\/openvpn\/easy-rsa\/pki\/ta.key 0/tls-crypt \/etc\/openvpn\/easy-rsa\/pki\/ta.key/" /etc/openvpn/server.conf
    fi

    if [[ ${APPLY_TWO_POINT_FOUR} == true ]]; then
      #If they enabled 2.4 disable dh parameters since the key exchange will use the matching curve from the ECDSA certificate
      $SUDO sed -i "s/\(dh \/etc\/openvpn\/easy-rsa\/pki\/dh\).*/dh none/" /etc/openvpn/server.conf
    else
      # Otherwise set the user encryption key size
      $SUDO sed -i "s/\(dh \/etc\/openvpn\/easy-rsa\/pki\/dh\).*/\1${ENCRYPT}.pem/" /etc/openvpn/server.conf
    fi

    # if they modified port put value in server.conf
    if [ $PORT != 1194 ]; then
        $SUDO sed -i "s/1194/${PORT}/g" /etc/openvpn/server.conf
    fi

    # if they modified protocol put value in server.conf
    if [ "$PROTO" != "udp" ]; then
        $SUDO sed -i "s/proto udp/proto tcp/g" /etc/openvpn/server.conf
    fi

    # write out server certs to conf file
    $SUDO sed -i "s/\(key \/etc\/openvpn\/easy-rsa\/pki\/private\/\).*/\1${SERVER_NAME}.key/" /etc/openvpn/server.conf
    $SUDO sed -i "s/\(cert \/etc\/openvpn\/easy-rsa\/pki\/issued\/\).*/\1${SERVER_NAME}.crt/" /etc/openvpn/server.conf
}

confUnattendedUpgrades() {
    cd /etc/apt/apt.conf.d

    if [[ $UNATTUPG == "unattended-upgrades" ]]; then
        $SUDO apt-get --yes --quiet --no-install-recommends install "$UNATTUPG" > /dev/null & spinner $!
        if [[ $PLAT == "Ubuntu" ]]; then
            # Ubuntu 50unattended-upgrades should already just have security enabled
            # so we just need to configure the 10periodic file
            cat << EOT | $SUDO tee 10periodic >/dev/null
    APT::Periodic::Update-Package-Lists "1";
    APT::Periodic::Download-Upgradeable-Packages "1";
    APT::Periodic::AutocleanInterval "5";
    APT::Periodic::Unattended-Upgrade "1";
EOT
        else
            # Fix Raspbian config
            if [[ $PLAT == "Raspbian" ]]; then
                wget -q -O - "$UNATTUPG_CONFIG" | $SUDO tar xz
                $SUDO cp "unattended-upgrades-$UNATTUPG_RELEASE/data/50unattended-upgrades.Raspbian" 50unattended-upgrades
                $SUDO rm -rf "unattended-upgrades-$UNATTUPG_RELEASE"
            fi

            # Add the remaining settings for all other distributions
            cat << EOT | $SUDO tee 02periodic >/dev/null
    APT::Periodic::Enable "1";
    APT::Periodic::Update-Package-Lists "1";
    APT::Periodic::Download-Upgradeable-Packages "1";
    APT::Periodic::Unattended-Upgrade "1";
    APT::Periodic::AutocleanInterval "7";
    APT::Periodic::Verbose "0";
EOT
        fi
    fi

}

confNetwork() {
    # Enable forwarding of internet traffic
    $SUDO sed -i '/net.ipv4.ip_forward=1/s/^#//g' /etc/sysctl.conf
    $SUDO sysctl -p

    # if ufw enabled, configure that (running as root because sometimes the executable is not in the user's $PATH, on Debian for example)
    if $SUDO bash -c 'hash ufw' 2>/dev/null; then
        if LANG=en_US.UTF-8 $SUDO ufw status | grep -q inactive
        then
            noUFW=1
        else
            echo "::: Detected UFW is enabled."
            echo "::: Adding UFW rules..."
            $SUDO sed "/delete these required/i *nat\n:POSTROUTING ACCEPT [0:0]\n-I POSTROUTING -s <IP_ADDRESS>/24 -o $IPv4dev -j MASQUERADE\nCOMMIT\n" -i /etc/ufw/before.rules
            # Insert rules at the beginning of the chain (in case there are other rules that may drop the traffic)
            $SUDO ufw insert 1 allow "$PORT"/"$PROTO" >/dev/null
            # Don't forward everything, just the traffic originated from the VPN subnet
            $SUDO ufw route insert 1 allow in on tun0 from <IP_ADDRESS>/24 out on "$IPv4dev" to any >/dev/null
            $SUDO ufw reload >/dev/null
            echo "::: UFW configuration completed."
        fi
    else
        noUFW=1
    fi
    # else configure iptables
    if [[ $noUFW -eq 1 ]]; then
        echo 1 > /tmp/noUFW
        $SUDO iptables -t nat -I POSTROUTING -s <IP_ADDRESS>/24 -o "$IPv4dev" -j MASQUERADE
        case ${PLAT} in
            Ubuntu|Debian|Devuan)
                $SUDO iptables-save | $SUDO tee /etc/iptables/rules.v4 > /dev/null
                ;;
            *)
                $SUDO netfilter-persistent save
                ;;
        esac
    else
        echo 0 > /tmp/noUFW
    fi

    $SUDO cp /tmp/noUFW /etc/pivpn/NO_UFW
}

confOVPN() {
    if ! IPv4pub=$(dig +short myip.opendns.com @<IP_ADDRESS>) || ! valid_ip "$IPv4pub"; then
        echo "dig failed, now trying to curl checkip.amazonaws.com"
        if ! IPv4pub=$(curl -s https://checkip.amazonaws.com) || ! valid_ip "$IPv4pub"; then
            echo "checkip.amazonaws.com failed, please check your internet connection/DNS"
            exit 1
        fi
    fi
    $SUDO cp /tmp/pivpnUSR /etc/pivpn/INSTALL_USER
    $SUDO cp /tmp/DET_PLATFORM /etc/pivpn/DET_PLATFORM

    $SUDO cp /etc/.pivpn/Default.txt /etc/openvpn/easy-rsa/pki/Default.txt

    if [[ ${APPLY_TWO_POINT_FOUR} == true ]]; then
      #If they enabled 2.4 remove key-direction options since it's not required
      $SUDO sed -i "/key-direction 1/d" /etc/openvpn/easy-rsa/pki/Default.txt
    fi

    if [[ ${useUpdateVars} == false ]]; then
        METH=$(whiptail --title "Public IP or DNS" --radiolist "Will clients use a Public IP or DNS Name to connect to your server (press space to select)?" ${r} ${c} 2 \
        "$IPv4pub" "Use this public IP" "ON" \
        "DNS Entry" "Use a public DNS" "OFF" 3>&1 1>&2 2>&3)

        exitstatus=$?
        if [ $exitstatus != 0 ]; then
            echo "::: Cancel selected. Exiting..."
            exit 1
        fi

        if [ "$METH" == "$IPv4pub" ]; then
            $SUDO sed -i 's/IPv4pub/'"$IPv4pub"'/' /etc/openvpn/easy-rsa/pki/Default.txt
        else
            until [[ $publicDNSCorrect = True ]]
            do
                PUBLICDNS=$(whiptail --title "PiVPN Setup" --inputbox "What is the public DNS name of this Server?" ${r} ${c} 3>&1 1>&2 2>&3)
                exitstatus=$?
                if [ $exitstatus != 0 ]; then
                echo "::: Cancel selected. Exiting..."
                exit 1
                fi
                if (whiptail --backtitle "Confirm DNS Name" --title "Confirm DNS Name" --yesno "Is this correct?\n\n Public DNS Name:  $PUBLICDNS" ${r} ${c}) then
                    publicDNSCorrect=True
                    $SUDO sed -i 's/IPv4pub/'"$PUBLICDNS"'/' /etc/openvpn/easy-rsa/pki/Default.txt
                else
                    publicDNSCorrect=False
                fi
            done
        fi
    else
        $SUDO sed -i 's/IPv4pub/'"$PUBLICDNS"'/' /etc/openvpn/easy-rsa/pki/Default.txt
    fi

    # if they modified port put value in Default.txt for clients to use
    if [ $PORT != 1194 ]; then
        $SUDO sed -i -e "s/1194/${PORT}/g" /etc/openvpn/easy-rsa/pki/Default.txt
    fi

    # if they modified protocol put value in Default.txt for clients to use
    if [ "$PROTO" != "udp" ]; then
        $SUDO sed -i -e "s/proto udp/proto tcp/g" /etc/openvpn/easy-rsa/pki/Default.txt
    fi

    # verify server name to strengthen security
    $SUDO sed -i "s/SRVRNAME/${SERVER_NAME}/" /etc/openvpn/easy-rsa/pki/Default.txt

    if [ ! -d "/home/$pivpnUser/ovpns" ]; then
        $SUDO mkdir "/home/$pivpnUser/ovpns"
    fi
    $SUDO chmod 0777 -R "/home/$pivpnUser/ovpns"
}

confLogging() {
  echo "if \$programname == 'ovpn-server' then /var/log/openvpn.log
if \$programname == 'ovpn-server' then stop" | $SUDO tee /etc/rsyslog.d/30-openvpn.conf > /dev/null

  echo "/var/log/openvpn.log
{
    rotate 4
    weekly
    missingok
    notifempty
    compress
    delaycompress
    sharedscripts
    postrotate
        invoke-rc.d rsyslog rotate >/dev/null 2>&1 || true
    endscript
}" | $SUDO tee /etc/logrotate.d/openvpn > /dev/null

  # Restart the logging service
  case ${PLAT} in
    Ubuntu|Debian|*vuan)
      $SUDO service rsyslog restart || true
      ;;
    *)
      $SUDO systemctl restart rsyslog.service || true
      ;;
  esac
}

finalExports() {
    # Update variables in setupVars.conf file
    if [ -e "${setupVars}" ]; then
        $SUDO sed -i.update.bak '/pivpnUser/d;/UNATTUPG/d;/pivpnInterface/d;/IPv4dns/d;/IPv4addr/d;/IPv4gw/d;/pivpnProto/d;/PORT/d;/ENCRYPT/d;/DOWNLOAD_DH_PARAM/d;/PUBLICDNS/d;/OVPNDNS1/d;/OVPNDNS2/d;' "${setupVars}"
    fi
    {
        echo "pivpnUser=${pivpnUser}"
        echo "UNATTUPG=${UNATTUPG}"
        echo "pivpnInterface=${pivpnInterface}"
        echo "IPv4dns=${IPv4dns}"
        echo "IPv4addr=${IPv4addr}"
        echo "IPv4gw=${IPv4gw}"
        echo "pivpnProto=${pivpnProto}"
        echo "PORT=${PORT}"
        echo "ENCRYPT=${ENCRYPT}"
        echo "APPLY_TWO_POINT_FOUR=${APPLY_TWO_POINT_FOUR}"
        echo "DOWNLOAD_DH_PARAM=${DOWNLOAD_DH_PARAM}"
        echo "PUBLICDNS=${PUBLICDNS}"
        echo "OVPNDNS1=${OVPNDNS1}"
        echo "OVPNDNS2=${OVPNDNS2}"
    } | $SUDO tee "${setupVars}" > /dev/null
}

# I suggest replacing some of these names.

#accountForRefactor() {
#    # At some point in the future this list can be pruned, for now we'll need it to ensure updates don't break.
#
#    # Refactoring of install script has changed the name of a couple of variables.  Is this always OK? Also if you only select one DNS server ??
    $SUDO sed -i '0,/\(dhcp-option DNS \)/ s/\(dhcp-option DNS \).*/\1'${OVPNDNS1}'\"/' /etc/openvpn/server.conf
    $SUDO sed -i '0,/\(dhcp-option DNS \)/! s/\(dhcp-option DNS \).*/\1'${OVPNDNS2}'\"/' /etc/openvpn/server.conf

    finalExports #re-export setupVars.conf to account for any new vars added in new versions
}

displayFinalMessage() {
    # Final completion message to user
    whiptail --msgbox --backtitle "Make it so." --title "Installation Complete!" "Now run 'pivpn add' to create the ovpn profiles.
Run 'pivpn help' to see what else you can do!" ${r} ${c}
    if (whiptail --title "Reboot" --yesno --defaultno "It is strongly recommended you reboot after installation.  Would you like to reboot now?" ${r} ${c}); then
        whiptail --title "Rebooting" --msgbox "The system will now reboot." ${r} ${c}
        printf "\nRebooting system...\n"
        $SUDO sleep 3
        $SUDO shutdown -r now
    fi
}

update_dialogs() {
    # reconfigure
    if [ "${reconfigure}" = true ]; then
        opt1a="Repair"
        opt1b="This will retain existing settings"
        strAdd="You will remain on the same version"
    else
        opt1a="Update"
        opt1b="This will retain existing settings."
        strAdd="You will be updated to the latest version."
    fi
    opt2a="Reconfigure"
    opt2b="This will allow you to enter new settings"

    UpdateCmd=$(whiptail --title "Existing Install Detected!" --menu "\n\nWe have detected an existing install.\n\nPlease choose from the following options: \n($strAdd)" ${r} ${c} 2 \
    "${opt1a}"  "${opt1b}" \
    "${opt2a}"  "${opt2b}" 3>&2 2>&1 1>&3) || \
    { echo "::: Cancel selected. Exiting"; exit 1; }

    case ${UpdateCmd} in
        ${opt1a})
            echo "::: ${opt1a} option selected."
            useUpdateVars=true
            ;;
        ${opt2a})
            echo "::: ${opt2a} option selected"
            useUpdateVars=false
            ;;
    esac
}

clone_or_update_repos() {
    if [[ "${reconfigure}" == true ]]; then
        echo "::: --reconfigure passed to install script. Not downloading/updating local repos"
    else
        # Get Git files
        getGitFiles ${pivpnFilesDir} ${pivpnGitUrl} || \
        { echo "!!! Unable to clone ${pivpnGitUrl} into ${pivpnFilesDir}, unable to continue."; \
            exit 1; \
        }
    fi
}

######## SCRIPT ############

main() {

    ######## FIRST CHECK ########
    # Must be root to install
    echo ":::"
    if [[ $EUID -eq 0 ]];then
        echo "::: You are root."
    else
        echo "::: sudo will be used for the install."
        # Check if it is actually installed
        # If it isn't, exit because the install cannot complete
        if [[ $(dpkg-query -s sudo) ]];then
            export SUDO="sudo"
            export SUDOE="sudo -E"
        else
            echo "::: Please install sudo or run this as root."
            exit 1
        fi
    fi

    # Check for supported distribution
    distro_check

    # Check arguments for the undocumented flags
    for var in "$@"; do
        case "$var" in
            "--reconfigure"  ) reconfigure=true;;
            "--i_do_not_follow_recommendations"   ) skipSpaceCheck=false;;
            "--unattended"     ) runUnattended=true;;
        esac
    done

    if [[ -f ${setupVars} ]]; then
        if [[ "${runUnattended}" == true ]]; then
            echo "::: --unattended passed to install script, no whiptail dialogs will be displayed"
            useUpdateVars=true
        else
            update_dialogs
        fi
    fi

    # Start the installer
    # Verify there is enough disk space for the install
    if [[ "${skipSpaceCheck}" == true ]]; then
        echo "::: --i_do_not_follow_recommendations passed to script, skipping free disk space verification!"
    else
        verifyFreeDiskSpace
    fi

    # Install the packages (we do this first because we need whiptail)
    addSoftwareRepo

    update_package_cache

    # Notify user of package availability
    notify_package_updates_available

    # Install packages used by this installation script
    install_dependent_packages PIVPN_DEPS[@]

    if [[ ${useUpdateVars} == false ]]; then
        # Display welcome dialogs
        welcomeDialogs

        # Find interfaces and let the user choose one
        chooseInterface

        # Only try to set static on Raspbian, otherwise let user do it
        if [[ $PLAT != "Raspbian" ]]; then
            avoidStaticIPv4Ubuntu
        else
            getStaticIPv4Settings
            setStaticIPv4
        fi

        # Choose the user for the ovpns
        chooseUser

        # Ask if unattended-upgrades will be enabled
        unattendedUpgrades

        # Clone/Update the repos
        clone_or_update_repos

        # Install
        if installPiVPN; then
            echo "::: Install Complete..."
        else
            exit 1
        fi
    else
        # Source ${setupVars} for use in the rest of the functions.
        source ${setupVars}

        echo "::: Using IP address: $IPv4addr"
        echo "${IPv4addr%/*}" > /tmp/pivpnIP
        echo "::: Using interface: $pivpnInterface"
        echo "${pivpnInterface}" > /tmp/pivpnINT
        echo "::: Using User: $pivpnUser"
        echo "${pivpnUser}" > /tmp/pivpnUSR
        echo "::: Using protocol: $pivpnProto"
        echo "${pivpnProto}" > /tmp/pivpnPROTO
        echo "::: Using port: $PORT"
        echo ${PORT} > /tmp/INSTALL_PORT
        echo ":::"

        # Only try to set static on Raspbian
        case ${PLAT} in
          Rasp*)
            setStaticIPv4 # This might be a problem if a user tries to modify the ip in the config file and then runs an update because of the way we check for previous configuration in /etc/dhcpcd.conf
            ;;
          *)
            echo "::: IP Information"
            echo "::: Since we think you are not using Raspbian, we will not configure a static IP for you."
            echo "::: If you are in Amazon then you can not configure a static IP anyway."
            echo "::: Just ensure before this installer started you had set an elastic IP on your instance."
            ;;
          esac

        # Clone/Update the repos
        clone_or_update_repos

        updatePiVPN
    fi

    echo "::: Restarting services..."
    # Start services
    case ${PLAT} in
        Ubuntu|Debian|*vuan)
            $SUDO service openvpn start
            ;;
        *)
            $SUDO systemctl enable openvpn.service
            $SUDO systemctl start openvpn.service
            ;;
    esac

    # Ensure that cached writes reach persistent storage
    echo "::: Flushing writes to disk..."
    sync

    echo "::: done."

    if [[ "${useUpdateVars}" == false ]]; then
        displayFinalMessage
    fi

    echo ":::"
    if [[ "${useUpdateVars}" == false ]]; then
        echo "::: Installation Complete!"
        echo "::: Now run 'pivpn add' to create an ovpn profile for each of your devices."
        echo "::: Run 'pivpn help' to see what else you can do!"
        echo "::: It is strongly recommended you reboot after installation."
    else
        echo "::: Update complete!"
    fi

    echo ":::"
}

if [[ "${PIVPN_TEST}" != true ]] ; then
  main "$@"
fi
+ [[ '' != true ]]
+ main
+ echo :::
:::
+ [[ 1000 -eq 0 ]]
+ echo '::: sudo will be used for the install.'
::: sudo will be used for the install.
dpkg-query -s sudo
++ dpkg-query -s sudo
+ [[ -n Package: sudo
Status: install ok installed
Priority: optional
Section: admin
Installed-Size: 2283
Maintainer: Bdale Garbee<EMAIL_ADDRESS>Architecture: armhf
Version: 1.8.10p3-1+deb8u5
Replaces: sudo-ldap
Depends: libaudit1 (>= 1:2.2.1), libc6 (>= 2.17), libpam0g (>= <IP_ADDRESS>), libselinux1 (>= 1.32), libpam-modules
Conflicts: sudo-ldap
Conffiles:
 /etc/init.d/sudo 69497d0565055f626ee2bc84f818ce0f
 /etc/pam.d/sudo 85da64f888739f193fc0fa896680030e
 /etc/sudoers 45437b4e86fba2ab890ac81db2ec3606
 /etc/sudoers.d/README 8d3cf36d1713f40a0ddc38e1b21a51b6
Description: Provide limited super user privileges to specific users
 Sudo is a program designed to allow a sysadmin to give limited root
 privileges to users and log root activity.  The basic philosophy is to give
 as few privileges as possible but still allow people to get their work done.
 .
 This version is built with minimal shared library dependencies, use the
 sudo-ldap package instead if you need LDAP support for sudoers.
Homepage: http://www.sudo.ws/ ]]
+ export SUDO=sudo
+ SUDO=sudo
+ export 'SUDOE=sudo -E'
+ SUDOE='sudo -E'
+ distro_check
+ hash lsb_release
lsb_release -si
++ lsb_release -si
+ PLAT=Raspbian
lsb_release -sc
++ lsb_release -sc
+ OSCN=jessie
+ [[ jessie != \b\i\o\n\i\c ]]
+ PIVPN_DEPS+=(dhcpcd5)
+ case ${PLAT} in
+ case ${OSCN} in
+ echo Raspbian
+ [[ -f /etc/pivpn/setupVars.conf ]]
+ [[ false == true ]]
+ update_dialogs
+ '[' false = true ']'
+ opt1a=Update
+ opt1b='This will retain existing settings.'
+ strAdd='You will be updated to the latest version.'
+ opt2a=Reconfigure
+ opt2b='This will allow you to enter new settings'
whiptail --title "Existing Install Detected!" --menu "\n\nWe have detected an existing install.\n\nPlease choose from the following options: \n($strAdd)" ${r} ${c} 2     "${opt1a}"  "${opt1b}"     "${opt2a}"  "${opt2b}" 3>&2 2>&1 1>&3
++ whiptail --title 'Existing Install Detected!' --menu '\n\nWe have detected an existing install.\n\nPlease choose from the following options: \n(You will be updated to the latest version.)' 21 70 2 Update 'This will retain existing settings.' Reconfigure 'This will allow you to enter new settings'
+ UpdateCmd=Reconfigure
+ case ${UpdateCmd} in
+ echo '::: Reconfigure option selected'
::: Reconfigure option selected
+ useUpdateVars=false
+ [[ false == true ]]
+ verifyFreeDiskSpace
+ echo '::: Verifying free disk space...'
::: Verifying free disk space...
+ local required_free_kilobytes=76800
df -Pk | grep -m1 '\/$' | awk '{print $4}'
++ df -Pk
++ grep -m1 '\/$'
++ awk '{print $4}'
+ local existing_free_kilobytes=2817992
+ [[ 2817992 =~ ^([0-9])+$ ]]
+ [[ 2817992 -lt 76800 ]]
+ addSoftwareRepo
+ case ${PLAT} in
+ update_package_cache
stat -c %Y ${PKG_CACHE}
++ stat -c %Y /var/lib/apt/lists/
+ timestamp=1558009551
date -d @"${timestamp}" "+%b %e"
++ date -d @1558009551 '+%b %e'
+ timestampAsDate='May 16'
date "+%b %e"
++ date '+%b %e'
+ today='May 16'
+ '[' '!' 'May 16' == 'May 16' ']'
+ notify_package_updates_available
+ echo :::
:::
+ echo -n '::: Checking apt-get for upgraded packages....'
::: Checking apt-get for upgraded packages....eval "${PKG_COUNT}"
++ eval 'apt-get -s -o Debug::NoLocking=true upgrade | grep -c ^Inst || true'
apt-get -s -o Debug::NoLocking=true upgrade | grep -c ^Inst || true
+++ apt-get -s -o Debug::NoLocking=true upgrade
+++ grep -c '^Inst'
+++ true
+ updatesToInstall=0
+ echo ' done!'
 done!
+ echo :::
:::
+ [[ 0 -eq 0 ]]
+ echo '::: Your system is up to date! Continuing with PiVPN installation...'
::: Your system is up to date! Continuing with PiVPN installation...
+ install_dependent_packages 'PIVPN_DEPS[@]'
+ argArray1=("${!1}")
+ declare -a argArray1
+ echo iptables-persistent iptables-persistent/autosave_v4 boolean true
+ sudo debconf-set-selections
+ echo iptables-persistent iptables-persistent/autosave_v6 boolean false
+ sudo debconf-set-selections
+ grep -q https /etc/apt/sources.list
+ command -v debconf-apt-progress
+ sudo debconf-apt-progress -- apt-get --yes --no-install-recommends install openvpn git tar wget grep iptables-persistent dnsutils expect whiptail net-tools dhcpcd5
+ [[ false == false ]]
+ welcomeDialogs
+ whiptail --msgbox --backtitle Welcome --title 'PiVPN Automated Installer' 'This installer will transform your Raspberry Pi into an OpenVPN server!' 21 70
+ whiptail --msgbox --backtitle 'Initiating network interface' --title 'Static IP Needed' 'The PiVPN is a SERVER so it needs a STATIC IP ADDRESS to function properly.

In the next section, you can choose to use your current network settings (DHCP) or to manually edit them.' 21 70
+ chooseInterface
+ interfacesArray=()
+ local interfacesArray
+ local interfaceCount
+ local chooseInterfaceCmd
+ local chooseInterfaceOptions
+ local firstloop=1
echo "${availableInterfaces}" | wc -l
++ echo 'eth0
wlan0'
++ wc -l
+ [[ 2 -eq 1 ]]
+ read -r line
+ mode=OFF
+ [[ 1 -eq 1 ]]
+ firstloop=0
+ mode=ON
+ interfacesArray+=("${line}" "available" "${mode}")
+ read -r line
+ mode=OFF
+ [[ 0 -eq 1 ]]
+ interfacesArray+=("${line}" "available" "${mode}")
+ read -r line
echo "${availableInterfaces}" | wc -l
++ wc -l
++ echo 'eth0
wlan0'
+ interfaceCount=2
+ chooseInterfaceCmd=(whiptail --separate-output --radiolist "Choose An Interface (press space to select):" ${r} ${c} ${interfaceCount})
"${chooseInterfaceCmd[@]}" "${interfacesArray[@]}" 2>&1 >/dev/tty
++ whiptail --separate-output --radiolist 'Choose An Interface (press space to select):' 21 70 2 eth0 available ON wlan0 available OFF
+ chooseInterfaceOptions=wlan0
+ [[ 0 = 0 ]]
+ for desiredInterface in '${chooseInterfaceOptions}'
+ pivpnInterface=wlan0
+ echo '::: Using interface: wlan0'
::: Using interface: wlan0
+ echo wlan0
+ [[ Raspbian != \R\a\s\p\b\i\a\n ]]
+ getStaticIPv4Settings
+ local ipSettingsCorrect
nslookup <IP_ADDRESS> | grep Server: | awk '{print $2}'
++ nslookup <IP_ADDRESS>
++ grep Server:
++ awk '{print $2}'
+ IPv4dns=<IP_ADDRESS>
+ whiptail --backtitle 'Calibrating network interface' --title 'Static IP Address' --yesno 'Do you want to use your current network settings as a static address?
                    IP address:    <IP_ADDRESS>
                    Gateway:       <IP_ADDRESS>' 21 70
+ whiptail --msgbox --backtitle 'IP information' --title 'FYI: IP Conflict' 'It is possible your router could still try to assign this IP to a device, which would cause a conflict.  But in most cases the router is smart enough to not do that.
If you are worried, either manually set the address, or modify the DHCP reservation pool so it does not include the IP you want.
It is also possible to use a DHCP reservation, but if you are going to do that, you might as well set a static address.' 21 70
+ setStaticIPv4
+ [[ -f /etc/dhcpcd.conf ]]
+ grep -q <IP_ADDRESS> /etc/dhcpcd.conf
+ echo '::: Static IP already configured.'
::: Static IP already configured.
+ chooseUser
+ whiptail --msgbox --backtitle 'Parsing User List' --title 'Local Users' 'Choose a local user that will hold your ovpn configurations.' 21 70
awk -F':' 'BEGIN {count=0} $3>=1000 && $3<=60000 { count++ } END{ print count }' /etc/passwd
++ awk -F: 'BEGIN {count=0} $3>=1000 && $3<=60000 { count++ } END{ print count }' /etc/passwd
+ numUsers=1
+ '[' 1 -eq 0 ']'
awk -F':' '$3>=1000 && $3<=60000 {print $1}' /etc/passwd
++ awk -F: '$3>=1000 && $3<=60000 {print $1}' /etc/passwd
+ availableUsers=pi
+ userArray=()
+ local userArray
+ local firstloop=1
+ read -r line
+ mode=OFF
+ [[ 1 -eq 1 ]]
+ firstloop=0
+ mode=ON
+ userArray+=("${line}" "" "${mode}")
+ read -r line
+ chooseUserCmd=(whiptail --title "Choose A User" --separate-output --radiolist "Choose (press space to select):" ${r} ${c} ${numUsers})
"${chooseUserCmd[@]}" "${userArray[@]}" 2>&1 >/dev/tty
++ whiptail --title 'Choose A User' --separate-output --radiolist 'Choose (press space to select):' 21 70 1 pi '' ON
+ chooseUserOptions=pi
+ [[ 0 = 0 ]]
+ for desiredUser in '${chooseUserOptions}'
+ pivpnUser=pi
+ echo '::: Using User: pi'
::: Using User: pi
+ echo pi
+ unattendedUpgrades
+ whiptail --msgbox --backtitle 'Security Updates' --title 'Unattended Upgrades' 'Since this server will have at least one port open to the internet, it is recommended you enable unattended-upgrades.\nThis feature will check daily for security package updates only and apply them when necessary.\nIt will NOT automatically reboot the server so to fully apply some updates you should periodically reboot.' 21 70
+ whiptail --backtitle 'Security Updates' --title 'Unattended Upgrades' --yesno 'Do you want to enable unattended upgrades of security patches to this server?' 21 70
+ UNATTUPG=unattended-upgrades
+ clone_or_update_repos
+ [[ false == true ]]
+ getGitFiles /etc/.pivpn https://github.com/pivpn/pivpn.git
+ echo :::
:::
+ echo '::: Checking for existing base files...'
::: Checking for existing base files...
+ is_repo /etc/.pivpn
+ echo -n ':::    Checking /etc/.pivpn is a repo...'
:::    Checking /etc/.pivpn is a repo...+ cd /etc/.pivpn
+ return 1
+ make_repo /etc/.pivpn https://github.com/pivpn/pivpn.git
+ echo -n ':::    Cloning https://github.com/pivpn/pivpn.git into /etc/.pivpn...'
:::    Cloning https://github.com/pivpn/pivpn.git into /etc/.pivpn...+ sudo rm -rf /etc/.pivpn
+ spinner 4663
+ local pid=4663
+ local delay=0.50
+ local 'spinstr=/-\|'
+ sudo git clone -q --depth 1 --no-single-branch https://github.com/pivpn/pivpn.git /etc/.pivpn
ps a | awk '{print $1}' | grep "${pid}"
++ ps a
++ awk '{print $1}'
++ grep 4663
+ '[' 4663 ']'
+ local 'temp=-\|'
+ printf ' [%c]  ' '/-\|'
 [/]  + local 'spinstr=-\|/'
+ sleep 0.50
+ printf '\b\b\b\b\b\b'                                                   ps a | awk '{print $1}' | grep "${pid}"
++ ps a
++ grep 4663
++ awk '{print $1}'
+ '[' 4663 ']'
+ local 'temp=\|/'
+ printf ' [%c]  ' '-\|/'
 [-]  + local 'spinstr=\|/-'
+ sleep 0.50
+ printf '\b\b\b\b\b\b'                                                   ps a | awk '{print $1}' | grep "${pid}"
++ ps a
++ awk '{print $1}'
++ grep 4663
+ '[' 4663 ']'
+ local 'temp=|/-'
+ printf ' [%c]  ' '\|/-'
 [\]  + local 'spinstr=|/-\'
+ sleep 0.50
+ printf '\b\b\b\b\b\b'                                                   ps a | awk '{print $1}' | grep "${pid}"
++ ps a
++ grep 4663
++ awk '{print $1}'
+ '[' 4663 ']'
+ local 'temp=/-\'
+ printf ' [%c]  ' '|/-\'
 [|]  + local 'spinstr=/-\|'
+ sleep 0.50
+ printf '\b\b\b\b\b\b'                                                   ps a | awk '{print $1}' | grep "${pid}"
++ ps a
++ grep 4663
++ awk '{print $1}'
+ '[' 4663 ']'
+ local 'temp=-\|'
+ printf ' [%c]  ' '/-\|'
 [/]  + local 'spinstr=-\|/'
+ sleep 0.50
+ printf '\b\b\b\b\b\b'                                                   ps a | awk '{print $1}' | grep "${pid}"
++ ps a
++ grep 4663
++ awk '{print $1}'
+ '[' 4663 ']'
+ local 'temp=\|/'
+ printf ' [%c]  ' '-\|/'
 [-]  + local 'spinstr=\|/-'
+ sleep 0.50
+ printf '\b\b\b\b\b\b'                                                   ps a | awk '{print $1}' | grep "${pid}"
++ ps a
++ awk '{print $1}'
++ grep 4663
+ '[' '' ']'
+ printf '    \b\b\b\b'
+ '[' -z '' ']'
+ :
+ echo ' done!'
 done!
+ installPiVPN
+ stopServices
+ sudo echo :::
:::
+ sudo echo -n '::: Stopping OpenVPN service...'
::: Stopping OpenVPN service...+ case ${PLAT} in
+ sudo systemctl stop openvpn.service
+ sudo echo ' done.'
 done.
+ sudo mkdir -p /etc/pivpn/
+ confUnattendedUpgrades
+ cd /etc/apt/apt.conf.d
+ [[ unattended-upgrades == \u\n\a\t\t\e\n\d\e\d\-\u\p\g\r\a\d\e\s ]]
+ spinner 4763
+ local pid=4763
+ local delay=0.50
+ local 'spinstr=/-\|'
+ sudo apt-get --yes --quiet --no-install-recommends install unattended-upgrades
ps a | awk '{print $1}' | grep "${pid}"
++ ps a
++ awk '{print $1}'
++ grep 4763
+ '[' 4763 ']'
+ local 'temp=-\|'
+ printf ' [%c]  ' '/-\|'
 [/]  + local 'spinstr=-\|/'
+ sleep 0.50
+ printf '\b\b\b\b\b\b'                                                   ps a | awk '{print $1}' | grep "${pid}"
++ awk '{print $1}'
++ grep 4763
++ ps a
+ '[' 4763 ']'
+ local 'temp=\|/'
+ printf ' [%c]  ' '-\|/'
 [-]  + local 'spinstr=\|/-'
+ sleep 0.50
+ printf '\b\b\b\b\b\b'                                                   ps a | awk '{print $1}' | grep "${pid}"
++ ps a
++ grep 4763
++ awk '{print $1}'
+ '[' 4763 ']'
+ local 'temp=|/-'
+ printf ' [%c]  ' '\|/-'
 [\]  + local 'spinstr=|/-\'
+ sleep 0.50
+ printf '\b\b\b\b\b\b'                                                   ps a | awk '{print $1}' | grep "${pid}"
++ ps a
++ grep 4763
++ awk '{print $1}'
+ '[' 4763 ']'
+ local 'temp=/-\'
+ printf ' [%c]  ' '|/-\'
 [|]  + local 'spinstr=/-\|'
+ sleep 0.50
+ printf '\b\b\b\b\b\b'                                                   ps a | awk '{print $1}' | grep "${pid}"
++ ps a
++ awk '{print $1}'
++ grep 4763
+ '[' 4763 ']'
+ local 'temp=-\|'
+ printf ' [%c]  ' '/-\|'
 [/]  + local 'spinstr=-\|/'
+ sleep 0.50
+ printf '\b\b\b\b\b\b'                                                   ps a | awk '{print $1}' | grep "${pid}"
++ ps a
++ grep 4763
++ awk '{print $1}'
+ '[' 4763 ']'
+ local 'temp=\|/'
+ printf ' [%c]  ' '-\|/'
 [-]  + local 'spinstr=\|/-'
+ sleep 0.50
+ printf '\b\b\b\b\b\b'                                                   ps a | awk '{print $1}' | grep "${pid}"
++ ps a
++ awk '{print $1}'
++ grep 4763
+ '[' 4763 ']'
+ local 'temp=|/-'
+ printf ' [%c]  ' '\|/-'
 [\]  + local 'spinstr=|/-\'
+ sleep 0.50
+ printf '\b\b\b\b\b\b'                                                   ps a | awk '{print $1}' | grep "${pid}"
++ ps a
++ grep 4763
++ awk '{print $1}'
+ '[' '' ']'
+ printf '    \b\b\b\b'
+ [[ Raspbian == \U\b\u\n\t\u ]]
+ [[ Raspbian == \R\a\s\p\b\i\a\n ]]
+ wget -q -O - https://github.com/mvo5/unattended-upgrades/archive/1.9.tar.gz
+ sudo tar xz
+ sudo cp unattended-upgrades-1.9/data/50unattended-upgrades.Raspbian 50unattended-upgrades
+ sudo rm -rf unattended-upgrades-1.9
+ cat
+ sudo tee 02periodic
+ installScripts
+ sudo echo :::
:::
+ sudo echo -n '::: Installing scripts to /opt/pivpn...'
::: Installing scripts to /opt/pivpn...+ '[' '!' -d /opt/pivpn ']'
+ sudo cp /etc/.pivpn/scripts/makeOVPN.sh /opt/pivpn/makeOVPN.sh
+ sudo cp /etc/.pivpn/scripts/clientStat.sh /opt/pivpn/clientStat.sh
+ sudo cp /etc/.pivpn/scripts/listOVPN.sh /opt/pivpn/listOVPN.sh
+ sudo cp /etc/.pivpn/scripts/removeOVPN.sh /opt/pivpn/removeOVPN.sh
+ sudo cp /etc/.pivpn/scripts/uninstall.sh /opt/pivpn/uninstall.sh
+ sudo cp /etc/.pivpn/scripts/pivpnDebug.sh /opt/pivpn/pivpnDebug.sh
+ sudo chmod 0755 /opt/pivpn/makeOVPN.sh /opt/pivpn/clientStat.sh /opt/pivpn/listOVPN.sh /opt/pivpn/removeOVPN.sh /opt/pivpn/uninstall.sh /opt/pivpn/pivpnDebug.sh
+ sudo cp /etc/.pivpn/pivpn /usr/local/bin/pivpn
+ sudo chmod 0755 /usr/local/bin/pivpn
+ sudo cp /etc/.pivpn/scripts/bash-completion /etc/bash_completion.d/pivpn
+ . /etc/bash_completion.d/pivpn
_pivpn()
{
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    dashopts="-a -c -d -l -r -h -u"
    opts="debug add clients list revoke uninstall help"
    if [ "${#COMP_WORDS[@]}" -eq 2 ]
    then
        if [[ ${cur} == -* ]] ; then
            COMPREPLY=( $(compgen -W "${dashopts}" -- "${cur}") )
        else
            COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
        fi
    elif [[ ( "$prev" == "add" || "$prev" == "-a" ) && "${#COMP_WORDS[@]}" -eq 3 ]]
    then
        COMPREPLY=( $(compgen -W "nopass" -- "${cur}") )
    fi
    return 0
}
complete -F _pivpn pivpn
++ complete -F _pivpn pivpn
+ sudo cp /tmp/pivpnINT /etc/pivpn/pivpnINTERFACE
+ sudo echo ' done.'
 done.
+ setCustomProto
whiptail --title "Protocol" --radiolist   "Choose a protocol (press space to select). Please only choose TCP if you know why you need TCP."  Please only choose TCP if you know why you need TCP.' 21 70 2 UDP '' ON TCP '' OFF
+ protocol=TCP
+ pivpnProto=tcp
+ echo '::: Using protocol: tcp'
::: Using protocol: tcp
+ echo tcp
+ PROTO=tcp
+ sudo cp /tmp/pivpnPROTO /etc/pivpn/INSTALL_PROTO
+ setCustomPort
+ [[ '' = True ]]
+ portInvalid=Invalid
cat /etc/pivpn/INSTALL_PROTO
++ cat /etc/pivpn/INSTALL_PROTO
+ PROTO=tcp
+ '[' tcp = udp ']'
+ DEFAULT_PORT=443
whiptail --title "Default OpenVPN Port" --inputbox "You can modify the default OpenVPN port. \nEnter a new value or hit 'Enter' to retain the default" ${r} ${c} $DEFAULT_PORT 3>&1 1>&2 2>&3
++ whiptail --title 'Default OpenVPN Port' --inputbox 'You can modify the default OpenVPN port. \nEnter a new value or hit '\''Enter'\'' to retain the default' 21 70 443
+ PORT=443
+ [[ 443 =~ ^[0-9]+$ ]]
+ '[' 443 -ge 1 ']'
+ '[' 443 -le 65535 ']'
+ :
+ [[ 443 == \I\n\v\a\l\i\d ]]
+ whiptail --backtitle 'Specify Custom Port' --title 'Confirm Custom Port Number' --yesno 'Are these settings correct?\n    PORT:   443' 21 70
+ PORTNumCorrect=True
+ [[ True = True ]]
+ echo 443
+ sudo cp /tmp/INSTALL_PORT /etc/pivpn/INSTALL_PORT
+ confOpenVPN
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1
++ cat /dev/urandom
++ tr -dc a-zA-Z0-9
++ head -n 1
++ fold -w 16
+ NEW_UUID=AqTc1WmHMWPOMdim
+ SERVER_NAME=server_AqTc1WmHMWPOMdim
+ [[ false == false ]]
+ [[ false == false ]]
+ [[ Raspbian == \R\a\s\p\b\i\a\n ]]
+ [[ jessie != \s\t\r\e\t\c\h ]]
+ APPLY_TWO_POINT_FOUR=false
+ [[ false == true ]]
+ [[ false == false ]]
whiptail --backtitle "Setup OpenVPN" --title "RSA certificate size" --radiolist             "Choose the desired size of your certificate (press space to select):\n   This is a certificate that will be generated on your system.  The larger the certificate, the more time this will take.  For most applications, it is recommended to use 2048 bits.  If you are testing, you can use 1024 bits to speed things up, but do not use this for normal use!  If you are paranoid about ... things... then grab a cup of joe and pick 4096 bits."   The larger the certificate, the more time this will take.  For most applications, it is recommended to use 2048 bits.  If you are testing, you can use 1024 bits to speed things up, but do not use this for normal use!  If you are paranoid about ... things... then grab a cup of joe and pick 4096 bits.' 21 70 3 1024 'Use a 1024-bit certificate (testing only)' OFF 2048 'Use a 2048-bit certificate (recommended level)' ON 4096 'Use a 4096-bit certificate (paranoid level)' OFF
+ ENCRYPT=2048
+ exitstatus=0
+ '[' 0 '!=' 0 ']'
+ [[ -d /etc/openvpn/easy-rsa/ ]]
+ sudo rm -rf /etc/openvpn/easy-rsa/
+ wget -q -O - https://github.com/OpenVPN/easy-rsa/releases/download/v3.0.6/EasyRSA-unix-v3.0.6.tgz
+ sudo tar xz -C /etc/openvpn
+ sudo mv /etc/openvpn/EasyRSA-v3.0.6 /etc/openvpn/easy-rsa
+ sudo chown -R root:root /etc/openvpn/easy-rsa
+ sudo mkdir /etc/openvpn/easy-rsa/pki
+ cd /etc/openvpn/easy-rsa
+ set +e
+ IFS=
+ read -d '' String
+ echo 'if [ -z "$EASYRSA_CALLER" ]; then
    echo "Nope." >&2
    return 1
fi
set_var EASYRSA            "/etc/openvpn/easy-rsa"
set_var EASYRSA_PKI        "$EASYRSA/pki"
set_var EASYRSA_CRL_DAYS   3650
'
+ sudo tee vars
+ set -e
+ [[ false == false ]]
+ echo 'set_var EASYRSA_ALGO       rsa'
+ sudo tee -a vars
set_var EASYRSA_ALGO       rsa
+ echo 'set_var EASYRSA_KEY_SIZE   2048'
+ sudo tee -a vars
set_var EASYRSA_KEY_SIZE   2048
+ sudo -E ./easyrsa --batch init-pki
+ printf '::: Building CA...\n'
::: Building CA...
+ sudo -E ./easyrsa --batch build-ca nopass
Generating RSA private key, 2048 bit long modulus
......................+++
.....................................+++
e is 65537 (0x10001)
+ printf '\n::: CA Complete.\n'

::: CA Complete.
+ [[ false == false ]]
+ [[ false == false ]]
+ whiptail --msgbox --backtitle 'Setup OpenVPN' --title 'Server Information' 'The server key, Diffie-Hellman parameters, and HMAC key will now be generated.' 21 70
+ sudo -E ./easyrsa build-server-full server_AqTc1WmHMWPOMdim nopass

Note: using Easy-RSA configuration from: ./vars

Using SSL: openssl OpenSSL 1.0.1t  3 May 2016
Generating a 2048 bit RSA private key
...............................+++
.................................+++
writing new private key to '/etc/openvpn/easy-rsa/pki/private/server_AqTc1WmHMWPOMdim.key.wnKiE7JlB7'
-----
Using configuration from /etc/openvpn/easy-rsa/pki/safessl-easyrsa.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'server_AqTc1WmHMWPOMdim'
Certificate is to be certified until Apr 30 12:50:16 2022 GMT (1080 days)

Write out database with 1 new entries
Data Base Updated
+ [[ false == false ]]
+ [[ false == false ]]
+ '[' 2048 -ge 4096 ']'
+ DOWNLOAD_DH_PARAM=false
+ [[ false == false ]]
+ '[' 2048 -ge 4096 ']'
+ sudo -E ./easyrsa gen-dh

Note: using Easy-RSA configuration from: ./vars

Using SSL: openssl OpenSSL 1.0.1t  3 May 2016
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
.....+........................................................+..............................+.............................................................................................+...+..................................................................................+...............................................+....................................................+........................................................................................................................+.........................................................................................................................................................+......................................................................................+.........................................................................................................................................................................................................................................................................................................................................+.............................................................................................................................................................+....................................................................................+.....................................................................................................................................................+.......................................................................................................................................................................................................................................................................................................................................................................................+....................................................................................................................................................................................+.........................................................................................................................................................................................................................................+.................................................................................................+................+...............+......................................................................................................................+................+.................................................................+..............................................................................................................................................................................................................................................................................................................+................+...............................................................................................................+............................................................................................+...........................................+..............................................................................................................................................................................................................................................+....+......................................................................................................................................................................................................................................+............................................................................................................................................................+..+............................................................+.............................................................................................................................+................................................................+..+...............................................................................+..........+................................................................................................................+..................................................+.....+.....................................................................................+.......+..............................................................................................................+.....................+.........................................................................................................+.............................................................................................+................................................................................................+.......................+......................................................................................................................................+............................................................^R
..^T...............................................................................................................................................................................................................................................................................+............................................................................................................................................................................................................................................................................................................................................++*++*

DH parameters of size 2048 created at /etc/openvpn/easy-rsa/pki/dh.pem

+ sudo -E mv pki/dh.pem pki/dh2048.pem
+ sudo -E openvpn --genkey --secret pki/ta.key
+ sudo -E ./easyrsa gen-crl

Note: using Easy-RSA configuration from: ./vars

Using SSL: openssl OpenSSL 1.0.1t  3 May 2016
Using configuration from /etc/openvpn/easy-rsa/pki/safessl-easyrsa.cnf

An updated CRL has been created.
CRL file: /etc/openvpn/easy-rsa/pki/crl.pem

+ sudo -E cp pki/crl.pem /etc/openvpn/crl.pem
+ sudo -E chown nobody:nogroup /etc/openvpn/crl.pem
+ sudo cp /etc/.pivpn/server_config.txt /etc/openvpn/server.conf
+ [[ false == true ]]
+ [[ false == true ]]
+ sudo sed -i 's/\(dh \/etc\/openvpn\/easy-rsa\/pki\/dh\).*/\12048.pem/' /etc/openvpn/server.conf
+ '[' 443 '!=' 1194 ']'
+ sudo sed -i s/1194/443/g /etc/openvpn/server.conf
+ '[' tcp '!=' udp ']'
+ sudo sed -i 's/proto udp/proto tcp/g' /etc/openvpn/server.conf
+ sudo sed -i 's/\(key \/etc\/openvpn\/easy-rsa\/pki\/private\/\).*/\1server_AqTc1WmHMWPOMdim.key/' /etc/openvpn/server.conf
+ sudo sed -i 's/\(cert \/etc\/openvpn\/easy-rsa\/pki\/issued\/\).*/\1server_AqTc1WmHMWPOMdim.crt/' /etc/openvpn/server.conf
+ confNetwork
+ sudo sed -i '/net.ipv4.ip_forward=1/s/^#//g' /etc/sysctl.conf
+ sudo sysctl -p
net.ipv4.ip_forward = 1
+ sudo bash -c 'hash ufw'
+ noUFW=1
+ [[ 1 -eq 1 ]]
+ echo 1
+ sudo iptables -t nat -I POSTROUTING -s <IP_ADDRESS>/24 -o wlan0 -j MASQUERADE
+ case ${PLAT} in
+ sudo netfilter-persistent save
run-parts: executing /usr/share/netfilter-persistent/plugins.d/15-ip4tables save
run-parts: executing /usr/share/netfilter-persistent/plugins.d/25-ip6tables save
+ sudo cp /tmp/noUFW /etc/pivpn/NO_UFW
+ confOVPN
dig +short myip.opendns.com @<IP_ADDRESS>
++ dig +short myip.opendns.com @<IP_ADDRESS>
+ IPv4pub=<IP_ADDRESS>
+ valid_ip <IP_ADDRESS>
+ local ip=<IP_ADDRESS>
+ local stat=1
+ [[ <IP_ADDRESS> =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
+ OIFS='
'
+ IFS=.
+ ip=($ip)
+ IFS='
'
+ [[ 182 -le 255 ]]
+ [[ 74 -le 255 ]]
+ [[ 185 -le 255 ]]
+ [[ 114 -le 255 ]]
+ stat=0
+ return 0
+ sudo cp /tmp/pivpnUSR /etc/pivpn/INSTALL_USER
+ sudo cp /tmp/DET_PLATFORM /etc/pivpn/DET_PLATFORM
+ sudo cp /etc/.pivpn/Default.txt /etc/openvpn/easy-rsa/pki/Default.txt
+ [[ false == true ]]
+ [[ false == false ]]
whiptail --title "Public IP or DNS" --radiolist "Will clients use a Public IP or DNS Name to connect to your server (press space to select)?" ${r} ${c} 2         "$IPv4pub" "Use this public IP" "ON"         "DNS Entry" "Use a public DNS" "OFF" 3>&1 1>&2 2>&3
++ whiptail --title 'Public IP or DNS' --radiolist 'Will clients use a Public IP or DNS Name to connect to your server (press space to select)?' 21 70 2 <IP_ADDRESS> 'Use this public IP' ON 'DNS Entry' 'Use a public DNS' OFF
+ METH=<IP_ADDRESS>
+ exitstatus=0
+ '[' 0 '!=' 0 ']'
+ '[' <IP_ADDRESS> == <IP_ADDRESS> ']'
+ sudo sed -i s/IPv4pub/<IP_ADDRESS>/ /etc/openvpn/easy-rsa/pki/Default.txt
+ '[' 443 '!=' 1194 ']'
+ sudo sed -i -e s/1194/443/g /etc/openvpn/easy-rsa/pki/Default.txt
+ '[' tcp '!=' udp ']'
+ sudo sed -i -e 's/proto udp/proto tcp/g' /etc/openvpn/easy-rsa/pki/Default.txt
+ sudo sed -i s/SRVRNAME/server_AqTc1WmHMWPOMdim/ /etc/openvpn/easy-rsa/pki/Default.txt
+ '[' '!' -d /home/pi/ovpns ']'
+ sudo chmod 0777 -R /home/pi/ovpns
+ setClientDNS
+ DNSChoseCmd=(whiptail --separate-output --radiolist "Select the DNS Provider for your VPN Clients (press space to select). To use your own, select Custom."
::: Using Google servers.
+ DNS_MAP=(["Google"]="<IP_ADDRESS> <IP_ADDRESS>" ["OpenDNS"]="<IP_ADDRESS> <IP_ADDRESS>" ["Level3"]="<IP_ADDRESS> <IP_ADDRESS>" ["DNS.WATCH"]="<IP_ADDRESS> <IP_ADDRESS>" ["Norton"]="<IP_ADDRESS> <IP_ADDRESS>" ["FamilyShield"]="<IP_ADDRESS> <IP_ADDRESS>" ["CloudFlare"]="<IP_ADDRESS> <IP_ADDRESS>")
+ declare -A DNS_MAP
awk '{print $1}' <<< "${DNS_MAP["${DNSchoices}"]}"
++ awk '{print $1}'
+ OVPNDNS1=<IP_ADDRESS>
awk '{print $2}' <<< "${DNS_MAP["${DNSchoices}"]}"
++ awk '{print $2}'
+ OVPNDNS2=<IP_ADDRESS>
+ sudo sed -i '0,/\(dhcp-option DNS \)/ s/\(dhcp-option DNS \).*/\<IP_ADDRESS>\"/' /etc/openvpn/server.conf
+ sudo sed -i '0,/\(dhcp-option DNS \)/! s/\(dhcp-option DNS \).*/\<IP_ADDRESS>\"/' /etc/openvpn/server.conf
+ setCustomDomain
+ DomainSettingsCorrect=False
+ whiptail --backtitle 'Custom Search Domain' --title 'Custom Search Domain' --yesno 'Would you like to add a custom search domain? \n (This is only for advanced users who have their own domain)\n' 21 70
+ echo sleep 0.1
sleep 0.1
+ confLogging
+ echo 'if $programname == '\''ovpn-server'\'' then /var/log/openvpn.log
if $programname == '\''ovpn-server'\'' then stop'
+ sudo tee /etc/rsyslog.d/30-openvpn.conf
+ echo '/var/log/openvpn.log
{
    rotate 4
    weekly
    missingok
    notifempty
    compress
    delaycompress
    sharedscripts
    postrotate
        invoke-rc.d rsyslog rotate >/dev/null 2>&1 || true
    endscript
}'
+ sudo tee /etc/logrotate.d/openvpn
+ case ${PLAT} in
+ sudo systemctl restart rsyslog.service
+ finalExports
+ '[' -e /etc/pivpn/setupVars.conf ']'
+ sudo sed -i.update.bak '/pivpnUser/d;/UNATTUPG/d;/pivpnInterface/d;/IPv4dns/d;/IPv4addr/d;/IPv4gw/d;/pivpnProto/d;/PORT/d;/ENCRYPT/d;/DOWNLOAD_DH_PARAM/d;/PUBLICDNS/d;/OVPNDNS1/d;/OVPNDNS2/d;' /etc/pivpn/setupVars.conf
+ echo pivpnUser=pi
+ echo UNATTUPG=unattended-upgrades
+ sudo tee /etc/pivpn/setupVars.conf
+ echo pivpnInterface=wlan0
+ echo IPv4dns=<IP_ADDRESS>
+ echo IPv4addr=<IP_ADDRESS>
+ echo IPv4gw=<IP_ADDRESS>
+ echo pivpnProto=tcp
+ echo PORT=443
+ echo ENCRYPT=2048
+ echo APPLY_TWO_POINT_FOUR=false
+ echo DOWNLOAD_DH_PARAM=false
+ echo PUBLICDNS=
+ echo OVPNDNS1=<IP_ADDRESS>
+ echo OVPNDNS2=<IP_ADDRESS>
+ echo '::: Install Complete...'
::: Install Complete...
+ echo '::: Restarting services...'
::: Restarting services...
+ case ${PLAT} in
+ sudo systemctl enable openvpn.service
Synchronizing state for openvpn.service with sysvinit using update-rc.d...
Executing /usr/sbin/update-rc.d openvpn defaults
Executing /usr/sbin/update-rc.d openvpn enable
+ sudo systemctl start openvpn.service
+ echo '::: Flushing writes to disk...'
::: Flushing writes to disk...
+ sync
+ echo '::: done.'
::: done.
+ [[ false == false ]]
+ displayFinalMessage
+ whiptail --msgbox --backtitle 'Make it so.' --title 'Installation Complete!' 'Now run '\''pivpn add'\'' to create the ovpn profiles.
Run '\''pivpn help'\'' to see what else you can do!' 21 70
+ whiptail --title Reboot --yesno --defaultno 'It is strongly recommended you reboot after installation.  Would you like to reboot now?' 21 70
+ echo :::
:::
+ [[ false == false ]]
+ echo '::: Installation Complete!'
::: Installation Complete!
+ echo '::: Now run '\''pivpn add'\'' to create an ovpn profile for each of your devices.'
::: Now run 'pivpn add' to create an ovpn profile for each of your devices.
+ echo '::: Run '\''pivpn help'\'' to see what else you can do!'
::: Run 'pivpn help' to see what else you can do!
+ echo '::: It is strongly recommended you reboot after installation.'
::: It is strongly recommended you reboot after installation.
+ echo :::
:::

It worked this time. Try to create a client with pivpn -a and connect remotely.

It worked this time. Try to create a client with pivpn -a and connect REMOTELY (not from your local network).

still not connecting...tried using mobile data...
P.S. I have forwarded  port 443 to 443 in router with TCP.
_Fri May 17 10:50:26 2019 OpenVPN 2.4.7 x86_64-w64-mingw32 [SSL (OpenSSL)] [LZO] [LZ4] [PKCS11] [AEAD] built on Apr 25 2019
Fri May 17 10:50:26 2019 Windows version 6.2 (Windows 8 or greater) 64bit
Fri May 17 10:50:26 2019 library versions: OpenSSL 1.1.0j  20 Nov 2018, LZO 2.10
Fri May 17 10:50:26 2019 MANAGEMENT: TCP Socket listening on [AF_INET]<IP_ADDRESS>:25340