#! /bin/sh

usage () {
    echo Usage: >&2
    echo "\t" $0 >&2
    echo "\t\t" "Strip .odt files in current directory, writing" >&2
    echo "\t\t" "converted files to ./converted directory, which" >&2
    echo "\t\t" "wll be created as necessary. Existing files of the" >&2
    echo "\t\t" "name will be overwritten." >&2
    echo "\t" $0 "[-o <output-dir>] [odtfile ...]" >&2
    echo "\t\t" "Strip the named odtfiles, or all .odt files in" >&2
    echo "\t\t" "the current directory if no files are specified." >&2
    echo "\t\t" "If the -o argument is specified, use the argument" >&2
    echo "\t\t" "as the output directory, which will be created as" >&2
    echo "\t\t" "necessary. If this is the same as a source" >&2
    echo "\t\t" "directory, original files wil be overwritten." >&2 
    echo "\t\t" "If no output directory is specified, a directory" >&2
    echo "\t\t" "./converted will be created in the current working" >&2
    echo "\t\t" "working directory." >&2
    echo "\t" "N.B. If two files with same basename but different" >&2
    echo "\t\t" "path are specified, the later conversion" >&2
    echo "\t\t" "will overwrite the earlier." >&2
    echo "\n\t" "On OS X Apache OpenOffice and LibreOffice will be looked for" >&2
    echo "\t" "in particular locations, preference going to LibreOffice." >&2
    echo "\n\t" "On linux, the default location of soffice is determined by" >&2
    echo "\t" "the command 'which soffice'." >&2
    echo "\n\t" "In both cases, the environment variable SOFFICE will override" >&2
    echo "\t" "other locations." >&2
}

if [ $# -ge 1 ]; then
    [ "x$1" = "x-h" -o "x$1" = "x-?" -o "x$1" = "x?" ] && {
	usage; exit 1
    };
fi

find_soffice () {
    # If the soffice binary is not on the PATH,
    # check for soffice in system-dependent location,
    # and check for an envvar SOFFICE
    # N.B. SOFFICE overrides other searches
    # If nothing is found, give up
    local sys loloc ooloc
    sys=`uname`
    unset sofficeloc
    loloc=/Applications/LibreOffice.app/Contents/MacOS/soffice
    ooloc=/Applications/OpenOffice.app/Contents/MacOS/soffice
    if [ $sys = Darwin ]; then
	# Check for LO and OO
	[ -x $ooloc ] && sofficeloc=$ooloc
	[ -x $loloc ] && sofficeloc=$loloc
    elif [ $sys = Linux ]; then
	sofficeloc=`which soffice`
    fi
    sofficeloc="${SOFFICE:-$sofficeloc}"
    [ "$sofficeloc" = "" ] && {
	echo Cannot locate program soffice >&2
	exit 2
    };
}

find_perl () {
    if ! which perl >/dev/null
    then echo  Cannot locate perl >&2
	exit 3
    fi
}

stripfodt () {
    # Replace text in the filename argument in place
    perl -pi -e 's/<text:alphabetical-index-mark text:string-value="[[:alpha:]]*"\/>//g' "$1" 
}

stripodts () {
    # Strip all .odt files in the current directory
    # ${outdir} specifies the outpur directory, which has already been created
    ls *.odt| while read f
    do
	stripodt "$f"
    done
}

stripodt () {
    # parameter is the file to strip
    # ${outdir} specifies the output directory, which has already been created
    f="$1"
    echo Stripping $f into $outdir/$f
    local dname bname
    dname=`dirname "$f"`
    bname=`basename "$f" .odt`
    $sofficeloc --headless --convert-to fodt --outdir "$outdir" "$f"
    stripfodt "$outdir/$bname.fodt"
    $sofficeloc --headless --convert-to odt --outdir "$outdir" "$outdir/$bname.fodt"
}

find_soffice
#echo $sofficeloc

find_perl

outdir=./converted

if [ $# -gt 0 ]; then
    if [ "x$1" = "x-o" ]; then
	shift
	[ $# -eq 0 ] && {
	    echo "\nNo argument to -o" >&2
	    exit 2
	};
	outdir="$1"
	shift
    fi
fi

# If $outdir doesn't exist, create it
if [ ! -d "$outdir" ]; then
    mkdir "$outdir"
fi

if [ ! \( -d "$outdir" -a -w "$outdir" \) ];then
    echo Cannot write to $outdir >&2
    exit 2
fi

if [ $# -eq 0 ]; then
    stripodts
else
    for f in "$@"; do
	stripodt "$f"
    done
fi

