#!/bin/bash
# vdr-run-check-change-video-dir.sh
# for RASPI NOOP Debian 2.0
# 
# workaround
# to correct faulty fstab, not changing user/group mounting NTFS
# needed for allocating video-dir correctly when running vdr
# 
# 1.  stops vdr + vdradmind
# 2a. checks if any HD known by fstab is mounted to /media/pi,
#     umounts it, mounts it to /media/<HD-Name>
# 2b. if none known HD is mounted in /media/pi it checks
#     whether one is mounted in /media. 
# 2c. if not attempts to mount one of the known HDs under /media 
# 3a. if no success script ends without starting vdr + vdradmind again
# 4.  if success checks correct link /var/lib/video to video-dir on HD
# 5.  takes the first known HD to make a sym-link for video-dir and finishs
# 
# 
# 
# log in /var/log/rc-local.log
#
# 171108
#

# initialize logfile
# if test -f /var/log/rc-local.log; then rm /var/log/rc-local.log; fi
date >> /var/log/rc-local.log

PATH01="/media"
PATH02="/pi"
NOPATH=""
VIDEO_PATH=/var/lib/video
VIDEO_DIR=/video.00
HD01=Tevion-DATA
HD02=ICY-NTFS
HD03=Verb-DATA
LOG=/var/log/rc-local.log

echo "Checking directory for VDR video record"
echo "service vdradmin-am + vdr stop"
service vdr stop
service vdradmin-am start

HD_exists=1

# is HD mounted under ${P1}${P2} ?
# then umount it, mount it under ${P1}
function is_HD () {
   P1=$1
   P2=$2
#   echo "path in loop 1: ${PATH01}${PATH02}/$HD"
   grep -q "${P1}${P2}/$HD" /proc/mounts
   if test $? -eq 0; then
      umount ${P1}${P2}/$HD  #> /dev/null
      echo "${P1}${P2}/$HD unmounted (1a)" 
      grep -q "${P1}${P2}/$HD" /proc/mounts
      if test $? -eq 0; then
            umount ${P1}${P2}/$HD  #> /dev/null
            echo "${P1}${P2}/$HD unmounted 2nd time (1b)"
      fi
      #   if it perhaps exists with wrong owner ...
      grep -q "${P1}/$HD" /proc/mounts
      if test $? -eq 0; then
         umount ${P1}/$HD  #> /dev/null
         echo "${P1}/$HD unmounted (1c)" 
         grep -q "${P1}/$HD" /proc/mounts
         if test $? -eq 0; then
               umount ${P1}/$HD  #> /dev/null
               echo "${P1}/$HD unmounted (1d)"
         fi
      fi
      if mount ${P1}/$HD ; then
         video_HD=$HD       # !
         HD_exists=0
         echo "${P1}/$HD mounted (1c)"
         break              # !
      else 
         HD_exists=1
         echo "${P1}/$HD could not be mounted"
      fi
   fi
}

# first loop looking for known HD mounted under /media/pi
for HD in $HD01 $HD02 $HD03; do
   is_HD  $PATH01 $PATH02
   echo "$HD   $HD_exists"; echo
done

######################################


# second loop looking for mounted known HD under /media,
# if not yet mounted during first loop
if test $HD_exists -eq 1; then
   P1=$PATH01
   P2=$NOPATH
   for HD in $HD01 $HD02 $HD03; do
      grep -q "${P1}${P2}/$HD" /proc/mounts
      if test $? -eq 0; then
         video_HD=$HD
         HD_exists=0
         echo "$HD   exists (2)"
         break
      fi
   done
fi

# third loop looking for existing known HD not mounted.
# if found mounts it under /media
if test $HD_exists -eq 1; then
   P1=$PATH01
   P2=$NOPATH
   for HD in $HD01 $HD02 $HD03; do
      if mount "${P1}${P2}/$HD"; then
         video_HD=$HD
         HD_exists=0
         echo "$HD   exists (3)"
         break
      fi
   done
fi

# check link
if test $HD_exists -eq 0; then
   echo "HD_exists $HD_exists"
   # if link exists
   if test -h $VIDEO_PATH; then
   echo "video path ok"
      if test ! -d $VIDEO_PATH; then
   echo "video path not dir"
         rm $VIDEO_PATH
         if test ! -d ${PATH01}/${HD}$VIDEO_DIR; then
   echo "video path not exist: ${PATH01}/${HD}$VIDEO_DIR"
            mkdir ${PATH01}/${HD}$VIDEO_DIR
         fi
   echo "link generated: ln -s ${PATH01}/${HD}$VIDEO_DIR $VIDEO_PATH"
         ln -s ${PATH01}/${HD}$VIDEO_DIR $VIDEO_PATH
      fi
   else
      if ! -d $VIDEO_PATH; then
   echo "else part"
         if ! -d ${PATH01}/${HD}$VIDEO_DIR; then
            mkdir ${PATH01}/${HD}$VIDEO_DIR
         fi
   echo "link generated: ln -s ${PATH01}/${HD}$VIDEO_DIR $VIDEO_PATH"
            ln -s ${PATH01}/${HD}$VIDEO_DIR $VIDEO_PATH
      fi
   fi
fi



if test -d $VIDEO_PATH; then
   echo "/var/lib/video exists"
   echo "start service vdr + vdradmin-am, need 20 sec"
   echo
   service vdradmin-am start
   service vdr start
   sleep 20
else
      echo "directory /var/lib/video does not exists, aborting" 
fi


#exit

#######################################################
## declare an array variable
#declare -a arr=("element1" "element2" "element3")

## now loop through the above array
#for i in "${arr[@]}"
#do
#   echo "$i"
   # or do whatever with individual element of the array
#done

# You can access them using echo "${arr[0]}", "${arr[1]}" also
#Also works for multi-line array declaration

#declare -a arr=("element1" 
#                "element2" "element3"
#                "element4"
#                )#

