#!/system/bin/sh
# Adapted from tether script from andTether application
# Original Header:
# ----------------
# @Version=6
# Copyright 2008 parrothd
# Copyright 2009 zodiac (Markus Gutschke)

adhoclog="$2"/var/adhoc.log
mac_whitelist="$2"/conf/whitelist_mac.conf
dnsmasq_pidfile="$2"/var/dnsmasq.pid
dnsmasq_leasefile="$2"/var/dnsmasq.leases
dnsmasq="$2"/bin/dnsmasq
dnsmasq_conf="$2"/conf/dnsmasq.conf
dhcpclient_log="$2"/var/dhcp.log
iptables="$2"/bin/iptables
tiwlan_ini="$2"/conf/tiwlan.ini

really_kill() {
  kill "$1" || :
  kill -0 "$1" && {
    sleep 1
    kill -9 "$1"
  } || :
  kill -0 "$1" || {
    echo "Failed to kill process $1"
    false
  }
}

run() {
    if eval { "$1;" } >/dev/null 2>&1; then
      local i
      echo -n "<div class=\"date\">`date +"%d.%m.%y %T"`</div><div class=\"action\">$2...</div><div class=\"output\">" >>"$adhoclog"
      if i=`eval "$3" 2>&1`; then
        echo "</div><div class=\"done\">done</div><hr>" >>"$adhoclog"
      else
        { echo "$i" | while read i; do echo -n "$i<br />"; done
        echo "</div><div class=\"failed\">failed</div><hr>"; } >>"$adhoclog"
      fi
    fi
}

startwifi() {
    ## Stop current users
    stopwifi

    ## Loading wlan.ko module
    run '!({ echo; lsmod; } | while read m; do case "$m" in wlan*) break;; esac; false; done)' \
        "Loading wlan.ko module" \
        'insmod /system/lib/modules/wlan.ko'

    ## Configuring WiFi
    run : \
        "Configuring WiFi interface" \
        'wlan_loader -f /system/etc/wifi/Fw1251r1c.bin -e /proc/calibration -i "$tiwlan_ini"'
}

stopwifi() {
    ## Stopping current users of the wifi interface
    run 'ps | while read u p pp v r w pc s n; do case "$n" in *dnsmasq*|*wpa_supplicant*|*dhcp*) break;; esac; false; done' \
        "Stopping current WiFi users" \
        '{ ps | while read u p pp v r w pc s n; do case "$n" in *dnsmasq*|*wpa_supplicant*|*dhcp*) really_kill "$p";; esac; done; :; }'

    ## Configuring WiFi
    run '{ echo; lsmod; } | while read m; do case "$m" in wlan*) break;; esac; false; done' \
        "Configuring WiFi interface" \
        'wlan_loader -f /system/etc/wifi/Fw1251r1c.bin -e /proc/calibration -i /system/etc/wifi/tiwlan.ini'

    # Unloading module
    run '{ echo; lsmod; } | while read m; do case "$m" in wlan*) break;; esac; false; done' \
        "Unloading wlan.ko module" \
        'rmmod wlan'
}

startint_server() {
    # Configuring network interface
    run '{ echo; lsmod; } | while read m; do case "$m" in wlan*) break;; esac; false; done' \
        "Configuring network interface" \
        'ifconfig tiwlan0 192.168.2.1 netmask 255.255.255.0 && ifconfig tiwlan0 up'
}

startint_client() {
    # Configuring network interface
    run '{ echo; lsmod; } | while read m; do case "$m" in wlan*) break;; esac; false; done' \
        "Configuring network interface" \
        'ifconfig tiwlan0 dhcp && ifconfig tiwlan0 up'
    
    sleep 5
    dhcpcd tiwlan0 2> "$dhcpclient_log"
}

stopint() {
    # Shutting down network interface
    run '{ echo; lsmod; } | while read m; do case "$m" in wlan*) break;; esac; false; done' \
        "Shutting down network interface" \
        'ifconfig tiwlan0 down'
}

startipt() {
    ## Setting up firewall rules
    run '{ echo; lsmod; } | while read m; do case "$m" in wlan*) break;; esac; false; done' \
        "Setting up firewall rules" \
        '"$iptables" -F &&
         "$iptables" -F -t nat &&
         "$iptables" -I FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT &&
         "$iptables" -I FORWARD -s 192.168.2.0/24 -j ACCEPT &&
         "$iptables" -P FORWARD DROP &&
         "$iptables" -t nat -I POSTROUTING -s 192.168.2.0/24 -j MASQUERADE'
}

stopipt() {
    ## Tearing down firewall rules
    run :\
        "Tearing down firewall rules" \
        '"$iptables" -F &&
		"$iptables" -t nat -F &&
		"$iptables" -X &&
		"$iptables" -t nat -X &&
        "$iptables" -P FORWARD ACCEPT'
}

startipfw() {
    ## Enabling IP forwarding
    run 'case "`cat /proc/sys/net/ipv4/ip_forward`" in 1) false;; esac' \
        "Enabling IP forwarding" \
        '{ echo 1 > /proc/sys/net/ipv4/ip_forward; }'
}
stopipfw() {
    ## Disabling IP forwarding
    run 'case "`cat /proc/sys/net/ipv4/ip_forward`" in 0) false;; esac' \
        "Disabling IP forwarding" \
        '{ echo 0 > /proc/sys/net/ipv4/ip_forward; }'
}

startdnsmasq() {
    ## Starting dnsmasq
    run '!(ps | while read u p pp v r w pc s n; do case "$n" in *dnsmasq*) break;; esac; false; done)' \
        "Starting dnsmasq" \
        '"$dnsmasq" --conf-file="$dnsmasq_conf"'
}

stopdnsmasq() {
    ## Stopping dnsmasq
    run 'ps | while read u p pp v r w pc s n; do case "$n" in *dnsmasq*) break;; esac; false; done' \
        "Stopping dnsmasq" \
        'ps | while read u p pp v r w pc s n; do case "$n" in *dnsmasq*) really_kill "$p";; esac; done; rm '"'$dnsmasq_pidfile' 2>/dev/null"
}

startsecnat() {
    ## Activating MAC access control
    run "dd if='$mac_whitelist' >/dev/null 2>&1" \
        "Activating MAC access control" \
        '"$iptables" -t nat -I PREROUTING -s 192.168.2.0/24 -j DROP'
}

startmacwhitelist() {
    ## Adding MAC addresses for allowed clients
    run "(read i <'$mac_whitelist') 2>/dev/null" \
        "Adding MAC addresses for allowed clients" \
        "(while read mac; do '$iptables' -t nat -I PREROUTING -m mac --mac-source \$mac -j ACCEPT || exit 1; done) <'$mac_whitelist'"
}

case "$1" in
'start_server')
    rm "$adhoclog" 2>/dev/null
    startwifi
    startint_server
#    startipt
#    startipfw
    startdnsmasq
#    startsecnat
#    startmacwhitelist
;;
'start_client')
    rm "$adhoclog" 2>/dev/null
    startwifi
    startint_client
#    startipt
#    startipfw
#    startdnsmasq
#    startsecnat
#    startmacwhitelist
;;
'stop_server')
    rm "$adhoclog" 2>/dev/null
    rm "$dnsmasq_leasefile" 2>/dev/null    
    stopdnsmasq
    stopint
#    stopipfw
#    stopipt
    stopwifi
;;
'stop_client')
    rm "$adhoclog" 2>/dev/null
    rm "$dnsmasq_leasefile" 2>/dev/null    
    rm "$dhcpclient_log" 2>/dev/null
    stopint
#    stopipfw
#    stopipt
    stopwifi
;;
'restartsecwifi')
#    stopipt
#    startipt
    startsecnat
    startmacwhitelist
;;
*)
echo "Usage: $0 [start|stop] [directory]"
;;
esac