#!/bin/bash
#===============================================================================
#
#          FILE:  installOpenOfficeMacro.sh
# 
#         USAGE:  ./installOpenOfficeMacro.sh 
# 
#   DESCRIPTION:  Installs this given OpenOffice macro in to the users OpenOffice install 
#                 so that the macro can be used from a script
# 
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR:  John Ryland (jryland@xiaofrog.com)
#       COMPANY:  InvertedLogic
#       VERSION:  1.0
#       CREATED:  06/29/08 17:15:25 CST
#      REVISION:  ---
#===============================================================================
if [ "$1" == "" ]
then
    echo "Usage: $0 file.bas"
    echo "(file should be in the current directory and no ./ prepended to it)"
    exit -1
fi

if ! [ -f "$1" ]
then
    echo "$0 error: File not found or not regular file"
    exit -1
fi

BASE=${1%.*}

#
# Some of this script comes from script found from here:
# http://kambing.ui.edu/gentoo-portage/app-office/indeview/indeview-0.6.6.ebuild
#
# Converts the plain text .bas file to an OpenOffice xml script file
cp "$1" "$1.tmp"
# Convert special characters to XML's &XXX; style

if [ "`uname`" == "Linux" ]
then
sed -i -e 's/&/\&amp;/g' "$1.tmp"
sed -i -e 's/"/\&quot;/g' "$1.tmp"
sed -i -e 's/</\&lt;/g' "$1.tmp"
sed -i -e 's/>/\&gt;/g' "$1.tmp"
sed -i -e "s/'/\&apos;/g" "$1.tmp"
sed -i -e "s///g" "$1.tmp"
else
sed -i "" -e 's/&/\&amp;/g' "$1.tmp"
sed -i "" -e 's/"/\&quot;/g' "$1.tmp"
sed -i "" -e 's/</\&lt;/g' "$1.tmp"
sed -i "" -e 's/>/\&gt;/g' "$1.tmp"
sed -i "" -e "s/'/\&apos;/g" "$1.tmp"
sed -i "" -e "s///g" "$1.tmp"
fi

cat >> "$BASE.xba" << _EOF_
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="$BASE" script:language="StarBasic">
_EOF_
cat "$1.tmp" >> "$BASE.xba"
cat >> "$BASE.xba" << _EOF_
</script:module>
_EOF_
rm "$1.tmp"

#
# Find the directory we should install the macro to
#
if [ -e ~/.openoffice.org/3/user/basic/Standard/ ]  # On Linux it should be somewhere like this
then
    BASIC_DIR=~/.openoffice.org/3/user/basic/Standard/
elif [ -e ~/.openoffice.org2.0/user/basic/Standard/ ]  # Or here
then
    BASIC_DIR=~/.openoffice.org2.0/user/basic/Standard/
elif [ -e ~/.openoffice.org2/user/basic/Standard/ ]  # Or here
then
    BASIC_DIR=~/.openoffice.org2/user/basic/Standard/
elif [ -e ~/Library/Preferences/NeoOffice-2.2/user/basic/Standard/ ] # On Mac it is here if using NeoOffice
then
    BASIC_DIR=~/Library/Preferences/NeoOffice-2.2/user/basic/Standard/
elif [ -e %APPDATA%/OpenOffice.org2/user/basic/Standard/ ] # On Windows
then
    BASIC_DIR=%APPDATA%/OpenOffice.org2/user/basic/Standard/
else
    echo "$0 error: Sorry don't know where to install the macro to"
    echo " - Requires OpenOffice or NeoOffice to be installed to batch convert docs to PDF"
    echo " - This error is from installMacro.sh, fix errors or add platforms here" 
    exit -1
fi

#
# now actually install the converted file
#
mv "$BASE.xba" "$BASIC_DIR"
#
# Append the macro file to the library of macros
#
grep "$BASE" "$BASIC_DIR/script.xlb" > /dev/null 2>&1 || sed -i "" -e 's;</library:library>; <library:element library:name="'$BASE'"/>\
</library:library>;' "$BASIC_DIR/script.xlb"


