#!/bin/bash
#########################################################
# 							#
# This is BashStyle-NG  				#
#							#
# Licensed under GNU GENERAL PUBLIC LICENSE v3    	#
#							#
# Copyright 2007 - 2011 Christopher Bratusek		#
#							#
#########################################################

# .dirindex syntax
# column1   :: column2 :: column3 :: column4 :: column5
# directory :: tags    :: type    :: date    :: owner

dirinfo_create_index () {

	if [[ ! -e $HOME/.dirindex || $1 == "--force" ]]; then
		find $HOME -name .dirinfo | sed -e 's/.dirinfo//g' > $HOME/.dirindex.0
	else	cp $HOME/.dirindex $HOME/.dirindex.0
	fi

	for directory in $(cat $HOME/.dirindex.0); do
		echo :: $(grep Tags "$directory/.dirinfo") >> $HOME/.dirindex.1
	done
	sed -e 's/Tags=(//g' -e 's/)//g' -i $HOME/.dirindex.1

	for directory in $(cat $HOME/.dirindex.0); do
		echo :: $(grep Type "$directory/.dirinfo") >> $HOME/.dirindex.2
	done
	sed -e 's/Type=//g' -i $HOME/.dirindex.2

	for directory in $(cat $HOME/.dirindex.0); do
		echo :: $(grep Created "$directory/.dirinfo") >> $HOME/.dirindex.3
	done
	sed -e 's/Created=//g' -i $HOME/.dirindex.3

	for directory in $(cat $HOME/.dirindex.0); do
		echo :: $(grep Owner "$directory/.dirinfo") >> $HOME/.dirindex.4
	done
	sed -e 's/Owner=//g' -e 's/\"//g' -i $HOME/.dirindex.4

	paste -d' ' $HOME/.dirindex.[0-4] > $HOME/.dirindex
	sed -e 's/\"//g' -i $HOME/.dirindex
	rm $HOME/.dirindex.[0-4]
	rm $HOME/.wait

}

dirinfo_index ()
{

	case $1 in
		*generate)
			touch $HOME/.wait
			dirinfo_create_index --force &

			SP_COLOUR="\e[37;44m"
			SP_WIDTH=5.5
			SP_DELAY=0.2
			SP_STRING=${2:-'+     '}

			while [ -e $HOME/.wait ]
			do
				printf "$SP_COLOUR\e7  %${SP_WIDTH}s  \e8\e[01;37m" "$SP_STRING"
				sleep ${SP_DELAY:-.2}
				SP_STRING=${SP_STRING#"${SP_STRING%?}"}${SP_STRING%?}
			done

			tput sgr0

		;;

		*rebuild)
			dirinfo_create_index
		;;

		*get-date)
			dirinfo_date --get
		;;

		*set-date)
			dirinfo_date --set
		;;

		*search)

			if [[ ! -e $HOME/.dirindex ]]; then
				echo "Please run dirinfo --generate first"
				exit 1
			fi

			shift

			case $1 in

				+tag)
					shift
					gawk -F"::" '$2 ~ /'"${@}"'/{print $1}' $HOME/.dirindex
				;;

				+type)
					shift
					gawk -F"::" '$3 ~ /'"${@}"'/{print $1}' $HOME/.dirindex
				;;

				+date)
					shift
					gawk -F"::" '$4 ~ /'"${@}"'/{print $1}' $HOME/.dirindex
				;;

				+owner)
					shift
					gawk -F"::" '$5 ~ /'"${@}"'/{print $1}' $HOME/.dirindex
				;;

				*)
					gawk -F"::" '/'"${@}"'/{print $1}' $HOME/.dirindex
				;;

			esac
		;;

		*list)

			if [[ ! -e $HOME/.dirindex ]]; then
				echo "Please run dirinfo --generate first"
				exit 1
			fi

			cat $HOME/.dirindex | sed -e 's/.dirinfo//g'
		;;

		*help | "")

			echo -e "\n${ewhite}Usage:\n"
			echo -e "${eorange}dirinfo${ewhite} | ${egreen}--generate ${eiceblue}[generate a new index]\
			\n${eorange}dirinfo${ewhite} | ${egreen}--rebuild ${eiceblue}[rebuild the index, but don't scan for new .dirinfo files]\
			\n${eorange}dirinfo${ewhite} | ${egreen}--search ${eiceblue}[search the index for +tag, +type, +date, +owner]\
			\n${eorange}dirinfo${ewhite} | ${egreen}--list ${eiceblue}[list all .dirinfo files indexed]\
			\n${eorange}dirinfo${ewhite} | ${egreen}--create ${eiceblue} [create .dirinfo for current directory]\
			\n${eorange}dirinfo${ewhite} | ${egreen}--get-date ${eiceblue} [get the date of the last modification]\
			\n${eorange}dirinfo${ewhite} | ${egreen}--set-date ${eiceblue} [set the date of the last modification]" | column -t
			echo ""
			tput sgr0

		;;
	esac

}