#!/bin/bash #


# What does this do.. and why?
# (and why is it so complicated?)
#
# What this does
#
#   1. Sets some vars and asks some questions
#   2. Installs everything into a single place
#      (inculding deps like node.js, with the correct version)
#   3. Depending on OS, creates a user for the service
#   4. Depending on OS, register with system launcher
#
# Why
#
#   So that you can get a fully configured, running product,
#   with zero manual configuration in a matter of seconds -
#   and have an uninstall that's just as easy.
#
# Why so complicated?
#
#  To support nuance differences between various versions of
#  Linux, macOS, and Android, including whether it's being
#  installed with user privileges, as root, wit a system user
#  system daemon launcher, etc. Also, this is designed to be
#  reusable with many apps and services, so it's very variabled...

set -e
set -u

### http_bash exported by get.sh

if [ "$(logname)" != "$(id -u -n)" ]; then
  echo "WARNING:"
  echo "    You are logged in as '$(logname)' but acting as '$(id -u -n)'."
  echo "    If the installation is not successful please log in as '$(id -u -n)' directly."
  sleep 3
fi

TELEBIT_VERSION=${TELEBIT_VERSION:-master}
TELEBIT_USERSPACE=${TELEBIT_USERSPACE:-no}
my_email=${1:-}
my_relay=${2:-}
my_servernames=${3:-}
my_secret=${4:-}

cur_user="$(id -u -n)"
TELEBIT_USER="${TELEBIT_USER:-$cur_user}"
my_user="$TELEBIT_USER"

cur_group="$(id -g -n)"
TELEBIT_GROUP="${TELEBIT_GROUP:-$cur_group}"
my_group="$TELEBIT_GROUP"

my_app_pkg_name="cloud.telebit.remote"
my_app="telebit"
my_daemon="telebitd"
my_bin="telebit.js"
my_name="Telebit Remote"
my_repo="telebit.js"
my_root=${my_root:-} # todo better install script
soft_sudo_cmd="sudo"
soft_sudo_cmde="sudo "
exec 3<>/dev/tty
read_cmd="read -u 3"
# TODO detect if rsync is available and use rsync -a (more portable)
rsync_cmd="cp -pPR"

set +e
my_edit=$(basename "${EDITOR:-}")
if [ -z "$my_edit" ]; then
  my_edit=$(basename "$(type -p edit)")
fi
if [ -z "$my_edit" ]; then
  my_edit=$(basename "$(type -p nano)")
fi
if [ -z "$my_edit" ]; then
  my_edit=$(basename "$(type -p vim)")
fi
if [ -z "$my_edit" ]; then
  my_edit=$(basename "$(type -p vi)")
fi
if [ -z "$my_edit" ]; then
  my_edit="nano"
fi
set -e

if [ "root" == $(whoami) ] || [ 0 == $(id -u) ]; then
  soft_sudo_cmd=" "
  soft_sudo_cmde=""
fi

echo ""

TELEBIT_REAL_PATH=${TELEBIT_PATH:-}

if [ $(id -u) -ne 0 ] && [ "$TELEBIT_USER" == "$cur_user" ]; then
  TELEBIT_USERSPACE="yes"
  if [ -z "${TELEBIT_REAL_PATH:-}" ]; then
    echo 'TELEBIT_PATH="'${TELEBIT_REAL_PATH:-}'"'
    TELEBIT_REAL_PATH=$HOME/Applications/$my_app
  fi
else
  TELEBIT_USERSPACE="no"
  if [ -z "${TELEBIT_REAL_PATH:-}" ]; then
    echo 'TELEBIT_PATH="'${TELEBIT_REAL_PATH:-}'"'
    TELEBIT_REAL_PATH=/opt/$my_app
  fi
fi
TELEBIT_PATH="$TELEBIT_REAL_PATH"
TELEBIT_TMP="$TELEBIT_REAL_PATH"
my_tmp="$(mktemp -d)"
#TELEBIT_TMP="$my_tmp/telebit"

echo "Installing $my_name to '$TELEBIT_REAL_PATH'"
# v10.2+ has much needed networking fixes, but breaks ursa. v9.x has severe networking bugs. v8.x has working ursa, but requires tls workarounds"
NODEJS_VER="${NODEJS_VER:-v10.2}"
export NODEJS_VER
export NODE_PATH="$TELEBIT_TMP/lib/node_modules"
export NPM_CONFIG_PREFIX="$TELEBIT_TMP"
# this comes last for security
export PATH="$PATH:$TELEBIT_REAL_PATH/bin"
sleep 0.25
echo "(your password may be required to begin the installation)"
real_sudo_cmd=$soft_sudo_cmd
real_sudo_cmde=$soft_sudo_cmde

set +e
mkdir -p $my_tmp "$TELEBIT_REAL_PATH" "$TELEBIT_REAL_PATH/etc" "$TELEBIT_REAL_PATH/var/log" 2>/dev/null && \
  chown -R $(id -u -n):$(id -g -n) $my_tmp "$TELEBIT_REAL_PATH" 2>/dev/null
if [ $? -eq 0 ]; then
  soft_sudo_cmd=" "
  soft_sudo_cmde=""
else
  $soft_sudo_cmd mkdir -p $my_tmp "$TELEBIT_REAL_PATH" "$TELEBIT_REAL_PATH/etc" "$TELEBIT_REAL_PATH/var/log"
  $soft_sudo_cmd chown -R $(id -u -n):$(id -g -n) $my_tmp "$TELEBIT_REAL_PATH"
fi
set -e


echo "  - installing node.js runtime to '$TELEBIT_REAL_PATH'..."
http_bash https://git.coolaj86.com/coolaj86/node-installer.sh/raw/branch/master/install.sh --no-dev-deps >/dev/null 2>/dev/null

#
# TODO create "upgrade" script and run that instead
#

my_node="$TELEBIT_REAL_PATH/bin/node"
tmp_node="$TELEBIT_TMP/bin/node"
my_npm="$my_node $TELEBIT_TMP/bin/npm"
tmp_npm="$tmp_node $TELEBIT_TMP/bin/npm"

#https://git.coolaj86.com/coolaj86/telebit.js.git
#https://git.coolaj86.com/coolaj86/telebit.js/archive/:tree:.tar.gz
#https://git.coolaj86.com/coolaj86/telebit.js/archive/:tree:.zip
set +e
my_unzip=$(type -p unzip)
my_tar=$(type -p tar)
# TODO extract to temporary directory, configure, copy etc, replace
if [ -n "$my_unzip" ]; then
  rm -f $my_tmp/$my_app-$TELEBIT_VERSION.zip
  echo "  - installing telebit zip to '$TELEBIT_REAL_PATH'..."
  http_get https://git.coolaj86.com/coolaj86/$my_repo/archive/$TELEBIT_VERSION.zip $my_tmp/$my_app-$TELEBIT_VERSION.zip
  # -o means overwrite, and there is no option to strip
  $my_unzip -o $my_tmp/$my_app-$TELEBIT_VERSION.zip -d $my_tmp/ >/dev/null
  $rsync_cmd  $my_tmp/$my_repo/* $TELEBIT_TMP/ > /dev/null
  rm -rf $my_tmp/$my_repo
elif [ -n "$my_tar" ]; then
  rm -f $my_tmp/$my_app-$TELEBIT_VERSION.tar.gz
  echo "  - installing telebit tar.gz to '$TELEBIT_REAL_PATH'..."
  http_get https://git.coolaj86.com/coolaj86/$my_repo/archive/$TELEBIT_VERSION.tar.gz $my_tmp/$my_app-$TELEBIT_VERSION.tar.gz
  $my_tar -xzf $my_tmp/$my_app-$TELEBIT_VERSION.tar.gz --strip 1 -C $TELEBIT_TMP/ >/dev/null
else
  echo "Neither tar nor unzip found. Abort."
  exit 13
fi
set -e

#
# TODO create slim packages that contain all the deps on each os and cpu
#
pushd $TELEBIT_TMP >/dev/null
  echo "  - installing telebit npm dependencies to '$TELEBIT_REAL_PATH'..."
  echo "    (are you noticing a pattern of where things are installed?)"
  $tmp_npm install >/dev/null 2>/dev/null
popd >/dev/null

echo "  - configuring telebit..."
echo ""

###############################################
#
# TODO convert to node script
#
# Now that node is installed and the telebit
# packeage is downloaded, everything can be
# run from node, except things requiring sudo
#
###############################################

# telebit remote
echo '#!/bin/bash' > "$TELEBIT_TMP/bin/$my_app"
echo "$my_node $TELEBIT_REAL_PATH/bin/$my_bin "'"$@"' >> "$TELEBIT_TMP/bin/$my_app"
chmod a+x "$TELEBIT_TMP/bin/$my_app"

# telebit daemon
echo '#!/bin/bash' > "$TELEBIT_TMP/bin/$my_daemon"
echo "$my_node $TELEBIT_REAL_PATH/bin/$my_daemon.js daemon "'"$@"' >> "$TELEBIT_TMP/bin/$my_daemon"
chmod a+x "$TELEBIT_TMP/bin/$my_daemon"

# Create uninstall script based on the install script variables
cat << EOF > $TELEBIT_TMP/bin/${my_app}_uninstall
#!/bin/bash
set -x
if [ "$(type -p launchctl)" ]; then
  sudo launchctl unload -w /Library/LaunchDaemons/${my_app_pkg_name}.plist
  sudo rm -f /Library/LaunchDaemons/${my_app_pkg_name}.plist

  launchctl unload -w $HOME/Library/LaunchAgents/${my_app_pkg_name}.plist
  rm -f $HOME/Library/LaunchAgents/${my_app_pkg_name}.plist
fi
if [ "$(type -p systemctl)" ]; then
  systemctl --user disable $my_app >/dev/null
  systemctl --user stop $my_app
  rm -f $HOME/.config/systemd/user/$my_app.service

  sudo systemctl disable $my_app >/dev/null
  sudo systemctl stop $my_app
  sudo rm -f /etc/systemd/system/$my_app.service
fi
sudo rm -rf $TELEBIT_REAL_PATH /usr/local/bin/$my_app
sudo rm -rf $TELEBIT_REAL_PATH /usr/local/bin/$my_daemon
rm -rf $HOME/.config/$my_app $HOME/.local/share/$my_app
EOF
chmod a+x $TELEBIT_TMP/bin/${my_app}_uninstall

echo""
echo "(your password may be required to complete the installation)"
echo""

#set +e
#if type -p setcap >/dev/null 2>&1; then
#  #echo "Setting permissions to allow $my_app to run on port 80 and port 443 without sudo or root"
#  echo "    > ${real_sudo_cmde}setcap cap_net_bind_service=+ep $TELEBIT_REAL_PATH/bin/node"
#  $real_sudo_cmd setcap cap_net_bind_service=+ep $TELEBIT_REAL_PATH/bin/node
#fi
#set -e

my_skip=""
set +e
# TODO for macOS https://apple.stackexchange.com/questions/286749/how-to-add-a-user-from-the-command-line-in-macos
# TODO do stuff for groups too
# TODO add ending $
if type -p dscl >/dev/null 2>/dev/null; then
  if [ -n "$(dscl . list /users | grep ^$my_user)" ] && [ -n "$(dscl . list /groups | grep ^$my_group)" ]; then
    my_skip="yes"
  fi
elif [ -n "$(cat $my_root/etc/passwd | grep $my_user)" ] && [ -n "$(cat $my_root/etc/group | grep $my_group)" ]; then
  my_skip="yes"
fi

if [ -z "$my_skip" ]; then
  if type -p adduser >/dev/null 2>/dev/null; then
    $real_sudo_cmd adduser --home $TELEBIT_REAL_PATH --gecos '' --disabled-password $my_user >/dev/null 2>&1
    #my_user=$my_app_name
    my_group=$my_user
  elif [ -n "$(cat /etc/passwd | grep www-data:)" ]; then
    # Linux (Ubuntu)
    my_user=www-data
    my_group=www-data
  elif [ -n "$(cat /etc/passwd | grep _www:)" ]; then
    # Mac
    my_user=_www
    my_group=_www
  else
    # Unsure
    my_user=$(id -u -n) # $(whoami)
    my_group=$(id -g -n)
  fi
fi
set -e

export TELEBIT_USER=$my_user
export TELEBIT_GROUP=$my_group
export TELEBIT_PATH
TELEBIT_CONFIG=$HOME/.config/$my_app.yml
# TODO check both expected sock paths in client by default
if [ "yes" == "$TELEBIT_USERSPACE" ]; then
  TELEBIT_TMP_CONFIGD=$HOME/.config/$my_daemon.yml
  TELEBIT_CONFIGD=$HOME/.config/$my_daemon.yml
  TELEBIT_SOCK=$HOME/.local/share/telebit/var/run/telebit.sock
else
  TELEBIT_TMP_CONFIGD=$TELEBIT_TMP/etc/$my_daemon.yml
  TELEBIT_CONFIGD=$TELEBIT_REAL_PATH/etc/$my_daemon.yml
  TELEBIT_SOCK=$TELEBIT_REAL_PATH/var/run/telebit.sock
fi
$my_node $TELEBIT_TMP/usr/share/template-launcher.js

# TODO don't create this in TMP_PATH if it exists in TELEBIT_REAL_PATH
mkdir -p "$(dirname $TELEBIT_TMP_CONFIGD)"
if [ ! -e "$TELEBIT_CONFIGD" ]; then

  echo "sock: $TELEBIT_SOCK" >> "$TELEBIT_TMP_CONFIGD"
  echo "root: $TELEBIT_REAL_PATH" >> "$TELEBIT_TMP_CONFIGD"
  cat $TELEBIT_REAL_PATH/usr/share/$my_daemon.tpl.yml >> "$TELEBIT_TMP_CONFIGD"

fi

mkdir -p "$(dirname $TELEBIT_CONFIG)"
if [ ! -e "$TELEBIT_CONFIG" ]; then

  echo "sock: $TELEBIT_SOCK" >> "$TELEBIT_CONFIG"

fi



# TODO
# Backup final directory, if it exists
# Move everything over to final directory
# Restore config files, if they exist
# rewrite system service file with real variables

# This should only affect non-USERSPACE installs
#echo "${soft_sudo_cmde}chown -R $my_user '$TELEBIT_REAL_PATH'
$soft_sudo_cmd chown -R $my_user "$TELEBIT_REAL_PATH"

# $HOME/.config/systemd/user/
# %h/.config/telebit/telebit.yml
echo "  - adding $my_app as a system service"
# TODO detect with type -p
my_system_launcher=""
my_app_launchd_service=""
if [ -d "/Library/LaunchDaemons" ]; then
  my_system_launcher="launchd"
  my_sudo_cmde="$real_sudo_cmde"
  my_sudo_cmd="$real_sudo_cmd"


  if [ "yes" == "$TELEBIT_USERSPACE" ]; then
    my_app_launchd_service_skel="etc/skel/Library/LaunchAgents/${my_app_pkg_name}.plist"
    my_app_launchd_service="$HOME/Library/LaunchAgents/${my_app_pkg_name}.plist"
    echo "    > $rsync_cmd $TELEBIT_REAL_PATH/usr/share/dist/$my_app_launchd_service $my_app_launchd_service"
    mdir -p $HOME/Library/LaunchAgents
    $rsync_cmd "$TELEBIT_REAL_PATH/usr/share/dist/$my_app_launchd_service_skel" "$my_app_launchd_service"

    echo "    > chown $(id -u -n):$(id -g -n) $my_app_launchd_service"
    chown $(id -u -n):$(id -g -n) "$my_app_launchd_service"
    my_sudo_cmd=""
    my_sudo_cmde=""
  else
    my_app_launchd_service_skel="usr/share/dist/Library/LaunchDaemons/${my_app_pkg_name}.plist"
    my_app_launchd_service="$my_root/Library/LaunchDaemons/${my_app_pkg_name}.plist"
    echo "    > ${real_sudo_cmde}$rsync_cmd $TELEBIT_REAL_PATH/usr/share/dist/$my_app_launchd_service $my_app_launchd_service"
    $real_sudo_cmd $rsync_cmd "$TELEBIT_REAL_PATH/usr/share/dist/$my_app_launchd_service_skel" "$my_app_launchd_service"

    echo "    > ${real_sudo_cmde}chown root:wheel $my_app_launchd_service"
    $real_sudo_cmd chown root:wheel "$my_app_launchd_service"
  fi

  echo "    > ${my_sudo_cmde}launchctl unload -w $my_app_launchd_service >/dev/null 2>/dev/null"
  $my_sudo_cmd launchctl unload -w "$my_app_launchd_service" >/dev/null 2>/dev/null

elif [ -d "$my_root/etc/systemd/system" ]; then
  my_system_launcher="systemd"

  if [ "yes" == "$TELEBIT_USERSPACE" ]; then
    echo "    > $rsync_cmd $TELEBIT_REAL_PATH/usr/share/dist/etc/skel/.config/systemd/user/$my_app.service $HOME/.config/systemd/user/$my_app.service"
    mkdir -p $HOME/.config/systemd/user
    $rsync_cmd "$TELEBIT_REAL_PATH/usr/share/dist/etc/skel/.config/systemd/user/$my_app.service" "$HOME/.config/systemd/user/$my_app.service"
  else
    echo "    > ${real_sudo_cmde}$rsync_cmd $TELEBIT_REAL_PATH/usr/share/dist/etc/systemd/system/$my_app.service /etc/systemd/system/$my_app.service"
    $real_sudo_cmd $rsync_cmd "$TELEBIT_REAL_PATH/usr/share/dist/etc/systemd/system/$my_app.service" "/etc/systemd/system/$my_app.service"
  fi
fi

sleep 1

###############################
# Actually Launch the Service #
###############################
echo ""
if [ "launchd" == "$my_system_launcher" ]; then

  if [ "yes" == "$TELEBIT_USERSPACE" ]; then
    echo "  > launchctl load -w $my_app_launchd_service"
    launchctl load -w "$my_app_launchd_service"
  else
    echo "  > ${real_sudo_cmde}launchctl load -w $my_app_launchd_service"
    $real_sudo_cmd launchctl load -w "$my_app_launchd_service"
  fi

elif [ "systemd" == "$my_system_launcher" ]; then

  if [ "yes" == "$TELEBIT_USERSPACE" ]; then
    # https://wiki.archlinux.org/index.php/Systemd/User
    # sudo loginctl enable-linger username

    echo "    > systemctl --user enable $my_app"
    systemctl --user daemon-reload
    systemctl --user enable $my_app >/dev/null
    #echo "    > systemctl --user enable systemd-tmpfiles-setup.service systemd-tmpfiles-clean.timer"
    #systemctl --user enable systemd-tmpfiles-setup.service systemd-tmpfiles-clean.timer
    echo "    > systemctl --user start $my_app"
    systemctl --user daemon-reload
    systemctl --user stop $my_app 2>/dev/null
    systemctl --user start $my_app
    sleep 1
    systemctl --user status $my_app
  else

    $real_sudo_cmd systemctl daemon-reload
    echo "    > ${real_sudo_cmde}systemctl enable $my_app"
    $real_sudo_cmd systemctl enable $my_app >/dev/null
    echo "    > ${real_sudo_cmde}systemctl start $my_app"
    $real_sudo_cmd systemctl daemon-reload
    $real_sudo_cmd systemctl restart $my_app
    sleep 1
    $real_sudo_cmd systemctl status $my_app
  fi

else

  echo "Run the service manually (we couldn't detect your system service to do that automatically):"
  echo ""
  echo "    $my_daemon --config $TELEBIT_CONFIGD"
  echo "    $my_app --config $TELEBIT_CONFIG"

fi

echo "    > ${real_sudo_cmde}ln -sf $TELEBIT_REAL_PATH/bin/$my_app /usr/local/bin/$my_app"
ln -sf $TELEBIT_REAL_PATH/bin/$my_app /usr/local/bin/$my_app 2>/dev/null || \
  $real_sudo_cmd ln -sf $TELEBIT_REAL_PATH/bin/$my_app /usr/local/bin/$my_app
echo "    > ${real_sudo_cmde}ln -sf $TELEBIT_REAL_PATH/bin/$my_daemon /usr/local/bin/$my_daemon"
ln -sf $TELEBIT_REAL_PATH/bin/$my_daemon /usr/local/bin/$my_daemon || \
  $real_sudo_cmd ln -sf $TELEBIT_REAL_PATH/bin/$my_daemon /usr/local/bin/$my_daemon


echo "  > telebit init --tty"
echo ""
sleep 0.25

$TELEBIT_REAL_PATH/bin/node $TELEBIT_REAL_PATH/bin/telebit.js init --tty

$TELEBIT_REAL_PATH/bin/node $TELEBIT_REAL_PATH/bin/telebit.js enable