#!/bin/bash
# Check if golang exists by the output of "go version"
if [ "$(go version | head -c 13)" == "go version go" ]; then
  echo "Assuming go is installed"
  shouldbuild="neigh"
  # Check location for simple vpn binary
  if [ -f /usr/bin/simple-vpn ]; then
    echo "Found \"/usr/bin/simple-vpn\" assuming it is installed correctly"
    echo "Run this script with force as a first argument to force a re-install"
  else
    shouldbuild="ye"
  fi
  # Look for an argument to force simple vpn re-install
  if [ "$1" = "force" ]; then
    shouldbuild="ye"
    echo "Install enforced!"
  fi
  if [ "$shouldbuild" = "ye" ]; then
    # Check for git repo existence if forcing re-install and delete if it does
    if [ -d simple-vpn ]; then
      echo "Found previous simple-vpn git download in working directory:"
      echo "Deleting!"
      rm -f -R simple-vpn
    fi
    # Clone github repo
    echo "Cloning simple-vpn"
    git clone https://codehub.onpointcoding.net/sean/simple-vpn.git
    cd simple-vpn

    # Build and copy binary to bin directory
    echo "Installing simple-vpn"
    go build
    sudo cp ./simple-vpn /usr/bin/ -i
    cd ..

    # Remove repository
    echo "Cleaning up"
    rm simple-vpn -rf
    echo "Components installed..."
  fi
else
  echo "Please install golang before continuing"
  exit
fi