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

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

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

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

	paste -d' ' $HOME/.dirindex.[0-5] > $HOME/.dirindex
	sed -e 's/\"//g' -i $HOME/.dirindex
	rm $HOME/.dirindex.[0-5]
	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
				;;

				*mark)
					shift
					gawk -F"::" '$6 ~ /'"${@}"'/{print $1 $6}' $HOME/.dirindex

				;;

				*jump)
					shift
					gawk -F"::" '$6 ~ /'"${@}"'/{print $1 $6}' $HOME/.dirindex | grep -w "${@}"
				;;

				*)
					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 | "")

			bsng-help -a "Christopher Roy Bratusek" -e "nano@tuxfamily.org" -h "http://www.nanolx.org/"\
				-l "GNU GPL v3" -n "dirinfo" -s "directory indexing facilities" -v "v2draft6" -y "2011-2013"\
				-o "--generate:|generate a new index"\
				-o "--rebuild:|rebuild the index, but do not scan for new .dirinfo files"\
				-o "--search:tag type date owner mark|search in the index"\
				-o "--list:|list all .dirinfo files indexed"\
				-o "--create:|create .dirinfo for current directory"\
				-o "--get-date:|get the date of last modification"\
				-o "--set-date:|set the date of last modification"
		;;
	esac

}
