#!/bin/bash

# PURPOSE: Script to create symlinks to reduce entry points to make browsing easier on the NG streamers
#
# AUTHOR: feedback[AT]HaveTheKnowHow[DOT]com

# NOTES: EDIT THE BOTTOM SECTION OF THIS SCRIPT AS REQUIRED 
#        DO NOT TOUCH THE TOP SECTION UNLESS THE CODE IS NOT WORKING IN YOUR ENVIRONMENT

CreateTheLinks() {

#DO NOT TOUCH THIS SECTION
#DO NOT TOUCH THIS SECTION
#DO NOT TOUCH THIS SECTION

	_RemoveLinks=$1
	_MaxDepth=$2
	_MinDepth=$3
	_DFolder=$4
	shift 4
	_SFolders=( "$@" )

	if ! [[ $_MaxDepth =~ ^[0-9]+$ ]]
	then
	  echo "Error: Argument 2 (Maxdepth) is not numeric" $_MaxDepth
	  exit
	fi

	if ! [[ $_MinDepth =~ ^[0-9]+$ ]]
	then
	  echo "Error: Argument 3 (Mindepth) is not numeric" $_MinDepth
	  exit
	fi

	if [ ! -d "$_DFolder" ]
	then
	  echo "Error: Symlink Folder does not exist:" $_DFolder
	  exit
	fi

	cd "$_DFolder"

	if [ $_RemoveLinks = 'TRUE' ]
	then 
	  #Remove existing links
  	  echo 'Removing existing links from:' $_DFolder

	  find * -mindepth 0 -type l | while read file
	  do
 	    rm "$file"
	  done
	fi

	#Create the links
	NUMDIRS=${#_SFolders[@]}

	for (( i=0;i<$NUMDIRS;i++ ))
	do
	  ThisFolder=${_SFolders[${i}]}

	  if [ -d "$ThisFolder" ]
	  then
	    echo 'Creating Links in '$_DFolder' for everything found in folder:' $ThisFolder

	    find "$ThisFolder/"* -maxdepth $_MaxDepth -mindepth $_MinDepth -type d |while read file
	    do
#	      echo Linking \""$(basename "$file")"\" to \""$file"\"
	      ln -s "$file" "$(basename "$file")"
	    done
	  else
	    echo 'ERROR: Source Folder does not exist:' "$ThisFolder"
	  fi
	done

	#go back to the folder we were in
	cd - >/dev/null

#END OF - DO NOT TOUCH THIS SECTION
#END OF - DO NOT TOUCH THIS SECTION
#END OF - DO NOT TOUCH THIS SECTION
#END OF - DO NOT TOUCH THIS SECTION
}


#EDIT THESE VALUES AS REQUIRED

# NOTES:
#
# Syntax is: CreateLinks TRUE x y "$DestFolder" "${Sources[@]}"
#
# Where x is the maximum number of folders you want to descent down in the hierarchy from the SourceFolders level. Normally this is one folder level and so specify 1
# and where y is the number of levels you want to ignore from the current one downwards. Normally this is none and so specify 0
# Specify TRUE as the first argument to remove the existing links in the DestFolder else specify FALSE

#DestFolder is the folder which will contain the symlinks
#SourceFolders is where you specify the folders you want to create the symlinks for

#--------------------------------------------------------------------#

#  DO MY DVDs

#--------------------------------------------------------------------#
DestFolder="/media/WD1000FYPS/Unprotected/MediaLinks/DVDs"
SourceFolders=( '/media/HD203WI/RAIDMain/Pre-recorded_DVDs'
		'/media/HD204UI_1/RAIDMain/Pre-recorded_DVDs'
		'/media/WD10EADS/RAIDMain/Pre-recorded_DVDs'
		)

#I want to create links for all folders found in the "SourceFolders" folders
CreateTheLinks 'TRUE' 1 0 "$DestFolder" "${SourceFolders[@]}"

SourceFolders=( '/media/WD20EADS/RAIDMain/Videos' )

#I want to create links for all folders found ONE LEVEL BELOW the "SourceFolders" folders
CreateTheLinks 'FALSE' 1 1 "$DestFolder" "${SourceFolders[@]}"

#--------------------------------------------------------------------#

#  DO MY BLU-RAYS

#--------------------------------------------------------------------#
DestFolder="/media/WD1000FYPS/Unprotected/MediaLinks/Blu-Rays"
SourceFolders=( '/media/HD203WI/RAIDMain/Blu-Rays' 
		'/media/HD204UI_1/RAIDMain/Blu-Rays'
		'/media/WD10EADS/RAIDMain/Blu-Rays/'
		'/media/WD20EADS/RAIDMain/Blu-Rays/'
		)
CreateTheLinks 'TRUE' 1 0 "$DestFolder" "${SourceFolders[@]}"

#--------------------------------------------------------------------#

#  DO MY TV SHOWS

#--------------------------------------------------------------------#
DestFolder="/media/WD1000FYPS/Unprotected/MediaLinks/TV Shows"
SourceFolders=( '/media/HD203WI/RAIDMain/TV Shows' 
		'/media/HD204UI_1/RAIDMain/TV Shows'
		)
CreateTheLinks 'TRUE' 1 0 "$DestFolder" "${SourceFolders[@]}"