#!/usr/bin/env bash

readonly FILES=(
  "./config/motion.conf"
  "./config/msmtprc"
  "./config/muttrc"
  "./scripts/alert"
  "./scripts/cameras"
  "./scripts/notify"
  "./scripts/sync"
)

B2_BUCKET="my-bucket-name"
CAMERA_NAME="Kitchen"
DOMAIN="${HOSTNAME}"
EMAIL_HOST="smtp.gmail.com"
EMAIL_PORT="587"
EMAIL_USER="user@example.com"
MOTION_DIR="/var/lib/motion"
MOTION_LOG_FILE="/var/log/motion/motion.log"
MOTION_PASSWORD="$(tr -dc A-Za-z0-9 </dev/urandom | head -c 24 ; echo '')"
MOTION_USER="${USER}"
NOTIFICATION_RECIPIENT="1234567890@sms.example.com"
PORT_CONTROL="8080"
PORT_STREAM="3000"
TLS_CERT="/etc/letsencrypt/live/${DOMAIN}/fullchain.pem"
TLS_KEY="/etc/letsencrypt/live/${DOMAIN}/privkey.pem"

input_settings() {
  local input

  printf "B2 Bucket (%s): " "${B2_BUCKET}"
  read -r input
  B2_BUCKET="${input:-$B2_BUCKET}"

  printf "Camera name (%s): " "${CAMERA_NAME}"
  read -r input
  CAMERA_NAME="${input:-$CAMERA_NAME}"

  printf "Domain (%s): " "${DOMAIN}"
  read -r input
  DOMAIN="${input:-$DOMAIN}"

  printf "Email host (%s): " "${EMAIL_HOST}"
  read -r input
  EMAIL_HOST="${input:-$EMAIL_HOST}"

  printf "Email port (%s): " "${EMAIL_PORT}"
  read -r input
  EMAIL_PORT="${input:-$EMAIL_PORT}"

  printf "Email user (%s): " "${EMAIL_USER}"
  read -r input
  EMAIL_USER="${input:-$EMAIL_USER}"

  printf "Email password (%s): " "${EMAIL_PASSWORD}"
  read -r input
  EMAIL_PASSWORD="${input:-$EMAIL_PASSWORD}"

  printf "Motion directory (%s): " "${MOTION_DIR}"
  read -r input
  MOTION_DIR="${input:-$MOTION_DIR}"

  printf "Motion log file (%s): " "${MOTION_LOG_FILE}"
  read -r input
  MOTION_LOG_FILE="${input:-$MOTION_LOG_FILE}"

  printf "Motion user (%s): " "${MOTION_USER}"
  read -r input
  MOTION_USER="${input:-$MOTION_USER}"

  printf "Motion password (%s): " "${MOTION_PASSWORD}"
  read -r input
  MOTION_PASSWORD="${input:-$MOTION_PASSWORD}"

  printf "Notification recipient (%s): " "${NOTIFICATION_RECIPIENT}"
  read -r input
  NOTIFICATION_RECIPIENT="${input:-$NOTIFICATION_RECIPIENT}"

  printf "Webcontrol port (%s): " "${PORT_CONTROL}"
  read -r input
  PORT_CONTROL="${input:-$PORT_CONTROL}"

  printf "Livestream port (%s): " "${PORT_STREAM}"
  read -r input
  PORT_STREAM="${input:-$PORT_STREAM}"

  printf "TLS certifcate path (%s): " "${TLS_CERT}"
  read -r input
  TLS_CERT="${input:-$TLS_CERT}"

  printf "TLS key path (%s): " "${TLS_KEY}"
  read -r input
  TLS_KEY="${input:-$TLS_KEY}"
}

print_settings() {
  printf "Your settings:\n"
  printf "B2 Bucket: %s\n" "${B2_BUCKET}"
  printf "Camera name: %s\n" "${CAMERA_NAME}"
  printf "Domain: %s\n" "${DOMAIN}"
  printf "Email host: %s\n" "${EMAIL_HOST}"
  printf "Email port: %s\n" "${EMAIL_PORT}"
  printf "Email user: %s\n" "${EMAIL_USER}"
  printf "Email password: %s\n" "${EMAIL_PASSWORD}"
  printf "Motion directory: %s\n" "${MOTION_DIR}"
  printf "Motion log file: %s\n" "${MOTION_LOG_FILE}"
  printf "Motion user: %s\n" "${MOTION_USER}"
  printf "Motion password: %s\n" "${MOTION_PASSWORD}"
  printf "Webcontrol port: %s\n" "${PORT_CONTROL}"
  printf "Livestream port: %s\n" "${PORT_STREAM}"
  printf "Notification recipient: %s\n" "${NOTIFICATION_RECIPIENT}"
  printf "TLS certificate: %s\n" "${TLS_CERT}"
  printf "TLS key: %s\n" "${TLS_KEY}"
}

write_settings() {
  for f in "${FILES[@]}"; do
    sed -i 's|\${B2_BUCKET}|'"${B2_BUCKET}"'|g' "${f}"
    sed -i 's|\${CAMERA_NAME}|'"${CAMERA_NAME}"'|g' "${f}"
    sed -i 's|\${DOMAIN}|'"${DOMAIN}"'|g' "${f}"
    sed -i 's|\${EMAIL_HOST}|'"${EMAIL_HOST}"'|g' "${f}"
    sed -i 's|\${EMAIL_PASSWORD}|'"${EMAIL_PASSWORD}"'|g' "${f}"
    sed -i 's|\${EMAIL_PORT}|'"${EMAIL_PORT}"'|g' "${f}"
    sed -i 's|\${EMAIL_USER}|'"${EMAIL_USER}"'|g' "${f}"
    sed -i 's|\${MOTION_DIR}|'"${MOTION_DIR}"'|g' "${f}"
    sed -i 's|\${MOTION_LOG_FILE}|'"${MOTION_LOG_FILE}"'|g' "${f}"
    sed -i 's|\${MOTION_PASSWORD}|'"${MOTION_PASSWORD}"'|g' "${f}"
    sed -i 's|\${MOTION_USER}|'"${MOTION_USER}"'|g' "${f}"
    sed -i 's|\${NOTIFICATION_RECIPIENT}|'"${NOTIFICATION_RECIPIENT}"'|g' "${f}"
    sed -i 's|\${PORT_CONTROL}|'"${PORT_CONTROL}"'|g' "${f}"
    sed -i 's|\${PORT_STREAM}|'"${PORT_STREAM}"'|g' "${f}"
    sed -i 's|\${TLS_CERT}|'"${TLS_CERT}"'|g' "${f}"
    sed -i 's|\${TLS_KEY}|'"${TLS_KEY}"'|g' "${f}"
  done
}

link_files() {
  chmod 400 "${PWD}/config/msmtprc"

  ln -s "${PWD}/config/motion.conf" "/etc/motion/motion.conf"
  ln -s "${PWD}/config/msmtprc" "${MOTION_DIR}/.msmtprc"
  ln -s "${PWD}/config/muttrc"  "${MOTION_DIR}/.muttrc"
  ln -s "${PWD}/scripts/alert" "${MOTION_DIR}/alert"
  ln -s "${PWD}/scripts/notify" "${MOTION_DIR}/notify"
  ln -s "${PWD}/scripts/sync" "${MOTION_DIR}/sync"
}

main() {
  input_settings

  print_settings

  echo "Update files?"
  select yn in "yes" "no"; do
    case "${yn}" in
      yes ) write_settings ;;
      no ) exit 0 ;;
    esac
  done

  echo "Link files?"
  select yn in "yes" "no"; do
    case "${yn}" in
      yes ) link_files && exit 0 ;;
      no ) exit 0 ;;
    esac
  done
}

main "$@"
