#!/bin/bash

## Up is a bash function to simplify vertical file system navigation.
## 
## Copyright (C) 2013 Serge Smeesters
## 
## This program is free software: you can redistribute it
## and/or modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation, either version 3
## of the License, or (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
## See the GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program. If not, see http://www.gnu.org/licenses/.

mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
[ -d $mydir ] || exit 4
configfile="$mydir/spm_namelength_config.sh"

if [ -f "$configfile" ]
then
	. "$configfile"
else
	echo "No config file find :("
	exit 4
fi

trunc_directory ()
{
	local shortsize
	(( shortsize = sizemax - HASH_SIZE ))

	local sizemaxp1
	(( sizemaxp1 = sizemax +1 ))
	local PATTERN="" ; for i in `seq 1 $sizemaxp1`; do PATTERN="${PATTERN}?"; done; PATTERN="${PATTERN}*"

	local filefullpathname
	find "$root" -depth -name "$PATTERN" | \
	while read filefullpathname ; do

		echo "--------------------------------"
		
		local filefullname=$(basename "$filefullpathname")

		local filepath=$(dirname "$filefullpathname")
		local filename=${filefullname%.*}
		local filextension=${filefullname##*.}
		local hashvalue="$(echo "$filename" | md5sum | cut -c1-$HASH_SIZE)"

		echo "↓ $filefullname"

		local filextensionsize=${#filextension}

		if [ $filextensionsize -gt $EXTENTION_MAX_SIZE ] ; then
			local fileshortfullname="$(echo "$filefullname" | cut -c1-$shortsize)$hashvalue"
		else
			local newsize
			(( newsize = shortsize - ( filextensionsize + 1 ) ))
			local fileshortname="$(echo "$filename" | cut -c1-$newsize)$hashvalue"
			local fileshortfullname="$fileshortname.$filextension"
		fi

		echo "→ $fileshortfullname"

		local fileshortfullpathname="$filepath/$fileshortfullname"

		if [ -e "$fileshortfullpathname" ] ; then
			echo "Waw... the shorten file seam to yet exists ! :("
			exit 4
		fi

		pushd "$filepath" >/dev/null
			echo "mv \"$fileshortfullname\" \"$filefullname\"" >> .namelengthrestore
			mv "$filefullname" "$fileshortfullname"
		popd >/dev/null

	done
}

restore_directory ()
{
	local filefullpathname
	find "$root" -depth -type f -name ".namelengthrestore" | \
	while read filefullpathname ; do

		local filepath=$(dirname "$filefullpathname")
		pushd "$filepath" >/dev/null
			cat .namelengthrestore
			. .namelengthrestore
			rm .namelengthrestore
		popd >/dev/null

	done
}

main ()
{
	if [ $# -lt 2 ] ; then
		echo "Less than 2 arguments :("
		exit 1
	fi

	# echo "\$2 : \"$2\""
	local root="$(readlink -f "$2")"
	# echo "root : \"$root\""
	if [ ! -d "$root" ] ; then
		echo "\"$root\" don't appear to be a directory :("
		exit 1
	fi

	case "$1" in
		trunc)

			if [ $# -lt 3 ] ; then
				echo "Trunc need 2 arguments..."
				exit 2
			fi

			local sizemax="$3"

			if [ ! "$sizemax" ] ; then exit 1 ; fi

			local minsizemax
			(( minsizemax = 5 * HASH_SIZE + EXTENTION_MAX_SIZE ))
			# echo "A reasonable sizemax should be $minsizemax"
			if [ "$sizemax" -lt "$minsizemax" ] ; then
				echo "sizemax, $sizemax less then minsizemax, $minsizemax :("
				exit 1
			fi

			trunc_directory
		;;

	   restore)
			restore_directory
	   ;;

	   *)
		  echo "Usage: $0 {trunc <DIR> <MAXLENGTH> |restore <DIR>} " >&2
		  exit 1
		  ;;
	esac
}

main "$@"
