#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# License
# =======
#
# Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

PROJECT_NAME='freedombone'

export TEXTDOMAIN=${PROJECT_NAME}-client
export TEXTDOMAINDIR="/usr/share/locale"

CURR_USER=$USER

# Version number of this script
VERSION="1.01"

WIFI_CHANNEL=2
WIFI_INTERFACE=wlan0

# B.A.T.M.A.N settings
BATMAN_CELLID='02:BA:00:00:03:01'
ESSID='mesh'

# Babel
BABEL_PORT=6696

# ssh (from https://stribika.github.io/2015/01/04/secure-secure-shell.html)
SSH_CIPHERS="chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr"
SSH_MACS="hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-ripemd160-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512,hmac-sha2-256,hmac-ripemd160,umac-128@openssh.com"
SSH_KEX="curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256"
SSH_HOST_KEY_ALGORITHMS="ssh-ed25519-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-rsa-cert-v00@openssh.com,ssh-ed25519,ssh-rsa"

ZERONET_PORT=15441
IPFS_PORT=4001
TRACKER_PORT=6969

# see https://stribika.github.io/2015/01/04/secure-secure-shell.html
function ssh_remove_small_moduli {
  sudo awk '$5 > 2000' /etc/ssh/moduli > /home/$CURR_USER/moduli
  sudo mv /home/$CURR_USER/moduli /etc/ssh/moduli
}

function configure_ssh_client {
  #sudo sed -i 's/#   PasswordAuthentication.*/   PasswordAuthentication no/g' /etc/ssh/ssh_config
  #sudo sed -i 's/#   ChallengeResponseAuthentication.*/   ChallengeResponseAuthentication no/g' /etc/ssh/ssh_config
  sudo sed -i "s/#   HostKeyAlgorithms.*/   HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS/g" /etc/ssh/ssh_config
  sudo sed -i "s/#   Ciphers.*/   Ciphers $SSH_CIPHERS/g" /etc/ssh/ssh_config
  sudo sed -i "s/#   MACs.*/   MACs $SSH_MACS/g" /etc/ssh/ssh_config
  if ! grep -q "HostKeyAlgorithms" /etc/ssh/ssh_config; then
      sudo cp /etc/ssh/ssh_config ~/ssh_config
      sudo chown $CURR_USER:$CURR_USER ~/ssh_config
      echo "   HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS" >> ~/ssh_config
      sudo mv ~/ssh_config /etc/ssh/ssh_config
      sudo chown root:root /etc/ssh/ssh_config
  fi
  sudo sed -i "s/Ciphers.*/Ciphers $SSH_CIPHERS/g" /etc/ssh/ssh_config
  if ! grep -q "Ciphers " /etc/ssh/ssh_config; then
      sudo cp /etc/ssh/ssh_config ~/ssh_config
      sudo chown $CURR_USER:$CURR_USER ~/ssh_config
      echo "   Ciphers $SSH_CIPHERS" >> ~/ssh_config
      sudo mv ~/ssh_config /etc/ssh/ssh_config
      sudo chown root:root /etc/ssh/ssh_config
  fi
  sudo sed -i "s/MACs.*/MACs $SSH_MACS/g" /etc/ssh/ssh_config
  if ! grep -q "MACs " /etc/ssh/ssh_config; then
      sudo cp /etc/ssh/ssh_config ~/ssh_config
      sudo chown $CURR_USER:$CURR_USER ~/ssh_config
      echo "   MACs $SSH_MACS" >> ~/ssh_config
      sudo mv ~/ssh_config /etc/ssh/ssh_config
      sudo chown root:root /etc/ssh/ssh_config
  fi

  # Create ssh keys
  if [ ! -f /home/$CURR_USER/.ssh/id_ed25519 ]; then
      ssh-keygen -t ed25519 -o -a 100
  fi
  if [ ! -f /home/$CURR_USER/.ssh/id_rsa ]; then
      ssh-keygen -t rsa -b 4096 -o -a 100
  fi

  ssh_remove_small_moduli

  echo ''
  echo $'Copy the following into a file called /home/username/.ssh/authorized_keys on the Freedombone server'
  echo ''
  echo $(cat /home/$CURR_USER/.ssh/id_rsa.pub)
  echo $(cat /home/$CURR_USER/.ssh/id_ed25519.pub)
  echo ''
}

function mesh_babel {
  if [ ! -f /tmp/meshtype ]; then
      sudo apt-get -y install babeld
      sudo apt-get -y install irssi mumble
      if [ ! -f /usr/bin/toxic ]; then
          sudo apt-get -y install toxic
          echo "n
/nick $USER
/exit
" | /usr/bin/toxic -d
      fi

      CURR_DIR=$(pwd)
      if [ ! -f ~/develop/toxid ]; then
          if [ ! -f ~/develop ]; then
              mkdir ~/develop
          fi
          cd ~/develop
          git clone https://github.com/bashrc/toxid
      fi
      cd ~/develop/toxid
      sudo make install
      cd $CURR_DIR
  fi

  babel_script=/tmp/babel

  echo '#!/bin/bash' > $babel_script
  echo '' >> $babel_script
  echo 'if [[ $1 == "ls" || $1 == "list" ]]; then' >> $babel_script
  echo '    avahi-browse -atl' >> $babel_script
  echo '    exit 0' >> $babel_script
  echo 'fi' >> $babel_script
  echo '' >> $babel_script
  echo 'if [[ $1 == "start" ]]; then' >> $babel_script
  echo '    if [ -f /tmp/meshtype ] ; then' >> $babel_script
  echo '        echo "Mesh already running"' >> $babel_script
  echo '        return' >> $babel_script
  echo '    fi' >> $batman_script
  echo '    # install avahi' >> $babel_script
  echo '    apt-get -y install avahi-utils avahi-autoipd avahi-daemon avahi-dnsconfd bittornado' >> $babel_script
  echo '    sed -i "s|#host-name=.*|host-name=$(hostname)|g" /etc/avahi/avahi-daemon.conf' >> $babel_script
  echo '    sed -i "s|host-name=.*|host-name=$(hostname)|g" /etc/avahi/avahi-daemon.conf' >> $babel_script
  echo '    if [ -f /bin/systemctl ]; then' >> $babel_script
  echo '        systemctl restart avahi-daemon' >> $babel_script
  echo '    else' >> $babel_script
  echo '        service avahi-daemon restart' >> $babel_script
  echo '    fi' >> $babel_script
  echo '    echo "babel" > /tmp/meshtype' >> $babel_script
  echo 'fi' >> $babel_script
  echo '' >> $babel_script
  echo "IFACE=$WIFI_INTERFACE" >> $babel_script
  echo 'if [[ $IFACE == "wlan0" ]]; then' >> $babel_script
  echo '    if grep -q "wlan1" /proc/net/dev; then' >> $babel_script
  echo '        IFACE=wlan1' >> $babel_script
  echo '    fi' >> $babel_script
  echo 'fi' >> $babel_script
  echo 'if [[ $IFACE == "wlan0" ]]; then' >> $babel_script
  echo '    if grep -q "wlan2" /proc/net/dev; then' >> $babel_script
  echo '        IFACE=wlan2' >> $babel_script
  echo '    fi' >> $babel_script
  echo 'fi' >> $babel_script
  echo 'if [[ $IFACE == "wlan0" ]]; then' >> $babel_script
  echo '    if grep -q "wlan3" /proc/net/dev; then' >> $babel_script
  echo '        IFACE=wlan3' >> $babel_script
  echo '    fi' >> $babel_script
  echo 'fi' >> $babel_script
  echo '' >> $babel_script
  echo 'if [[ ! grep -q "$IFACE" /proc/net/dev || $1 == "stop" ]]; then' >> $babel_script
  echo '    if ! grep -q "$IFACE" /proc/net/dev; then' >> $babel_script
  echo '        echo "Interface $IFACE was not found"' >> $babel_script
  echo '    else' >> $babel_script
  echo '        echo "Stopping"' >> $babel_script
  echo '    fi' >> $babel_script
  echo '    ifconfig $IFACE down' >> $babel_script
  echo '    pkill babeld' >> $babel_script
  echo '    if [ -f /bin/systemctl ]; then' >> $babel_script
  echo '        systemctl restart network-manager' >> $babel_script
  echo '    else' >> $babel_script
  echo '        service network-manager restart' >> $babel_script
  echo '    fi' >> $babel_script
  echo '    exit 1' >> $babel_script
  echo 'fi' >> $babel_script
  echo '' >> $babel_script
  echo 'if [ -f /bin/systemctl ]; then' >> $babel_script
  echo '    systemctl stop network-manager' >> $babel_script
  echo 'else' >> $babel_script
  echo '    service network-manager stop' >> $babel_script
  echo 'fi' >> $babel_script
  echo 'ifconfig $IFACE down' >> $babel_script
  echo -n 'iwconfig $IFACE mode ad-hoc channel ' >> $babel_script
  echo "$WIFI_CHANNEL essid \"$ESSID\"" >> $babel_script
  echo 'ifconfig $IFACE up' >> $babel_script
  echo -n 'ifconfig $IFACE:avahi ' >> $babel_script
  echo -n "$LOCAL_NETWORK_STATIC_IP_ADDRESS netmask " >> $babel_script
  echo '255.255.255.0 broadcast 192.168.13.255' >> $babel_script
  echo -n 'babeld -D $IFACE:avahi -p ' >> $babel_script
  echo -n "$BABEL_PORT -d 5 " >> $babel_script
  echo '$IFACE' >> $babel_script
  echo 'exit 0' >> $babel_script
  chmod +x $babel_script
  sudo mv $babel_script /usr/bin/babel
}

function mesh_batman {
  if [ ! -f /tmp/meshtype ]; then
      sudo apt-get -y install iproute bridge-utils libnetfilter-conntrack3 batctl
      sudo apt-get -y install python-dev libevent-dev ebtables python-pip
      sudo apt-get -y install wireless-tools rfkill
      sudo apt-get -y install irssi mumble
      if [ ! -f /usr/bin/toxic ]; then
          sudo apt-get -y install toxic
          echo "n
/nick $USER
/exit
" | /usr/bin/toxic -d
      fi
  fi

  batman_script=/tmp/batman

  echo '#!/bin/bash' > $batman_script
  echo '' >> $batman_script
  echo 'if [[ $1 == "start" ]]; then' >> $batman_script
  echo '    # install avahi' >> $batman_script
  echo '    apt-get -y install avahi-utils avahi-autoipd avahi-daemon avahi-dnsconfd bittornado' >> $batman_script
  echo '    sed -i "s|#host-name=.*|host-name=$(hostname)|g" /etc/avahi/avahi-daemon.conf' >> $batman_script
  echo '    sed -i "s|host-name=.*|host-name=$(hostname)|g" /etc/avahi/avahi-daemon.conf' >> $batman_script
  echo '    sed -i "s|use-ipv4=.*|use-ipv4=yes|g" /etc/avahi/avahi-daemon.conf' >> $batman_script
  echo '    sed -i "s|use-ipv6=.*|use-ipv6=no|g" /etc/avahi/avahi-daemon.conf' >> $batman_script
  echo '    sed -i "s|#disallow-other-stacks=.*|disallow-other-stacks=yes|g" /etc/avahi/avahi-daemon.conf' >> $batman_script
  echo '    sed -i "s|hosts:.*|hosts:          files mdns4_minimal dns mdns4 mdns|g" /etc/nsswitch.conf' >> $batman_script
  echo '    if [ -f /bin/systemctl ]; then' >> $batman_script
  echo '        systemctl restart avahi-daemon' >> $batman_script
  echo '    else' >> $batman_script
  echo '        service avahi-daemon restart' >> $batman_script
  echo '    fi' >> $batman_script
  echo 'fi' >> $batman_script
  echo '' >> $batman_script
  echo '# Mesh definition' >> $batman_script
  echo "ESSID=$ESSID" >> $batman_script
  echo "CELLID=$BATMAN_CELLID" >> $batman_script
  echo "CHANNEL=$WIFI_CHANNEL" >> $batman_script
  echo '' >> $batman_script
  echo '# Ethernet bridge definition (bridged to bat0)' >> $batman_script
  echo 'BRIDGE=br-mesh' >> $batman_script
  echo "IFACE=$WIFI_INTERFACE" >> $batman_script
  echo 'EIFACE=eth0' >> $batman_script
  echo '' >> $batman_script
  echo 'if [[ $IFACE == "wlan0" ]]; then' >> $batman_script
  echo '    if grep -q "wlan1" /proc/net/dev; then' >> $batman_script
  echo '        IFACE=wlan1' >> $batman_script
  echo '    fi' >> $batman_script
  echo 'fi' >> $batman_script
  echo 'if [[ $IFACE == "wlan0" ]]; then' >> $batman_script
  echo '    if grep -q "wlan2" /proc/net/dev; then' >> $batman_script
  echo '        IFACE=wlan2' >> $batman_script
  echo '    fi' >> $batman_script
  echo 'fi' >> $batman_script
  echo 'if [[ $IFACE == "wlan0" ]]; then' >> $batman_script
  echo '    if grep -q "wlan3" /proc/net/dev; then' >> $batman_script
  echo '        IFACE=wlan3' >> $batman_script
  echo '    fi' >> $batman_script
  echo 'fi' >> $batman_script
  echo '' >> $batman_script
  echo 'if [ -e /etc/default/batctl ]; then' >> $batman_script
  echo '    . /etc/default/batctl' >> $batman_script
  echo 'fi' >> $batman_script
  echo '' >> $batman_script
  echo 'start() {' >> $batman_script
  echo '    if [ -f /tmp/meshtype ] ; then' >> $batman_script
  echo '        echo "Mesh already running"' >> $batman_script
  echo '        return' >> $batman_script
  echo '    fi' >> $batman_script
  echo '    if [ -z "$IFACE" ] ; then' >> $batman_script
  echo '        echo "error: unable to find wifi interface, not enabling batman-adv mesh"' >> $batman_script
  echo '        return' >> $batman_script
  echo '    fi' >> $batman_script
  echo '    echo "info: enabling batman-adv mesh network $ESSID on $IFACE"' >> $batman_script

  echo '    if [ -f /bin/systemctl ]; then' >> $batman_script
  echo '        systemctl stop network-manager' >> $batman_script
  echo '    else' >> $batman_script
  echo '        service network-manager stop' >> $batman_script
  echo '    fi' >> $batman_script
  echo '    sleep 5' >> $batman_script
  echo '' >> $batman_script
  echo "    # remove an avahi service which isn't used" >> $batman_script
  echo '    if [ -f /etc/avahi/services/udisks.service ]; then' >> $batman_script
  echo '        sudo rm /etc/avahi/services/udisks.service' >> $batman_script
  echo '    fi' >> $batman_script
  echo '' >> $batman_script
  echo '    # Might have to re-enable wifi' >> $batman_script
  echo '    rfkill unblock $(rfkill list|awk -F: "/phy/ {print $1}") || true' >> $batman_script
  echo '' >> $batman_script
  echo '    ifconfig $IFACE down' >> $batman_script
  echo '    ifconfig $IFACE mtu 1532' >> $batman_script
  echo '    iwconfig $IFACE enc off' >> $batman_script
  echo '    iwconfig $IFACE mode ad-hoc essid $ESSID channel $CHANNEL' >> $batman_script
  echo '    sleep 1' >> $batman_script
  echo '    iwconfig $IFACE ap $CELLID' >> $batman_script
  echo '' >> $batman_script
  echo '    modprobe batman-adv' >> $batman_script
  echo '    batctl if add $IFACE' >> $batman_script
  echo '    ifconfig $IFACE up' >> $batman_script
  echo '    avahi-autoipd --force-bind --daemonize --wait $BRIDGE' >> $batman_script
  echo '    avahi-autoipd --force-bind --daemonize --wait $IFACE' >> $batman_script
  echo '    ifconfig bat0 up promisc' >> $batman_script
  echo '' >> $batman_script
  echo '    #Use persistent HWAddr' >> $batman_script
  echo '    ether_new=$(ifconfig eth0 | grep HWaddr | sed -e "s/.*HWaddr //")' >> $batman_script
  echo '    if [ ! -f /var/lib/mesh-node/bat0 ]; then' >> $batman_script
  echo '        mkdir /var/lib/mesh-node' >> $batman_script
  echo '        echo "${ether_new}" > /var/lib/mesh-node/bat0' >> $batman_script
  echo '    else' >> $batman_script
  echo '        ether=$(cat /var/lib/mesh-node/bat0)' >> $batman_script
  echo '        ifconfig bat0 hw ether ${ether}' >> $batman_script
  echo '    fi' >> $batman_script
  echo '' >> $batman_script
  echo '    if [ "$EIFACE" ] ; then' >> $batman_script
  echo '        brctl addbr $BRIDGE' >> $batman_script
  echo '        brctl addif $BRIDGE bat0' >> $batman_script
  echo '        brctl addif $BRIDGE $EIFACE' >> $batman_script
  echo '        ifconfig bat0 0.0.0.0' >> $batman_script
  echo '        ifconfig $EIFACE 0.0.0.0' >> $batman_script
  echo '        ifconfig $EIFACE up promisc' >> $batman_script
  echo '        ifconfig $BRIDGE up' >> $batman_script
  echo '    fi' >> $batman_script
  echo '' >> $batman_script
  echo '    iptables -A INPUT -p tcp --dport 548 -j ACCEPT' >> $batman_script
  echo '    iptables -A INPUT -p udp --dport 548 -j ACCEPT' >> $batman_script
  echo '    iptables -A INPUT -p tcp --dport 5353 -j ACCEPT' >> $batman_script
  echo '    iptables -A INPUT -p udp --dport 5353 -j ACCEPT' >> $batman_script
  echo '    iptables -A INPUT -p tcp --dport 5354 -j ACCEPT' >> $batman_script
  echo '    iptables -A INPUT -p udp --dport 5354 -j ACCEPT' >> $batman_script
  echo "    iptables -A INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT" >> $batman_script
  echo "    iptables -A INPUT -p udp --dport $ZERONET_PORT -j ACCEPT" >> $batman_script
  echo "    iptables -A INPUT -p udp --dport $TRACKER_PORT -j ACCEPT" >> $batman_script
  echo "    iptables -A INPUT -p tcp --dport $TRACKER_PORT -j ACCEPT" >> $batman_script
  echo "    iptables -A INPUT -p tcp --dport $IPFS_PORT -j ACCEPT" >> $batman_script
  echo "    iptables -A INPUT -p udp --dport 1900 -j ACCEPT" >> $batman_script
  echo "    iptables -A INPUT -p tcp --dport 80 -j ACCEPT" >> $batman_script
  echo "    iptables -A INPUT -p udp -m udp -j ACCEPT" >> $batman_script
  echo '' >> $batman_script
  echo '    if [ -f /bin/systemctl ]; then' >> $batman_script
  echo '        systemctl restart avahi-daemon' >> $batman_script
  echo '    else' >> $batman_script
  echo '        service avahi-daemon restart' >> $batman_script
  echo '    fi' >> $batman_script
  echo '    echo "batman" > /tmp/meshtype' >> $batman_script
  echo '}' >> $batman_script
  echo '' >> $batman_script
  echo 'stop() {' >> $batman_script
  echo '    if [ -z "$IFACE" ]; then' >> $batman_script
  echo '        echo "error: unable to find wifi interface, not enabling batman-adv mesh"' >> $batman_script
  echo '        return' >> $batman_script
  echo '    fi' >> $batman_script
  echo '    if [ "$EIFACE" ]; then' >> $batman_script
  echo '        brctl delif $BRIDGE bat0' >> $batman_script
  echo '        brctl delif $BRIDGE $EIFACE' >> $batman_script
  echo '        ifconfig $BRIDGE down || true' >> $batman_script
  echo '        brctl delbr $BRIDGE' >> $batman_script
  echo '        ifconfig $EIFACE down -promisc' >> $batman_script
  echo '    fi' >> $batman_script
  echo '' >> $batman_script
  echo '    avahi-autoipd -k $BRIDGE' >> $batman_script
  echo '    avahi-autoipd -k $IFACE' >> $batman_script
  echo '    ifconfig bat0 down -promisc' >> $batman_script
  echo '' >> $batman_script
  echo '    batctl if del $IFACE' >> $batman_script
  echo '    rmmod batman-adv' >> $batman_script
  echo '    ifconfig $IFACE mtu 1500' >> $batman_script
  echo '    ifconfig $IFACE down' >> $batman_script
  echo '    iwconfig $IFACE mode managed' >> $batman_script
  echo '' >> $batman_script
  echo '    iptables -D INPUT -p tcp --dport 548 -j ACCEPT' >> $batman_script
  echo '    iptables -D INPUT -p udp --dport 548 -j ACCEPT' >> $batman_script
  echo '    iptables -D INPUT -p tcp --dport 5353 -j ACCEPT' >> $batman_script
  echo '    iptables -D INPUT -p udp --dport 5353 -j ACCEPT' >> $batman_script
  echo '    iptables -D INPUT -p tcp --dport 5354 -j ACCEPT' >> $batman_script
  echo '    iptables -D INPUT -p udp --dport 5354 -j ACCEPT' >> $batman_script
  echo "    iptables -D INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT" >> $batman_script
  echo "    iptables -D INPUT -p udp --dport $ZERONET_PORT -j ACCEPT" >> $batman_script
  echo "    iptables -D INPUT -p udp --dport $TRACKER_PORT -j ACCEPT" >> $batman_script
  echo "    iptables -D INPUT -p tcp --dport $TRACKER_PORT -j ACCEPT" >> $batman_script
  echo "    iptables -D INPUT -p tcp --dport $IPFS_PORT -j ACCEPT" >> $batman_script
  echo "    iptables -D INPUT -p udp --dport 1900 -j ACCEPT" >> $batman_script
  echo "    iptables -D INPUT -p tcp --dport 80 -j ACCEPT" >> $batman_script
  echo "    iptables -D INPUT -p udp -m udp -j ACCEPT" >> $batman_script
  echo '' >> $batman_script
  echo '    if [ -f /bin/systemctl ]; then' >> $batman_script
  echo '        systemctl restart network-manager' >> $batman_script
  echo '    else' >> $batman_script
  echo '        service network-manager restart' >> $batman_script
  echo '    fi' >> $batman_script
  echo '    ' >> $batman_script
  echo '    # restore tox bootstrap nodes for the internet' >> $batman_script
  echo '    if [ -f /usr/share/toxic/DHTnodes.internet ]; then' >> $batman_script
  echo '        mv /usr/share/toxic/DHTnodes.internet /usr/share/toxic/DHTnodes' >> $batman_script
  echo '    fi' >> $batman_script
  echo '    if [ -f /usr/local/share/toxic/DHTnodes.internet ]; then' >> $batman_script
  echo '        mv /usr/local/share/toxic/DHTnodes.internet /usr/local/share/toxic/DHTnodes' >> $batman_script
  echo '    fi' >> $batman_script
  echo '' >> $batman_script
  echo '    if [ -f /bin/systemctl ]; then' >> $batman_script
  echo '        systemctl stop avahi-daemon' >> $batman_script
  echo '    else' >> $batman_script
  echo '        service avahi-daemon stop' >> $batman_script
  echo '    fi' >> $batman_script
  echo '' >> $batman_script
  echo '    sudo rm -f /tmp/meshtype' >> $batman_script
  echo '' >> $batman_script
  echo '    # kill processes' >> $batman_script
  echo '    zeronet_proc=$(ps aux | grep zeronet | grep -v grep | awk -F " " "{print $2}" | head -n1)' >> $batman_script
  echo '    if [ "$zeronet_proc" ]; then' >> $batman_script
  echo '        kill -9 $zeronet_proc 2> /dev/null' >> $batman_script
  echo '    fi' >> $batman_script
  echo '    tracker_proc=$(ps aux | grep bttrack | grep -v grep | awk -F " " "{print $2}" | head -n1)' >> $batman_script
  echo '    if [ "$tracker_proc" ]; then' >> $batman_script
  echo '        kill -9 $tracker_proc 2> /dev/null' >> $batman_script
  echo '    fi' >> $batman_script
  echo '    if [ ! -f /etc/systemd/system/ipfs.service ]; then' >> $batman_script
  echo '        ipfs_proc=$(ps aux | grep ipfs | grep -v grep | awk -F " " "{print $2}" | head -n1)' >> $batman_script
  echo '        if [ "$ipfs_proc" ]; then' >> $batman_script
  echo '            kill -9 $ipfs_proc 2> /dev/null' >> $batman_script
  echo '        fi' >> $batman_script
  echo '    fi' >> $batman_script
  echo '}' >> $batman_script
  echo '' >> $batman_script
  echo 'if ! grep -q "$IFACE" /proc/net/dev; then' >> $batman_script
  echo '    echo "Interface $IFACE was not found"' >> $batman_script
  echo '    stop' >> $batman_script
  echo '    exit 1' >> $batman_script
  echo 'fi' >> $batman_script
  echo '' >> $batman_script
  echo 'case "$1" in' >> $batman_script
  echo '    start|stop)' >> $batman_script
  echo '        $1' >> $batman_script
  echo '        ;;' >> $batman_script
  echo '    restart)' >> $batman_script
  echo '        stop' >> $batman_script
  echo '        sleep 10' >> $batman_script
  echo '        start' >> $batman_script
  echo '        ;;' >> $batman_script
  echo '    status)' >> $batman_script
  echo '        batctl o' >> $batman_script
  echo '        ;;' >> $batman_script
  echo '    ping)' >> $batman_script
  echo '        batctl ping $2' >> $batman_script
  echo '        ;;' >> $batman_script
  echo '    ls|list)' >> $batman_script
  echo '        avahi-browse -atl' >> $batman_script
  echo '        ;;' >> $batman_script
  echo '    *)' >> $batman_script
  echo '    echo "error: invalid parameter $1"' >> $batman_script
  echo '    echo "usage: $0 {start|stop|restart|status|ping|ls|list}"' >> $batman_script
  echo '    exit 2' >> $batman_script
  echo '    ;;' >> $batman_script
  echo 'esac' >> $batman_script
  echo 'exit 0' >> $batman_script
  chmod +x $batman_script
  sudo cp -f $batman_script /usr/bin/batman
  rm $batman_script
}

function show_help {
    echo ''
    echo $"${PROJECT_NAME}-client"
    echo ''
    exit 0
}

while [[ $# > 1 ]]
do
key="$1"

case $key in
    -h|--help)
    show_help
    ;;
    --essid)
    shift
    ESSID="$1"
    ;;
    --channel)
    shift
    WIFI_CHANNEL=${1}
    ;;
    *)
    # unknown option
    ;;
esac
shift
done

echo $'Configuring client'
configure_ssh_client
mesh_batman
mesh_babel
echo $'Configuration complete'
exit 0
