#!/bin/bash

USER="ms"
GROUP="unifi-streaming"
EXTERNAL_MINIMUM_SPACE=54683238
SRV_DIR="/srv"
SSD1_DIR="/ssd1"
JSON_DB_DIR="/etc/unifi-protect/jsonDb"

version=$(dpkg-query -W -f='${Version}' msr 2>/dev/null)
echo "#-> MSR $version pre-start"

if [ -z ${MS_TMPFS_DIR} ]; then
  echo "MS_TMPFS_DIR is not set - using default /var/opt/ms/tmp"
  MS_TMPFS_DIR="/var/opt/ms/tmp"
fi

if [ -z ${MS_INTERNAL_DIR} ]; then
  echo "MS_INTERNAL_DIR is not set - using default /data/ms"
  MS_INTERNAL_DIR="/data/ms"
fi

if [ -z ${MS_EXTERNAL_DIR} ]; then
  echo "MS_EXTERNAL_DIR is not set - using default /srv/ms"
  MS_EXTERNAL_DIR="/srv/ms"
fi


is_link() {
  [ -L "$1" ] && [ -e "$1" ] && return
  false
}

get_disk_space() {
  df "$1" -k | tail -n 1 | awk '{printf $2}'
}

isSrvReadOnly() {
  if ! command -v findmnt &> /dev/null; then
    # ignore if findmnt not found
    return 1
  fi
  # report RO state only if target mounted
  echo "Read Only file system check"
  local TMP=$(findmnt --target $SRV_DIR -o SOURCE,OPTIONS -n)
  echo $TMP
  echo $TMP | grep -E '^ro$|,ro$|,ro,|^ro,| ro'
  return $?
}

usermod -g ${GROUP} ${USER}

if [ -d "/etc" ]; then
  mkdir -p /etc/msr || true
  chown -R ${USER}:${GROUP} /etc/msr || true
  chmod -R ug+rw /etc/msr || true
fi

if [ -d $JSON_DB_DIR ]; then
  echo "Make sure JSON DB directory $JSON_DB_DIR is readable"
  chown -R :${GROUP} "$JSON_DB_DIR" || true
  chmod -R ug+r "$JSON_DB_DIR" || true
fi

# External storage available
if (mountpoint -q $SRV_DIR || is_link $SRV_DIR) && [ $(get_disk_space $SRV_DIR) -ge $EXTERNAL_MINIMUM_SPACE ] && (! isSrvReadOnly); then
  echo "External storage available"

  if [ ! -d $MS_EXTERNAL_DIR ]; then
    echo "Creating external directory $MS_EXTERNAL_DIR"
    mkdir -p "${MS_EXTERNAL_DIR}/logs"
    echo "Changing owner for $MS_EXTERNAL_DIR to $USER:$GROUP"
    chown ${USER}:${GROUP} "${MS_EXTERNAL_DIR}"
    echo "Setting group permissions for $MS_EXTERNAL_DIR"
    chmod -R g+rw "${MS_EXTERNAL_DIR}"
    echo "Changing owner for $MS_EXTERNAL_DIR/logs to $USER:$GROUP"
    chown ${USER}:${GROUP} "${MS_EXTERNAL_DIR/logs}"
    chmod -R g+rw "${MS_EXTERNAL_DIR/logs}"
  fi

  if [ -d $MS_INTERNAL_DIR ]; then
    echo "Checking if internal directory $MS_INTERNAL_DIR is empty"
    if [ "$(find ${MS_INTERNAL_DIR}/ -maxdepth 1 -mindepth 1 -type d | wc -l)" -gt 0 ]; then
      TIMESTAMP=$(date +"%Y-%m-%d_%H%M%S")
      DEST_DIR="$MS_EXTERNAL_DIR/$TIMESTAMP"
      echo "Creating directory $DEST_DIR"
      mkdir -p "$DEST_DIR"
      echo "Moving internal directory $MS_INTERNAL_DIR to external directory $DEST_DIR"
      rsync -av $MS_INTERNAL_DIR/ $DEST_DIR/
      echo "Removing internal directory $MS_INTERNAL_DIR"
      rm -rf "${MS_INTERNAL_DIR:?}"/*
      echo "Internal directory $MS_INTERNAL_DIR cleaned"
    fi
  fi
else
  echo "Using internal storage"

  if [ ! -d $MS_INTERNAL_DIR ]; then
    echo "Creating internal directory $MS_INTERNAL_DIR"
    mkdir -p "${MS_INTERNAL_DIR}/logs"
    echo "Changing owner for $MS_INTERNAL_DIR to $USER:$GROUP"
    chown ${USER}:${GROUP} "${MS_INTERNAL_DIR}"
    echo "Setting group permissions for $MS_INTERNAL_DIR"
    chmod -R g+rw "${MS_INTERNAL_DIR}"
    echo "Setting group permissions for $MS_INTERNAL_DIR/logs"
    chown ${USER}:${GROUP} "$MS_INTERNAL_DIR/logs"
    echo "Setting group permissions for $MS_INTERNAL_DIR/logs"
    chmod -R g+rw "${MS_INTERNAL_DIR}/logs"
  fi
fi

if [ -d ${MS_INTERNAL_DIR} ]; then
  if [ -L $MS_INTERNAL_DIR ]; then
    echo "Changing owner for $MS_INTERNAL_DIR to $USER:$GROUP"
    chown -c -h $USER:$GROUP $MS_INTERNAL_DIR
    chown -c -R $USER:$GROUP $MS_INTERNAL_DIR/ | tail -n 10
  else
    echo "Changing owner for $MS_INTERNAL_DIR to $USER:$GROUP"
    chown -c -R $USER:$GROUP $MS_INTERNAL_DIR | tail -n 10
  fi
fi

if [ -d $MS_EXTERNAL_DIR ]; then
  echo "Changing owner for $MS_EXTERNAL_DIR to $USER:$GROUP"
  chown -c $USER:$GROUP $MS_EXTERNAL_DIR
  chown -c -R $USER:$GROUP $(find $MS_EXTERNAL_DIR -mindepth 1 -maxdepth 1 -type d -printf "%p ") | tail -n 10
  echo "Directory $MS_EXTERNAL_DIR ready"
fi

if [ -e $SSD1_DIR ]; then
  echo "SSD1 storage available"
  if [ ! -d $SSD1_DIR/unifi-protect/video ]; then
    echo "Creating SSD1 directory $SSD1_DIR/unifi-protect/video"
    mkdir -p "$SSD1_DIR/unifi-protect/video" || true
    echo "Changing owner for $SSD1_DIR/unifi-protect to $USER:$GROUP"
    chown -R ${USER}:${GROUP} "$SSD1_DIR/unifi-protect" || true  
    echo "Setting group permissions for $SSD1_DIR/unifi-protect"
    chmod -R ug+rw "$SSD1_DIR/unifi-protect" || true
  fi
fi

echo "<-# MSR $version pre-start"
