#!/usr/bin/bash
#

lvisf_basepath="/mnt/archive/lvisf"

# figure out the drive id
lvisf_drive=$(ls -l /dev/disk/by-label/ | grep EXTERNAL)
lvisf_disk_device=$(ls -ls /dev/disk/by-id/ | grep ${lvisf_drive: -4} | grep Samsung)
lvisf_temp="${lvisf_disk_device%-part*}"
lvisf_diskid=$(echo -e ${lvisf_temp} | awk '{print $NF}')
lvisf_folder="${lvisf_basepath}/id/${lvisf_diskid}"
lvisf_relative_link="id/${lvisf_diskid}"
echo
echo "Found LVISF_EXTERNAL labelled drive drive here:"
echo "${lvisf_disk_device}"
echo
echo "Output folder will be:  ${lvisf_folder}"

# test if there is a symbolic link already to this drive
link_exists=$(find ${lvisf_basepath} -maxdepth 1 -type l -ls | grep ${lvisf_diskid} | wc -l)
if [[ "$link_exists" -eq 1 ]]; then
   echo "Link already exists for this drive:"
   find ${lvisf_basepath} -maxdepth 1 -type l -ls | grep ${lvisf_diskid}
else
   madelink=0
   while [[ "$madelink" -eq 0 ]]
   do
      echo "No link exists to this drive.  Enter the exterior label or name to give this drive below"
      read -p "Enter link name: " linkname
      echo "Will create symbolic link:  ln -s ${lvisf_relative_link} $linkname"
      read -r -p "Are you sure? [y/N] " response
      if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]
      then
         cd ${lvisf_basepath}
         ln -s ${lvisf_relative_link} ${linkname}
	 madelink=1
      fi
   done
fi

# mount and copy the data drive
sudo mount -o ro /mnt/lvisf_external
echo "Command to run:"
echo "time rsync -vaP /mnt/lvisf_external/ ${lvisf_folder}"
read -r -p "Are you sure? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]
   time rsync -vaP /mnt/lvisf_external/ ${lvisf_folder}
fi
