#!/bin/bash

trap 'terminate' INT TERM QUIT

terminate() {
    trap '' INT TERM QUIT
    echo ""
    echo "[*] Shutting down..."
    kill 0
    wait
    echo "[*] Shut down complete."
}

error_checking() {
    case $1 in
        --dev|-v|--version|-h|--help|-s|--setup|--setup-target|--setup-victim|--backend|--sniffer|--realtime|-a|--attack)
            ;;
        *)
            echo "Invalid argument."
            usage
            exit 1
            ;;
    esac
}

usage() {
    echo "Usage:
    ./rupture <parameter>
where <parameter> is:
    -s, --setup
          Initialize backend, create database schema and load it with
          configured targets and victims and create client files
          for each one.
    --setup-target
          Load configured targets in database.
    --setup-victim
          Choose configured victims to be loaded in database and create
          the relative client files for each one.
    --backend
          Deploy backend module. Standard output messages are logged in
          'backend/logs/<datetime>.log'.
    --realtime
          Deploy realtime module. Standard output messages are logged in
          'realtime/logs/<datetime>.log'.
    --sniffer
          Deploy sniffer module. Standard output messages are logged in
          'sniffer/logs/<datetime>.log'.
    -a, --attack
          Deploy backend, realtime and sniffer all together.
          Standard output will display the evolution of the attack, which
          is also logged in 'rupture.log'. Logs for each module also exist
          in relative directories."
}

version() {
    echo "rupture version $(cat $BASEDIR/.version)"
}


use_nvm() {
    if [ -z "$NVM_DIR" ]; then
        export NVM_DIR="$HOME/.nvm"
    fi
    [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
    nvm use 6.3
}

setup() {
    $BASEDIR/backend/setup_backend.sh
    $BASEDIR/backend/setup_targets.sh
    use_nvm
    $BASEDIR/backend/setup_victims.sh
}

attack() {
    echo "[*] Deploying Rupture..."
    $BASEDIR/backend/deploy_backend.sh &>/dev/null &
    $BASEDIR/sniffer/deploy_sniffer.sh &>/dev/null &
    use_nvm
    $BASEDIR/realtime/deploy_realtime.sh &>/dev/null &
    echo "[*] Rupture is live."

    cat /dev/null > $BASEDIR/rupture.log
    tail -f $BASEDIR/rupture.log
}

## Check all arguments for validation before beginning setup
error_checking $@

BASEDIR=$(dirname -- "$(readlink -f -- "${BASH_SOURCE}")")
RUPTUREDIR="$HOME/.rupture"
mkdir -p $RUPTUREDIR
if [ ! -d $RUPTUREDIR/client ]; then
    mkdir -p $RUPTUREDIR/client
    cp -r $BASEDIR/client $RUPTUREDIR
fi

while [[ $# -gt 0 ]]
do
    var="$1"
    case $var in
        --dev)
            rm -r $RUPTUREDIR
            ;;
        -v|--version)
            version
            ;;
        -h|--help)
            usage
            ;;
        -s|--setup)
            setup
            ;;
        --setup-target)
            $BASEDIR/backend/setup_targets.sh
            ;;
        --setup-victim)
            $BASEDIR/backend/setup_victims.sh
            ;;
        --backend)
            $BASEDIR/backend/deploy_backend.sh
            ;;
        --sniffer)
            $BASEDIR/sniffer/deploy_sniffer.sh
            ;;
        --realtime)
            $BASEDIR/realtime/deploy_realtime.sh
            ;;
        -a|--attack)
            attack
            ;;
    esac
    shift
done
