#!/bin/bash
set -e
set -o pipefail

FFMPEG_INSTALL_DIR="/usr/lib/audiobookshelf-ffmpeg/"

declare -r init_type='auto'
declare -ri no_rebuild='0'

add_user() {
  : "${1:?'User was not defined'}"
  declare -r user="$1"
  declare -r uid="$2"

  if [ -z "$uid" ]; then
    declare -r uid_flags=""
  else
    declare -r uid_flags="--uid $uid"
  fi

  declare -r group="${3:-$user}"
  declare -r descr="${4:-No description}"
  declare -r shell="${5:-/bin/false}"

  if ! getent passwd | grep -q "^$user:"; then
    echo "Creating system user: $user in $group with $descr and shell $shell"
    useradd $uid_flags --gid $group --no-create-home --system --shell $shell -c "$descr" $user
  fi
}

add_group() {
  : "${1:?'Group was not defined'}"
  declare -r group="$1"
  declare -r gid="$2"

  if [ -z "$gid" ]; then
    declare -r gid_flags=""
  else
    declare -r gid_flags="--gid $gid"
  fi

  if ! getent group | grep -q "^$group:" ; then
    echo "Creating system group: $group"
    groupadd $gid_flags --system $group
  fi
}

start_service () {
  : "${1:?'Service name was not defined'}"
  declare -r service_name="$1"

  if hash systemctl 2> /dev/null; then
    if [[ "$init_type" == 'auto' || "$init_type" == 'systemd' ]]; then
      {
        systemctl enable "$service_name.service" && \
        systemctl start "$service_name.service"
      } || echo "$service_name could not be registered or started"
    fi
  elif hash service 2> /dev/null; then
    if [[ "$init_type" == 'auto' || "$init_type" == 'upstart' || "$init_type" == 'sysv' ]]; then
      service "$service_name" start || echo "$service_name could not be registered or started"
    fi
  elif hash start 2> /dev/null; then
    if [[ "$init_type" == 'auto' || "$init_type" == 'upstart' ]]; then
      start "$service_name" || echo "$service_name could not be registered or started"
    fi
  elif hash update-rc.d 2> /dev/null; then
      if [[ "$init_type" == 'auto' || "$init_type" == 'sysv' ]]; then
      {
        update-rc.d "$service_name" defaults && \
        "/etc/init.d/$service_name" start
      } || echo "$service_name could not be registered or started"
    fi
  else
    echo 'Your system does not appear to use systemd, Upstart, or System V, so the service could not be started'
  fi
}


add_group 'audiobookshelf' ''
add_user 'audiobookshelf' '' 'audiobookshelf' 'audiobookshelf user-daemon' '/bin/false'

mkdir -p '/var/log/audiobookshelf'
chown -R 'audiobookshelf:audiobookshelf' '/var/log/audiobookshelf'
chown -R 'audiobookshelf:audiobookshelf' '/usr/share/audiobookshelf'
chown -R 'audiobookshelf:audiobookshelf' "$FFMPEG_INSTALL_DIR"

start_service 'audiobookshelf'
