#!/bin/sh

get_var_value()
{
  expr "$(env | grep "^${1}=")" : "${1}=\(.*\)\$"
}

if [ -z "$(which podman 2>/dev/null)" ]
then
  sudo apk add podman
fi

if [ -z "$(which podman-compose 2>/dev/null)" ]
then
  sudo apk add podman-compose
fi

if [ -z "$(which yq 2>/dev/null)" ]
then
  YQ_PLATFORM="linux_$(expr "$(arch)" : "armv7" && echo arm || expr "$(arch)" : "x86_64" && echo amd64 || arch)"
  sudo wget -O /usr/local/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_${YQ_PLATFORM}
  sudo chmod +x /usr/local/bin/yq
fi

if [ -z "$(which envsubst 2>/dev/null)" ]
then
  if [ "$(arch)" = "x86_64" ] || [ "$(arch)" = "arm64" ]
  then
    ENVSUBST_VERSION=1.4.3
    sudo wget -O /usr/local/bin/envsubst https://github.com/a8m/envsubst/releases/download/v${ENVSUBST_VERSION}/envsubst-Linux-$(arch)
    sudo chmod +x /usr/local/bin/envsubst
  else
    sudo apk add go
    go install github.com/a8m/envsubst/cmd/envsubst@latest
  fi
fi

if [ -z "$(which bash 2>/dev/null)" ]
then
  sudo apk add bash
fi

if [ -z "$(grep "\bjoeac.net\b" /etc/group)" ]
then
  sudo adduser -D -h /home/joeac.net joeac.net
  sudo adduser joeac.net joeac.net
fi

if ! ( expr "$(groups joeac.net)" wheel )
then
  sudo adduser joeac.net wheel
fi

su -l joeac.net
if ! [ $(whoami) = "joeac.net" ]
then
  echo "Could not log in to user: joeac.net. Log in manually and re-run this script as joeac.net."
  exit 1
fi

if ! [ -d /home/joeac.net/joeac.net/.git ]
then
  git clone https://git.joeac.net/joeac/joeac.net.git /home/joeac.net/joeac.net
fi
sudo chown joeac.net:joeac.net /home/joeac.net/joeac.net
sudo chmod 770 /home/joeac.netjoeac.net

if ! [ -f DIGITALOCEAN_TOKEN ]
then
  if [ -z "${DIGITALOCEAN_TOKEN}" ]
  then
    read -sp "DIGITALOCEAN_TOKEN: " DIGITALOCEAN_TOKEN
  fi
  echo ${DIGITALOCEAN_TOKEN} > /home/joeac.net/joeac.net/DIGITALOCEAN_TOKEN
fi

if ! ( podman secret exists remote_smtp_password )
then
  if [ -z "${REMOTE_SMTP_PASSWORD}" ]
  then
    read -sp "REMOTE_SMTP_PASSWORD: " REMOTE_SMTP_PASSWORD
  fi
  echo "${REMOTE_SMTP_PASSWORD}" | podman secret create remote_smtp_password -
  unset REMOTE_SMTP_PASSWORD
fi

while read line
do
  if expr "${line}" : "[[:alnum:]_]\+=" 1>/dev/null
  then
    var_name="$(expr "${line}" : "\([[:alnum:]_]\+\)=")"

    if [ -n "$(grep "^${var_name}=" /home/joeac.net/joeac.net/.env)" ]
    then
      continue
    fi

    if [ -z "$(get_var_value ${var_name})" ]
    then
      default_value="$(expr "${line}" : "[[:alnum:]_]\+=\(.*\)\$")"
      read -sp "${var_name}: " ${var_name}
      if [ -z "$(get_var_value ${var_name})" ]
      then
        eval ${var_name}="${default_value}"
      fi
    fi

    echo "${var_name}=$(get_var_value ${var_name})" >> /home/joeac.net/joeac.net/.env
  fi
done </home/joeac.net/joeac.net/example.env

make --directory=/home/joeac.net/joeac.net
make --directory=/home/joeac.net/joeac.net install
