UPDATE_URL="https://raw.githubusercontent.com/necro-nemesis/LabyrinthAP/master/"
wget -q ${UPDATE_URL}/installers/common.sh -O /tmp/raspapcommon.sh
source /tmp/raspapcommon.sh && rm -f /tmp/raspapcommon.sh

function test_root_user() {
    if [ "$EUID" -ne 0 ]
        then echo "Please run LabyrinthAP installer as root"
        exit
    fi
}

function update_system_packages() {
    install_log "Updating sources"
    sudo apt-get update || install_error "Unable to update package list"
}

function install_dependencies() {
    install_log "Installing required packages"
    sudo apt-get -y install curl
    echo "Install public key used to sign the lokinet binaries."
    curl -so /etc/apt/trusted.gpg.d/oxen.gpg https://deb.oxen.io/pub.gpg
    echo "deb https://deb.oxen.io $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/oxen.list
    #curl -s https://deb.imaginary.stream/public.gpg | sudo apt-key add -
    #echo "deb https://deb.imaginary.stream $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/imaginary.stream.list
    sudo apt-get update
    sudo yes | apt-get install whois lighttpd $php_package git resolvconf hostapd dnsmasq vnstat libqmi-utils udhcpc lokinet || install_error "Unable to install dependencies"
}

#Patch Lokinet Service Wants/After DNSMasq

function patch_lokinet_service() {
  install_log "Patching Lokinet Service"
  sed -i '/^After=network-online.target/a Wants=dnsmasq.service \nAfter=dnsmasq.service' /lib/systemd/system/lokinet.service
  sudo systemctl daemon-reload

}

#Avoid race condition between Hostapd and dnsmasq

function set_runlevel_five() {
  install_log "Setting Hostapd after DNSMasq init sequence"
  sudo mv /etc/rc5.d/S01dnsmasq /etc/rc5.d/S02dnsmasq
  sudo mv /etc/rc5.d/S01hostapd /etc/rc5.d/S03hostapd

}

#Remove NetworkManager and install dhcpd if required Armbian.

function check_for_networkmananger() {
  install_log "Checking for NetworkManager"
  echo "Checking for Network Manager"
    if [ -f /lib/systemd/system/network-manager.service ] || [ -f /lib/systemd/system/NetworkManager.service ]; then
  echo "Network Manager found. Replacing with DHCPCD"
        sudo apt-get -y purge network-manager
        sudo apt-get -y install dhcpcd5
    fi

}

install_raspap
