:
# convert a directory listing to an input file for text2html
#
# 04Feb06 wb add -c and -d
# 11Oct06 wb add -t, change default title to use basename
# 12Oct06 wb add -f and -s
# 14May07 wb add -p
# 05Jan08 wb add -y
# 11Jun08 wb add -b for blank lines

show_name=no
show_high=no
show_date=no
show_size=no
file_opt=
title=
is_patch=
do_copy=
add_blank_lines=

case "`pwd`" in
*/pat) is_patch=yes ;;
esac

while [ ! -z "$1" ]
do
	case "$1" in
	-b) add_blank_lines=yes ;;				# add blank lines
	-c) show_name=yes ; show_high=yes ; show_date=yes ;;	# options for a CD image
	-n) show_name=yes ;;					# show the file name
	-h) show_high=yes ;;					# add a link to a high res directory
	-d) show_date=yes ;;					# show the date
	-y) show_date=date ;;					# show the date but not the time
	-s) show_size=yes ;;					# show the size
	-f) file_opt=":t" ;;					# treat files as text instead of images
	-t) title="$2" ; shift ;;				# get title, use the directory by default
	-p) is_patch=yes ;;					# set for patches
	-*) echo "$0: Warning: Unknown option $1" ;;
	*) echo "$0: Warning: Unknown parameter $1" ;;
	esac
	shift
done

if [ "$is_patch" = yes ]
then
	file_opt=":t" ; show_size=yes ; show_date=yes ; do_copy=no ; title="Patches"
fi

high_dir=high
if [ ! -d "high" ]
then
	if [ -d "../high" ]
	then
		high_dir="../high"
	fi
fi

if [ "$do_copy" = no ]
then
	echo -n '[/copy]'
fi

if [ ! -z "$title" ]
then
	echo "$title"
else
	basename `pwd`
fi

echo

#if [ "$show_name" = yes ]
#then
#	sed -e 's/\(.*\)/[\1] \1/'
#else
#	sed -e 's/^/[/' -e 's/$/]/'
#fi

while read line
do
	if [ ! -z "$line" ]
	then
		if [ "$show_high" = yes ]
		then
			echo -n "[$high_dir/$line,${line}${file_opt}]"
		else
			echo -n "[${line}${file_opt}]"
		fi
		if [ "$show_name" = yes ]
		then
			echo -n " $line"
		fi
		if [ "$show_size" = yes ]
		then
			file="$line"
			echo -n " `ls -l --human-readable "$file" | awk '{ print $5 }'`"
		fi
		if [ "$show_date" = yes ]
		then
			file="$line"
			if [ -f "$high_dir/$line" ]
			then
				file="$high_dir/$line"
			fi
			echo -n " `ls -ld --time-style=long-iso "$file" | awk '{ print $6 " " $7 }'`"
		elif [ "$show_date" = date ]
		then
			file="$line"
			if [ -f "$high_dir/$line" ]
			then
				file="$high_dir/$line"
			fi
			echo -n " `ls -ld --time-style="+%F" "$file" | awk '{ print $6 }'`"
		fi
		echo
		if [ "$add_blank_lines" = yes ]
		then
			echo
		fi
	fi
done
