#!/bin/bash

# Not every platform has or needs sudo, gotta save them O(1)s...
sudo_cmd=""
((EUID)) && [[ -z "$ANDROID_ROOT" ]] && sudo_cmd="sudo"

set -e
set -u

###############################
#                             #
# boilerplate for curl / wget #
#                             #
###############################

http_get=""
http_opts=""
http_out=""

detect_http_get()
{
  if type -p curl >/dev/null 2>&1; then
    http_get="curl"
    http_opts="-fsSL"
    http_out="-o"
    #curl -fsSL "$caddy_url" -o "$PREFIX/tmp/$caddy_pkg"
  elif type -p wget >/dev/null 2>&1; then
    http_get="wget"
    http_opts="--quiet"
    http_out="-O"
    #wget --quiet "$caddy_url" -O "$PREFIX/tmp/$caddy_pkg"
  else
    echo "Aborted, could not find curl or wget"
    return 7
  fi
}

dap_dl()
{
  $http_get $http_opts $http_out "$2" "$1"
}

dap_dl_bash()
{
  dap_url=$1
  #dap_args=$2
  rm -rf dap-tmp-runner.sh
  $http_get $http_opts $http_out dap-tmp-runner.sh "$dap_url"; bash dap-tmp-runner.sh; rm dap-tmp-runner.sh
}

detect_http_get

###############################
#                             #
# actual script continues...  #
#                             #
###############################

install_walnut()
{
  $sudo_cmd mkdir -p /srv/walnut/{var,etc,packages,node_modules}
  # www-data exists on linux, _www exists on mac OS
  $sudo_cmd chown -R $(whoami):www-data /srv/walnut || $sudo_cmd chown -R $(whoami):_www /srv/walnut
  if [ ! -d "/srv/walnut/core/" ]; then
    git clone https://git.daplie.com/Daplie/walnut.js.git /srv/walnut/core
  fi
  pushd /srv/walnut/core
    if [ ! -d "./.git/" ]; then
      echo "'/srv/walnut/core' exists but is not a git repository... not sure what to do here..."
    fi
    git checkout v1.2
    git pull
  popd
  rm -rf /srv/walnut/core/node_modules
  ln -sf ../node_modules /srv/walnut/core/node_modules
  /srv/walnut/core/install-helper.sh /srv/walnut
  # Now that the install is finished we need to set the owner to the user that will actually
  # be running the walnut server.
  $sudo_cmd chown -R www-data:www-data /srv/walnut || $sudo_cmd chown -R _www:_www /srv/walnut
}

# Install node
echo "----Installing Nodejs and NPM----"
echo "v8.2.1" > /tmp/NODEJS_VER
daplie-install-node-dev
npm install -g npm@4

# Install goldilocks
echo "----Installing goldilocks.js----"
daplie-install-goldilocks

echo "----Installing walnut.js----"
#$sudo_cmd mkdir -p /opt/goldilocks/{lib,bin,etc}
#export NODE_PATH=/opt/walnut/lib/node_modules
#export NPM_CONFIG_PREFIX=/opt/walnut
old_PATH=$PATH
export PATH=/opt/walnut/bin:$PATH

# Install walnut
install_walnut

# Install bower, some systems may be missing it, and it is a dependency
/opt/walnut/bin/npm install -g bower
touch /.bowerrc
echo '{ "allow_root": true }' > /.bowerrc

# Restore PATH to original value
export PATH=$old_PATH

echo ""
echo "You must have some set of domain set up to properly use goldilocks+walnut:"
echo ""
echo "  example.com"
echo "  www.example.com"
echo "  api.example.com"
echo "  assets.example.com"
echo "  cloud.example.com"
echo "  api.cloud.example.com"
echo ""
echo "Check the WALNUT README.md for more info and how to set up /etc/goldilocks/goldilocks.yml"
echo ""
