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

dirinfo_get_creation_date () {

	if [[ ! "$1" ]]; then
		DIR="."
	else	DIR="$1"
	fi

	if [[ -d "$DIR" ]]; then
		stat "$DIR" | grep "Modify" | gawk '{print $2}'
	fi

}

dirinfo_set_creation_date () {

	if [[ ! "$1" ]]; then
		DIR="."
	else	DIR="$1"
	fi

	if [[ -e "$DIR"/.dirinfo ]]; then
		sed -e '/Created/d' -i "$DIR"/.dirinfo
		echo "Created=$(dirinfo_get_creation_date $DIR)" >> "$DIR"/.dirinfo
	fi

}

dirinfo_get_dirinfo () {

	if [[ ! "$1" ]]; then
		DIR="."
	else	DIR="$1"
	fi

	if [[ -e "$DIR"/.dirinfo ]]; then
		. "$DIR"/.dirinfo
	fi

	echo -e "\n${eyellow} -- $PWD --${ewhite}"

	if [[ $Notes == "" ]]; then
		Notes="No notes."
	fi

	if [[ ! ${Type} ]]; then
		dirinfo_display_info
	else	dirinfo_display_ascii
	fi

	unset Owner Created Allow Delete Tags Notes Type ASCII
	echo ""

}

dirinfo_display_info () {

	if [[ ${Owner} ]]; then
		echo -e "\n${eiceblue}\tOwner:   ${Owner}"
	fi

	if [[ ${Created} ]]; then
		echo -e "${emagenta}\tCreated: ${Created}"
	fi

	if [[ ${Tags} ]]; then
		echo -e "${ered}\tTags:    ${Tags[@]}"
	fi

	if [[ ${Notes} ]]; then
		echo -e "\n${egreen} ${Notes}"
	fi

}

dirinfo_display_ascii () {

	if [[ -e $BSNG_RC_DIR/ascii/folder-${Type} ]]; then
		ASCII="$BSNG_RC_DIR/ascii/folder-${Type}"
	elif [[ -e $DIR/.dirascii ]]; then
		ASCII="$DIR/.dirascii"
	fi

	if [[ $ASCII ]]; then
		echo -e "$(cat $ASCII)" | sed -e "s/@OWNER@/${Owner}/g" \
			-e "s/@CREATED@/${Created}/g" \
			-e "s/@TAGS@/${Tags[*]}/g" \
			-e "s/@NOTES@/${Notes}/g"
	else 	unset Type
		dirinfo_display_info
	fi

}

dirinfo_index_db () {

	find $HOME -name .dirinfo | grep -w .dirinfo > $HOME/.dirinfo_index

}

dirinfo_index_tags () {

	if [[ -e $HOME/.dirinfo_index ]]; then
		rm -f $HOME/.dirinfo_tags
		for dirinfo in $(cat $HOME/.dirinfo_index); do
			echo "${dirinfo%.dirinfo}" :: $(grep Tags "$dirinfo") >> $HOME/.dirinfo_tags
		done
		sed -e 's/Tags=(//g' -e 's/)//g' -i $HOME/.dirinfo_tags
	fi

}

dirinfo_index_types () {

	if [[ -e $HOME/.dirinfo_index ]]; then
		rm -f $HOME/.dirinfo_types
		for dirinfo in $(cat $HOME/.dirinfo_index); do
			echo "${dirinfo%.dirinfo}" :: $(grep Type "$dirinfo") >> $HOME/.dirinfo_types
		done
		sed -e 's/Type=//g' -i $HOME/.dirinfo_types
	fi

}

dirinfo_index_dates () {

	if [[ -e $HOME/.dirinfo_index ]]; then
		rm -f $HOME/.dirinfo_dates
		for dirinfo in $(cat $HOME/.dirinfo_index); do
			echo "${dirinfo%.dirinfo}" :: $(grep Created "$dirinfo") >> $HOME/.dirinfo_dates
		done
		sed -e 's/Created=//g' -i $HOME/.dirinfo_dates
	fi

}

dirinfo_index () {

	case $1 in
		*update)
			echo "[01/04] .dirinfo index"
			dirinfo_index_db
			echo "[02/04] creation-date index"
			dirinfo_index_dates
			echo "[03/04] tag index"
			dirinfo_index_tags
			echo "[04/04] type index"
			dirinfo_index_types
		;;

		*rebuild)
			echo "[01/03] creation-date index"
			dirinfo_index_dates
			echo "[02/03] tag index"
			dirinfo_index_tags
			echo "[03/03] type index"
			dirinfo_index_types
		;;

		*search)
			case $2 in
				*tag)
					grep " $3" $HOME/.dirinfo_tags | gawk '{print $1}'
				;;

				*type)
					grep " $3" $HOME/.dirinfo_types | gawk '{print $1}'
				;;

				*date)
					grep " $3" $HOME/.dirinfo_dates | gawk '{print $1}'
				;;
			esac
		;;

		*list)
			cat $HOME/.dirinfo_index | sed -e 's/.dirinfo//g'
		;;
	esac

}

dirinfo_create () {

	if [[ ! -e $PWD/.dirinfo ]]; then
		echo "Whoowns the folder?"
		read OWNER

		echo -e "\nWhen has the folder been created? (YYYY-MM-DD format)"
		read CREATION

		echo -e "\nWhat type of content?"
		echo "(home download mediaplayer audio video game home picture share document book)"
		read TYPE

		echo -e "\nEnter some tags for the folder. (separate using  space)"
		read TAGS

		echo -e "\nDo you want to add some notes?"
		read NOTES

		echo Owner=\"$OWNER\" >> $PWD/.dirinfo
		echo Created=$CREATION >> $PWD/.dirinfo
		echo Type=$TYPE >> $PWD/.dirinfo
		echo Tags=\($TAGS\) >> $PWD/.dirinfo
		echo Notes=\"$NOTES\" >> $PWD/.dirinfo

	fi

}

lscd () {

	builtin cd "${@}" &>/dev/null
	dirinfo_get_dirinfo

	echo -e "${epink}content:"
	ls $LSCD_OPTS

	echo "$PWD" > $HOME/.lastpwd

}
