#!/bin/bash

SRCNAMES="appdata Backup Cleanup Cloud isos Inbox logs Media Movies scripts Series Software Public"
MNTUSER="/mnt/user"
LOGDIR="$MNTUSER/Logs"
LOG="$LOGDIR/mergerfs.log"

# 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

# check if we are the only local instance
if [[ "`pidof -x $(basename $0) -o %PPID`" ]]; then
  echo "$0 script is already running with PID `pidof -x $(basename $0) -o %PPID`" >> "${LOG}"
  exit
fi

for SRCNAME in $SRCNAMES
do
  MOUNTPOINT="$MNTUSER/Union/$SRCNAME"
  if [ "x$1" = "x" ]
  then
    fusermount -u "$MOUNTPOINT"
  else
    fusermount -uz "$MOUNTPOINT"
  fi
done