#!/bin/bash -eu

# convert-cwk
#
# Converts a directory tree of AppleWorks/ClarisWorks documents into a slightly
# more readable format.
#
# version 20050923

usage="Usage: $0 IN OUT"

IN="${1:?$usage}"
OUT="${2:?$usage}"

too_long_base="/tmp/toolong"

# OUT should be absolute since we reference it after we chdir to IN
if test "$OUT" = "${OUT#/}"; then
	OUT="$PWD/$OUT"
fi

cd "$IN" 

while read in; do
	#out_dir="$(dirname "$OUT/$in")"
	# this saves us a fork, and works as long as $in does not end in /
	out_dir="$OUT/$in"; out_dir="${out_dir%/*}"
	mkdir -p "$out_dir"

	#in_base="$(basename "$in")"
	# this saves us a fork, and works as long as $in does not end in /
	in_base="${in##*/}"
	out_base="${out_dir}/${in_base%.cwk}"

	for f in "$out_base".{rtf,xls,png,txt}; do
		if test -f "$f"; then
			#echo "skip: already done: $f"
			continue 2
		fi
	done

	# XXX: it seems that appleworks munges the filename of *sourcE* documents
	#      if when their file names are > 32 characters.
	# XXX: assumes input and output filenames have the same length
	if test "${#in_base}" -ge 32; then
		echo "name too long: $in"
		
		in_abs="$(mktemp -u -t too_long_in).cwk"
		cp "$in" "$in_abs"
		
		overflow_out_base="$out_base"
		out_base="$(mktemp -u -t too_long_out)"
	else
		# If we could get AppleWorks to chdir to $IN then would it be able to
		# handle relative paths? This would clean up a LOT of crap in this
		# script
		in_abs="$PWD/$in"
		unset overflow_out_base
	fi
	
	echo "convert: $in_abs"
	osascript > /dev/null <<- EOF
		tell application "AppleWorks 6"
			-- XXX: no idea about the correct syntax to do this
			-- set locked file warning of front application preferences to false
			activate
			try
				open posix file "$in_abs" -- XXX: no error if file can't be opened
				try
					if document kind of front document as string is "text document" then
						save front document in posix file "$out_base.rtf" using translator "RTF"
					else if document kind of front document as string is "spreadsheet document" then
						save front document in posix file "$out_base.xls" using translator "Excel Win 97, 2000, XP 2002 spr"
					else if document kind of front document as string is "painting document" then
						save front document in posix file "$out_base.png" using translator "PNG [QT]"
					else if document kind of front document as string is "drawing document" then
						save front document in posix file "$out_base.png" using translator "PNG [QT]"
					else if document kind of front document as string is "database document" then
						save front document in posix file "$out_base.txt" using translator "ASCII text"
					else
						display dialog "Unknown document kind: " & document kind of front document as string
					end
				end 
				close front document without saving
			end
		end
	EOF
	
	# XXX: no way to get the saved file name from appleworks
	if test -n "${overflow_out_base:-}"; then
		# XXX: why doesn't the completion work in _this_ shell?
		out=$(echo "$out_base".*)
		
		ext="${out##*.}"
		echo '   ' "-> $overflow_out_base.$ext"
		mv "$out" "$overflow_out_base.$ext"
		rm "$in_abs"
	fi
	echo '   ' 'done'
done < <(find . -type f -name '*.cwk')
