#!/bin/bash

set -ue

REINSTALL=0
PACKAGES=1

while getopts -- "-:" a ; do
  case "${a}" in
    -)
    case "${OPTARG}" in
      reinstall)
        REINSTALL=1
        ;;
      no-packages)
        PACKAGES=0
        ;;
      *)
        echo "Unknown long option ${OPTARG}" >/dev/stderr
        exit 1
        ;;
    esac
  esac
done

shift $(($OPTIND-1))

if [ $# -ne 1 ] ; then
  echo "Usage: ${0} <tool>" >/dev/stderr
  exit 1
fi
TOOL=${1}

function install_pkgs {
  if [ ${PACKAGES} -eq 0 ] ; then
    return 0
  fi
  if [ `id -u` -ne "0" ] ; then
    sudo apt-get -y install $* || (
      echo -n "Unable to install packages, please ensure these " >/dev/stderr
      echo "are installed, then run with --no-packages." >/dev/stderr
      echo $*
      false )
    return 0
  fi
  apt-get -y install $*
}

function download {
  SRC=${1}
  DST=${2}
  echo -n "Downloading ${SRC} to ${DST}..." >&2
  wget --no-server-response -q -O ${DST} --content-disposition ${SRC}
  echo " done." >&2
}

function check_sudo {
  sudo -l >/dev/null
}

DESTDIR="${HOME}/tools/${TOOL}"

if [ -d ${DESTDIR} ] ; then
  if [ ${REINSTALL} -eq 1 ] ; then
    rm -ri ${DESTDIR}
  else
    echo "${DESTDIR} exists but not reinstalling." >/dev/stderr
    exit 1
  fi
fi

case ${TOOL} in
  john)
    install_pkgs libssl-dev git build-essential yasm libgmp-dev libpcap-dev \
      pkg-config libbz2-dev libopenmpi-dev openmpi-bin libnss3-dev \
      libkrb5-dev libgmp-dev
    jtemp=`mktemp -d`
    git clone https://github.com/magnumripper/JohnTheRipper.git ${jtemp}/john
    cd ${jtemp}/john/src
    ./configure && make -sj2
    mkdir -p ${DESTDIR}
    cp -r ${jtemp}/john/run/* ${DESTDIR}
    rm -rf ${jtemp}
    # Persistent files
    mkdir -p ${HOME}/.john
    touch ${HOME}/.john/john.pot
    ln -sf ${HOME}/.john/* ${DESTDIR}
    ;;
  wordlists)
    mkdir -p ${DESTDIR}
    download \
      http://downloads.skullsecurity.org/passwords/rockyou.txt.bz2 \
      ${DESTDIR}/rockyou.txt.bz2
    bunzip2 ${DESTDIR}/rockyou.txt.bz2
    download \
      http://downloads.skullsecurity.org/passwords/phpbb.txt.bz2 \
      ${DESTDIR}/phpbb.txt.bz2
    bunzip2 ${DESTDIR}/phpbb.txt.bz2
    download \
      http://downloads.skullsecurity.org/passwords/hak5.txt.bz2 \
      ${DESTDIR}/hak5.txt.bz2
    bunzip2 ${DESTDIR}/hak5.txt.bz2
    ;;
  gcloud)
    mkdir -p ${DESTDIR}
    gtemp=`mktemp -d`
    gbase="https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/"
    # TODO: find a way to make this version independent
    gsdk="google-cloud-sdk-142.0.0-linux-x86_64.tar.gz"
    download "${gbase}${gsdk}" /tmp/gcloud.tar.gz
    tar zxf /tmp/gcloud.tar.gz --strip-components=1 -C ${DESTDIR}
    rm /tmp/gcloud.tar.gz
    ;;
  android-sdk)
    # TODO: find a way to make this version independent
    asdk="https://dl.google.com/android/repository/tools_r25.2.3-linux.zip"
    download ${asdk} /tmp/android-tools.zip
    unzip -d ${DESTDIR} /tmp/android-tools.zip
    rm /tmp/android-tools.zip
    # Install components
    ${DESTDIR}/tools/bin/sdkmanager "emulator" "platform-tools"
    ;;
  burp)
    # Install latest burp free
    mkdir -p ${DESTDIR}
    download \
      https://portswigger.net/DownloadUpdate.ashx\?Product\=Free \
      ${DESTDIR}/burp-free.jar
    if [ $? -ne 0 ] ; then
      echo "Download failed." >&2
      exit 1
    fi
    if [ -x /usr/bin/jarwrapper ] ; then
      # We have binfmt support for jar, so add to bin
      chmod +x ${DESTDIR}/*.jar
      ln -sf ${DESTDIR}/*.jar ${HOME}/bin/burp
    fi
    ;;
  mitmproxy)
    # Find a better way to get the latest
    src="https://github.com/mitmproxy/mitmproxy/releases/download/v2.0.1/mitmproxy-2.0.1-linux.tar.gz"
    download ${src} /tmp/mitmproxy.tar.gz
    mkdir -p ${DESTDIR}
    tar zx -C ${DESTDIR} -f /tmp/mitmproxy.tar.gz
    rm /tmp/mitmproxy.tar.gz
    ;;
  esp)
    src="https://dl.espressif.com/dl/xtensa-esp32-elf-linux64-1.22.0-61-gab8375a-5.2.0.tar.gz"
    download ${src} /tmp/esp32.tar.gz
    mkdir -p ${DESTDIR}
    tar zx -C ${DESTDIR} -f /tmp/esp32.tar.gz
    rm /tmp/esp32.tar.gz
    git clone --recursive https://github.com/espressif/esp-idf.git ${DESTDIR}/esp-idf
    ;;
  dex2jar)
    src="https://github.com/pxb1988/dex2jar/releases/download/2.0/dex-tools-2.0.zip"
    download ${src} /tmp/dex2jar.zip
    tmpd=`mktemp -d`
    unzip -d ${tmpd} /tmp/dex2jar.zip
    mv ${tmpd}/* ${DESTDIR}
    rm /tmp/dex2jar.zip
    rm -rf ${tmpd}
    rm ${DESTDIR}/*.bat
    chmod +x ${DESTDIR}/*.sh
    ;;
  proxmark3)
    install_pkgs p7zip git build-essential libreadline5 libreadline-dev \
      libusb-0.1-4 libusb-dev libqt4-dev perl pkg-config wget libncurses5-dev \
      gcc-arm-none-eabi libstdc++-arm-none-eabi-newlib
    src="https://github.com/Proxmark/proxmark3.git"
    git clone ${src} ${DESTDIR}
    cd ${DESTDIR}
    make -sj2
    check_sudo && sudo /bin/sh -c \
      "cp -rf driver/78-mm-usb-device-blacklist.rules \
         /etc/udev/rules.d/77-mm-usb-device-blacklist.rules &&
         udevadm control --reload-rules"
    ;;
  *)
    echo "Unknown tool: ${TOOL}" >/dev/stderr
    exit 1
    ;;
esac
