#!/bin/bash
#
# This script installs tig core files and components

set -e

if ! hash rimraf 2>/dev/null; then
  >&2 echo -e "${CYAN}- installing rimraf... -${NC}"
  npm install -g rimraf >/dev/null
  >&2 echo -e "${GREEN}- rimraf installed successfully -${NC}"
fi

if ! hash mkdirp 2>/dev/null; then
  >&2 echo -e "${CYAN}- installing mkdirp... -${NC}"
  npm install -g mkdirp >/dev/null
  >&2 echo -e "${GREEN}- mkdirp installed successfully -${NC}"
fi

TIG_ROOT="${TIG_HOME-"$HOME/.tig"}"
TIGRC_PATH="$TIG_ROOT/tigrc"
if [ ! -f "$TIGRC_PATH" ] ; then
  TIG_CLONE_ROOT="$TIG_ROOT/tig"
  TIG_CLONE_URL="https://github.com/cchamberlain/tig"
  mkdirp "$TIG_ROOT" >/dev/null
  if [ -d "$TIG_CLONE_ROOT" ] ; then
    rimraf "$TIG_CLONE_ROOT" >/dev/null
  fi
  pushd "$TIG_ROOT" >/dev/null
    git clone "$TIG_CLONE_URL" &>/dev/null
  popd >/dev/null
  TIG_CLONE_RC_PATH="$TIG_CLONE_ROOT/etc/tigrc"
  cp "$TIG_CLONE_RC_PATH" "$TIGRC_PATH"
fi
. "$TIGRC_PATH"

initialize_flag=false
web_flag=false
mongo_flag=false
build_flag=false
developer_flag=false

usage_short="${BLUE}usage: tig install [-i] [-w] [-m] [-b] [-d]"
usage_long="$usage_short\n-i: initialize core components (default: $initialize_flag)"
usage_long="$usage_long\n-w: install web server components (default: $web_flag)"
usage_long="$usage_long\n-m: install mongodb server components (default: $mongo_flag)"
usage_long="$usage_long\n-b: install build server components (default: $build_flag)"
usage_long="$usage_long\n-d: install developer components (default: $developer_flag)${NC}"

usage_short="$usage_short\nuse -h to get supported command information${NC}"

options=":iwmbdh"
shopt -u nocasematch
OPTIND=1
while getopts "$options" opt ; do
    case "$opt" in
    i )
      exit 0;
      ;;
    w )
      web_flag=true
      ;;
    m )
      mongo_flag=true
      ;;
    b )
      build_flag=true
      web_flag=true
      ;;
    d )
      developer_flag=true
      build_flag=true
      web_flag=true
      ;;
    h )
      >&2 echo -e "$usage_long" && exit 1
      exit 0
      ;;
    \?)
      >&2 echo -e "Unknown option: -$OPTARG"
      >&2 echo -e "$usage_short"
      exit 1
      ;;
    : )
      >&2 echo -e "Missing option argument for -$OPTARG"
      >&2 echo -e "$usage_short"
      exit 1
      ;;
    * )
      >&2 echo -e "Unimplemented option: -$OPTARG" && exit 1
      >&2 echo -e "$usage_short"
      exit 1
    esac
done
shift $((OPTIND-1))


if [ "$web_flag" = true ] ; then
  >&2 echo -e "${CYAN}- installing web server components... -${NC}"

  if ! hash jq 2>/dev/null; then
    >&2 echo -e "${CYAN}- installing jq... -${NC}"
    if [ "$PNAME" = "windows" ]; then
      curl http://stedolan.github.io/jq/download/win64/jq.exe >"${LOCAL_BIN_ROOT}/jq.exe"
    elif ! hash brew 2>/dev/null; then
      brew install jq
    else
      curl http://stedolan.github.io/jq/download/linux64/jq >"${LOCAL_BIN_ROOT}/jq"
    fi
    >&2 echo -e "${GREEN}- jq installed successfully -${NC}"
  fi

  if ! hash forever 2>/dev/null ; then
    >&2 echo -e "${CYAN}- installing forever... -${NC}"
    if hash sudo 2>/dev/null ; then
      sudo npm install -g forever
    else
      npm install -g forever
    fi
    >&2 echo -e "${GREEN}- forever installed successfully -${NC}"
  fi

  >&2 echo -e "${GREEN}- finished installing web server components -${NC}"
fi

if [ "$mongo_flag" = true ] ; then
  if ! hash mongod 2>/dev/null; then
    if [ "$PNAME" = "windows" ] ; then
      >&2 echo -e "${CYAN}- installing mongodb... -${NC}"
      mkdirp "${SRC_BIN_ROOT}"
      pushd "${SRC_BIN_ROOT}"
        curl -L https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-ssl-3.0.3-signed.msi >mongodb_x86_64.msi
        cmd //c msiexec.exe //q //i mongodb-3.0.3-signed.msi INSTALLLOCATION="%USERPROFILE%\\local\\mongodb"
        mkdirp "${TIG_ROOT}/data/db"
      popd
      >&2 echo -e "${GREEN}- mongodb installed successfully -${NC}"
    fi
  fi
fi

if [ "$build_flag" = true ] ; then
  >&2 echo -e "${CYAN}- installing build server components... -${NC}"

  if ! hash gulp 2>/dev/null ; then
    >&2 echo -e "${CYAN}- installing gulp... -${NC}"
    if hash sudo 2>/dev/null ; then
      sudo npm install -g gulp
    else
      npm install -g gulp
    fi
    >&2 echo -e "${GREEN}- gulp installed successfully -${NC}"
  fi

  >&2 echo -e "${GREEN}- finished installing build server components -${NC}"
fi


if [ "$developer_flag" = true ] ; then
  >&2 echo -e "${CYAN}- installing developer components... -${NC}"

  if ! hash chrome 2>/dev/null; then
    if [ "$PNAME" = "windows" ]; then
      >&2 echo -e "${CYAN}- installing chrome... -${NC}"
      cinst google-chrome-x64 -yf
      >&2 echo -e "${GREEN}- chrome installed successfully -${NC}"
    fi
  fi

  if ! hash atom 2>/dev/null; then
    if [ "$PNAME" = "windows" ] ; then
      >&2 echo -e "${CYAN}- installing atom... -${NC}"
      cinst Atom -yf
      >&2 echo -e "${GREEN}- atom installed successfully -${NC}"
    fi
  fi

  if ! hash ack 2>/dev/null; then
    >&2 echo -e "${CYAN}- installing ack... -${NC}"
    curl http://beyondgrep.com/ack-2.14-single-file > "${LOCAL_BIN_ROOT}/ack"
    >&2 echo -e "${GREEN}- ack installed successfully -${NC}"
  fi

  if ! hash nodemon 2>/dev/null; then
    >&2 echo -e "${CYAN}- installing nodemon... -${NC}"
    npm install -g nodemon >/dev/null
    >&2 echo -e "${GREEN}- nodemon installed successfully -${NC}"
  fi

  if ! hash forever 2>/dev/null; then
    >&2 echo -e "${CYAN}- installing forever... -${NC}"
    npm install -g forever >/dev/null
    >&2 echo -e "${GREEN}- forever installed successfully -${NC}"
  fi

  if ! hash ag 2>/dev/null; then
    >&2 echo -e "${CYAN}- Installing ag (the silver searcher)... -${NC}"
    if hash brew 2>/dev/null; then
      brew install the_silver_searcher
      >&2 echo -e "${GREEN}- ag installed successfully -${NC}"
    elif hash pacman 2>/dev/null; then
      pacman -S the_silver_searcher
      >&2 echo -e "${GREEN}- ag installed successfully -${NC}"
    elif hash choco 2>/dev/null; then
      cinst ag -yf &>/dev/null
      >&2 echo -e "${GREEN}- ag installed successfully -${NC}"
    fi
  fi

  if [ "$PNAME" = "windows" ]; then
    ### sysinternals: process explorer
    if ! hash procmon 2>/dev/null ; then
      >&2 echo -e "${CYAN}- installing sysinternals... -${NC}"
      cinst sysinternals -yf
      >&2 echo -e "${GREEN}- sysinternals installed successfully -${NC}"
    fi

  fi

  >&2 echo -e "${GREEN}- finished installing developer components -${NC}"
fi