#!/bin/bash

SRCNAMES="appdata Backup Inbox logs Media scripts Software Public"
MNTUSER="/mnt/user"
LOGDIR="$MNTUSER/Logs"
LOG="$LOGDIR/mergerfs.log"
DEBUG="False"
checkOK="true"
  
# some helpers and error handling:
info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Mount interrupted >&2; exit 2' INT TERM

##
## Output to a logfile
##
#exec > >(tee -i $LOG)
#exec 2>&1

for SRCNAME in $SRCNAMES
do
  MOUNTPOINT="/Union/$SRCNAME"
  REMOTEDIR="$MNTUSER/$SRCNAME"
  LOCALDIR="$MNTUSER/$SRCNAME"
  fusermount -uz "$MOUNTPOINT"
  mergerfs -o rw,async_read=false,use_ino,allow_other,func.getattr=newest,category.action=all,category.create=ff,cache.files=partial,dropcacheonclose=true "$LOCALDIR":"$REMOTEDIR" "$MOUNTPOINT"
  if [ $DEBUG == "True" ]; then
    echo "Test Local File = $TESTLOCALFILE"
    echo "Test Remote File = $TESTREMOTEFILE"    
    echo "Mountpoint = $MOUNTPOINT"
    echo "Sourcename = $SRCNAME"
    echo "LOCALDIR = $LOCALDIR"
    echo "REMOTEDIR = $REMOTEDIR"
    echo "$( date ) - Merging folders $LOCALDIR:$REMOTEDIR - mergerfs -o rw,async_read=false,use_ino,allow_other,func.getattr=newest,category.action=all,category.create=ff,cache.files=partial,dropcacheonclose=true $LOCALDIR:$REMOTEDIR $MOUNTPOINT"
  fi
done

#mergerfs -o rw,async_read=false,use_ino,allow_other,func.getattr=newest,category.action=all,category.create=ff,cache.files=partial,dropcacheonclose=true "/mnt/remotes/hass_backup":"/mnt/remotes/hass_backup" "/mnt/user/Union/hass_backup"
#mergerfs -o rw,async_read=false,use_ino,allow_other,func.getattr=newest,category.action=all,category.create=ff,cache.files=partial,dropcacheonclose=true "/mnt/remotes/hass_config":"/mnt/remotes/hass_config" "/mnt/user/Union/hass_config"

#if [ $checkOK == "true" ]
#then 
#  echo "$( date ) - Evertything OK!"  >> /mnt/user/logs/mergerfs.log 
#else
#  echo "$( date ) - Remerging has been processed"  >> /mnt/user/logs/mergerfs.log 
#fi

