#!/bin/sh
# The MIT License (MIT)
#
# Copyright (c) 2017 Adar Nimrod <nimrod@shore.co.il>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# Logs in to the Kavim's buses wifi.
# Requires: cURL, sh, awk, logger, nmcli.
# To install, clone the repo, cd to this directory and run:
#
# sudo cp --preserve=mode kavim-wifi /etc/NetworkManager/dispatcher.d/90kavimwifi
#
# If you don't want to clone the repo but you just want this script, run:
#
# curl https://www.shore.co.il/cgit/rcfiles/plain/Documents/bin/kavim-wifi | sudo tee /etc/NetworkManager/dispatcher.d/90kavimwifi
# sudo chmod +x /etc/NetworkManager/dispatcher.d/90kavimwifi

set -eu

alias tolower='awk "{print tolower(\$0)}"'

die () {
    logger -p user.err "$@"
    exit 1
}

iswifi () {
    # TODO: Check if a network interface is passed.
    [ "$(nmcli --terse --fields GENERAL.TYPE device show "$1" | awk -F: '{print $2}')" = 'wifi' ]
}

wifi_connection () {
    nmcli --terse --fields GENERAL.CONNECTION device show "$1" | awk -F: '{print $2}'
}

if [ $# -ne 2 ]
then
    echo "Usage: $0 interface action"
    exit 1
else
    interface="$1"
    action="$2"
fi

which curl > /dev/null || die "Can't login to the kavim wifi, cURL is not installed."
which awk > /dev/null || die "Can't login to the kavim wifi, awk is not installed."
which logger > /dev/null || die "Can't login to the kavim wifi, logger is not installed."
which nmcli > /dev/null || die "Can't login to the kavim wifi, nmcli is not installed."

[ "$action" = 'up' ] || die "Can't login to the kavim wifi, action $action isn't up."
iswifi "$interface" || die "Can't login to the kavim wifi, interface $interface isn't wifi."
connection="$(wifi_connection "$interface" | tolower)"
[ "$connection" = "kavim-wifi" ] || die "Can't login to the kavim wifi, wifi network $connection isn't Kavim-wifi."

redirect_url="$(curl --output /dev/null --silent --write-out '%{redirect_url}' http://detectportal.firefox.com/success.txt)"
logger -p user.debug "Kavim wifi redirect url: $redirect_url"
login_url="$(echo "$redirect_url&command=login&access_mode=open&name=" | sed 's@https://[a-zA-Z0-9_/:\.\-\?]*\?@https://guest1.ic.peplink.com/cp/login?@')"
logger -p user.debug "Kavim wifi login URL: $login_url"
http_code="$(curl --output /dev/null --silent --write-out '%{http_code}' "$login_url")"
logger -p user.debug "Kavim wifi login HTTP code: $http_code"