#!/bin/bash

# This file is part of the "pdf2oo" library.
#
# Pdf2oo is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation (version 2 of the License).
#  
# Pdf2oo is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#  
# You should have received a copy of the GNU General Public License
# along with pdf2oo.  See the file LICENSE.  If you haven't received
# a copy of the GNU General Public License, write to:
#  
# Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA
# 02111-1307  USA
#
# Author: Vincenzo Ciancia
#
# applejack@users.sf.net
# vincenzo_ml@yahoo.it

resolution="150"
transparent="false"
color="false"
imgtype="jpg"
type="odp"
portrait="portrait"
interactive="false"

usage(){
    cat<<EOF
usage: $(basename "$0") [-batch|-interactive] [-color|-gray] [-r resolution] [-transparent|-opaque] input_file [output_file]

Resolution is in dpi. Defaults are 150 dpi, grayscale, not transparent, interactive.

EOF
    exit 0
}

check_requirements(){
    local MISSING="COMMAND  | SUSE PACKAGE | DEBIAN PACKAGE
---------+--------------+---------------"
    local failed=false
    local file
    which pdfinfo > /dev/null || { MISSING="$MISSING
pdfinfo  | xpdf-config  | xpdf-utils"; 
	failed=true; }

    which pdftoppm > /dev/null || { MISSING="$MISSING
pdftoppm | xpdf         | xpdf-reader";
	failed=true; }

    which convert > /dev/null || { MISSING="$MISSING
convert  | ImageMagick  | imagemagick"; 
	failed=true; }

    which zip > /dev/null || { MISSING="$MISSING
zip      | zip          | zip"; 
	failed=true; }

    if [ "$failed" = "true" ]
	then file=$(mktemp)
	cat << EOF > $file
You need the following commands to run $(basename "$0"):

$MISSING

Install them using your distribution's package manager, then retry.
EOF
	which kdialog > /dev/null && kdialog --msgbox "$(cat $file)" || cat $file
	rm $file	
	exit 1
    fi
}

check_ui_requirements(){
    local MISSING="COMMAND  | SUSE PACKAGE | DEBIAN PACKAGE
---------+--------------+---------------"
    local failed=false

    which kdialog > /dev/null || { MISSING="$MISSING
kdialog       | kdebase3     | kdebase-bin"; 
	failed=true; }

    which kmdr-executor > /dev/null || { MISSING="$MISSING
kmdr-executor | kdewebdev3   | kommander"; 
	failed=true; }

    which dcop > /dev/null || { MISSING="$MISSING
dcop          | kdelibs3     | kdelibs-bin"; 
	failed=true; }

    if [ "$failed" = "true" ]
	then
	file=$(mktemp)
	cat <<EOF > $file
You need the following commands to run $(basename "$0") in _graphical_ mode:

$MISSING

Install them using your distribution's package manager, then retry. You can still use batch mode (-batch command line option) in a terminal window. 
EOF
	if tty -s; then
	    cat $file
	    rm $file
	    echo 
	    echo Proceeding interactively since we are in an interactive shell
	    echo
	    echo
	    interactive=false
	else
	    which kdialog > /dev/null && kdialog --msgbox "$(cat $file)" || cat $file
	    rm $file
	    exit 1	    
	fi	
    fi
}

exit=false

while [ "${1:0:1}" == "-" ]
  do case "$1" in
      "-transparent")
	  transparent="true"
	  ;;
      "-opaque")
	  transparent="false"
	  ;;
      "-color")	 
	  color="true"
	  ;;
      "-gray")
	  color="false"
	  ;;
      "-r")
	shift
	resolution="$1"
	;;
      "-batch")
        interactive="false"
	;;
      "-interactive")
	interactive="true"
	;;
      *)
	usage
	;;
  esac
  shift
done

if [ "$DISPLAY" = "" ]
    then interactive="false"
fi

check_requirements

if [ "$interactive" = "true" ]
    then  check_ui_requirements
fi

if [ "$1" = "" -a "$interactive" = "false" ]
    then usage
fi

my_exit(){
    if [ -d "$destdir" ]
    then rm -rf "$destdir"
    fi
    if [ "$pbar" != "" ]
	then dcop "$pbar" close
    fi
    if [ "$interactive" = "true" ]
	then kdialog --error "Error while $phase$1"
    else
        echo "Error while $phase$1"
    fi
    exit 1
}

normalize(){
    local PATHNAME="$1"
    local DESTPATHNAME
    if (echo "$PATHNAME" | grep -q "^file://")
	then PATHNAME="${PATHNAME:7}"
    fi
    if [ "${PATHNAME:0:1}" = "/" ]
	then DESTPATHNAME="$PATHNAME"
    else DESTPATHNAME="$(pwd)/$PATHNAME"
    fi
    echo "$DESTPATHNAME"
}

phase="accessing source file"

if [ "$1" = "" ]
    then 
    if [ "$interactive" = "false" ]
	then my_exit
    fi
else
    srcfile=$(normalize "$1")
    if [ "$interactive" = "false" ]
	then 
	if ! [ -f "$srcfile" ]
	    then my_exit ": $srcfile not found"
	fi
    fi
fi

if [ "$2" != "" ]
    then destfile="$2" 
elif [ "$srcfile" != "" ]
    then destfile=$(dirname "$srcfile")/$(basename "$srcfile" .pdf).$type
fi

phase="initializing values"

if [ "$interactive" = "true" ]    
    then export srcfile destfile resolution color transparent
    cat > ~/.pdf2oo_dialog.kmdr << ____END____
<!DOCTYPE UI><UI version="3.0" stdsetdef="1">
<class>pdf2oo</class>
<widget class="Dialog">
    <property name="name">
        <cstring>pdf2oo</cstring>
    </property>
    <property name="geometry">
        <rect>
            <x>0</x>
            <y>0</y>
            <width>383</width>
            <height>270</height>
        </rect>
    </property>
    <property name="sizePolicy">
        <sizepolicy>
            <hsizetype>5</hsizetype>
            <vsizetype>5</vsizetype>
            <horstretch>0</horstretch>
            <verstretch>0</verstretch>
        </sizepolicy>
    </property>
    <property name="caption">
        <string>pdf2oo</string>
    </property>
    <property name="focusPolicy">
        <enum>NoFocus</enum>
    </property>
    <property name="acceptDrops">
        <bool>true</bool>
    </property>
    <property name="populationText">
        <string></string>
    </property>
    <property name="associations" stdset="0">
        <stringlist>
            <string></string>
            <string>@infile.setText(@env(srcfile))
@outfile.setText(@env(destfile))
@resolution.setText(@env(resolution))
@transparent.setChecked(@env(transparent))
@color.setChecked(@env(color))


</string>
            <string></string>
        </stringlist>
    </property>
    <vbox>
        <property name="name">
            <cstring>unnamed</cstring>
        </property>
        <property name="margin">
            <number>11</number>
        </property>
        <property name="spacing">
            <number>6</number>
        </property>
        <widget class="QLayoutWidget">
            <property name="name">
                <cstring>Layout5</cstring>
            </property>
            <grid>
                <property name="name">
                    <cstring>unnamed</cstring>
                </property>
                <property name="margin">
                    <number>0</number>
                </property>
                <property name="spacing">
                    <number>6</number>
                </property>
                <widget class="Label" row="2" column="0">
                    <property name="name">
                        <cstring>Label1_2</cstring>
                    </property>
                    <property name="text">
                        <string>Output:</string>
                    </property>
                </widget>
                <spacer row="1" column="0">
                    <property name="name">
                        <cstring>Spacer7_2</cstring>
                    </property>
                    <property name="orientation">
                        <enum>Horizontal</enum>
                    </property>
                    <property name="sizeType">
                        <enum>Expanding</enum>
                    </property>
                    <property name="sizeHint">
                        <size>
                            <width>51</width>
                            <height>0</height>
                        </size>
                    </property>
                </spacer>
                <widget class="FileSelector" row="2" column="1" rowspan="1" colspan="2">
                    <property name="name">
                        <cstring>outfile</cstring>
                    </property>
                    <property name="sizePolicy">
                        <sizepolicy>
                            <hsizetype>7</hsizetype>
                            <vsizetype>0</vsizetype>
                            <horstretch>58</horstretch>
                            <verstretch>0</verstretch>
                        </sizepolicy>
                    </property>
                    <property name="acceptDrops">
                        <bool>true</bool>
                    </property>
                    <property name="populationText">
                        <string></string>
                    </property>
                    <property name="associations" stdset="0">
                        <stringlist>
                            <string></string>
                        </stringlist>
                    </property>
                    <property name="selectionFilter">
                        <string>*.odp</string>
                    </property>
                    <property name="selectionType">
                        <enum>Save</enum>
                    </property>
                </widget>
                <widget class="ExecButton" row="1" column="1">
                    <property name="name">
                        <cstring>ExecButton1</cstring>
                    </property>
                    <property name="text">
                        <string>Suggest output file name from &amp;input</string>
                    </property>
                    <property name="associations" stdset="0">
                        <stringlist>
                            <string>@outfile.setText(@infile.text.odp)</string>
                        </stringlist>
                    </property>
                </widget>
                <widget class="FileSelector" row="0" column="1" rowspan="1" colspan="2">
                    <property name="name">
                        <cstring>infile</cstring>
                    </property>
                    <property name="sizePolicy">
                        <sizepolicy>
                            <hsizetype>7</hsizetype>
                            <vsizetype>0</vsizetype>
                            <horstretch>58</horstretch>
                            <verstretch>0</verstretch>
                        </sizepolicy>
                    </property>
                    <property name="acceptDrops">
                        <bool>true</bool>
                    </property>
                    <property name="populationText">
                        <string></string>
                    </property>
                    <property name="associations" stdset="0">
                        <stringlist>
                            <string></string>
                        </stringlist>
                    </property>
                    <property name="selectionFilter">
                        <string>*.pdf</string>
                    </property>
                </widget>
                <spacer row="1" column="2">
                    <property name="name">
                        <cstring>Spacer8</cstring>
                    </property>
                    <property name="orientation">
                        <enum>Horizontal</enum>
                    </property>
                    <property name="sizeType">
                        <enum>Expanding</enum>
                    </property>
                    <property name="sizeHint">
                        <size>
                            <width>91</width>
                            <height>0</height>
                        </size>
                    </property>
                </spacer>
                <widget class="Label" row="0" column="0">
                    <property name="name">
                        <cstring>Label1</cstring>
                    </property>
                    <property name="text">
                        <string>Input:</string>
                    </property>
                </widget>
            </grid>
        </widget>
        <widget class="QLayoutWidget">
            <property name="name">
                <cstring>Layout14</cstring>
            </property>
            <hbox>
                <property name="name">
                    <cstring>unnamed</cstring>
                </property>
                <property name="margin">
                    <number>0</number>
                </property>
                <property name="spacing">
                    <number>6</number>
                </property>
                <widget class="ButtonGroup">
                    <property name="name">
                        <cstring>ButtonGroup1</cstring>
                    </property>
                    <property name="title">
                        <string>Color</string>
                    </property>
                    <property name="selectedId" stdset="0">
                        <number>1</number>
                    </property>
                    <vbox>
                        <property name="name">
                            <cstring>unnamed</cstring>
                        </property>
                        <property name="margin">
                            <number>11</number>
                        </property>
                        <property name="spacing">
                            <number>6</number>
                        </property>
                        <widget class="RadioButton">
                            <property name="name">
                                <cstring>color</cstring>
                            </property>
                            <property name="text">
                                <string>&amp;Color</string>
                            </property>
                        </widget>
                        <widget class="RadioButton">
                            <property name="name">
                                <cstring>grayscale</cstring>
                            </property>
                            <property name="text">
                                <string>&amp;Grayscale</string>
                            </property>
                            <property name="checked">
                                <bool>true</bool>
                            </property>
                        </widget>
                    </vbox>
                </widget>
                <spacer>
                    <property name="name">
                        <cstring>Spacer2</cstring>
                    </property>
                    <property name="orientation">
                        <enum>Horizontal</enum>
                    </property>
                    <property name="sizeType">
                        <enum>Fixed</enum>
                    </property>
                    <property name="sizeHint">
                        <size>
                            <width>16</width>
                            <height>16</height>
                        </size>
                    </property>
                </spacer>
                <widget class="ButtonGroup">
                    <property name="name">
                        <cstring>ButtonGroup1_2</cstring>
                    </property>
                    <property name="title">
                        <string>Transparency</string>
                    </property>
                    <property name="selectedId" stdset="0">
                        <number>1</number>
                    </property>
                    <vbox>
                        <property name="name">
                            <cstring>unnamed</cstring>
                        </property>
                        <property name="margin">
                            <number>11</number>
                        </property>
                        <property name="spacing">
                            <number>6</number>
                        </property>
                        <widget class="RadioButton">
                            <property name="name">
                                <cstring>transparent</cstring>
                            </property>
                            <property name="text">
                                <string>Trans&amp;parent</string>
                            </property>
                        </widget>
                        <widget class="RadioButton">
                            <property name="name">
                                <cstring>opaque</cstring>
                            </property>
                            <property name="text">
                                <string>Op&amp;aque</string>
                            </property>
                            <property name="checked">
                                <bool>true</bool>
                            </property>
                        </widget>
                    </vbox>
                </widget>
                <spacer>
                    <property name="name">
                        <cstring>Spacer3</cstring>
                    </property>
                    <property name="orientation">
                        <enum>Horizontal</enum>
                    </property>
                    <property name="sizeType">
                        <enum>Expanding</enum>
                    </property>
                    <property name="sizeHint">
                        <size>
                            <width>16</width>
                            <height>0</height>
                        </size>
                    </property>
                </spacer>
            </hbox>
        </widget>
        <widget class="QLayoutWidget">
            <property name="name">
                <cstring>Layout13</cstring>
            </property>
            <hbox>
                <property name="name">
                    <cstring>unnamed</cstring>
                </property>
                <property name="margin">
                    <number>0</number>
                </property>
                <property name="spacing">
                    <number>6</number>
                </property>
                <widget class="Label">
                    <property name="name">
                        <cstring>Label2</cstring>
                    </property>
                    <property name="sizePolicy">
                        <sizepolicy>
                            <hsizetype>1</hsizetype>
                            <vsizetype>5</vsizetype>
                            <horstretch>0</horstretch>
                            <verstretch>0</verstretch>
                        </sizepolicy>
                    </property>
                    <property name="text">
                        <string>Resolution:</string>
                    </property>
                </widget>
                <widget class="SpinBoxInt">
                    <property name="name">
                        <cstring>resolution</cstring>
                    </property>
                    <property name="sizePolicy">
                        <sizepolicy>
                            <hsizetype>1</hsizetype>
                            <vsizetype>0</vsizetype>
                            <horstretch>0</horstretch>
                            <verstretch>0</verstretch>
                        </sizepolicy>
                    </property>
                    <property name="maxValue">
                        <number>1200</number>
                    </property>
                    <property name="minValue">
                        <number>50</number>
                    </property>
                    <property name="lineStep">
                        <number>50</number>
                    </property>
                </widget>
                <spacer>
                    <property name="name">
                        <cstring>Spacer9</cstring>
                    </property>
                    <property name="orientation">
                        <enum>Horizontal</enum>
                    </property>
                    <property name="sizeType">
                        <enum>Expanding</enum>
                    </property>
                    <property name="sizeHint">
                        <size>
                            <width>61</width>
                            <height>0</height>
                        </size>
                    </property>
                </spacer>
            </hbox>
        </widget>
        <spacer>
            <property name="name">
                <cstring>Spacer10</cstring>
            </property>
            <property name="orientation">
                <enum>Vertical</enum>
            </property>
            <property name="sizeType">
                <enum>Expanding</enum>
            </property>
            <property name="sizeHint">
                <size>
                    <width>0</width>
                    <height>60</height>
                </size>
            </property>
        </spacer>
        <widget class="QLayoutWidget">
            <property name="name">
                <cstring>Layout11</cstring>
            </property>
            <hbox>
                <property name="name">
                    <cstring>unnamed</cstring>
                </property>
                <property name="margin">
                    <number>0</number>
                </property>
                <property name="spacing">
                    <number>0</number>
                </property>
                <widget class="CloseButton">
                    <property name="name">
                        <cstring>CloseButton3</cstring>
                    </property>
                    <property name="minimumSize">
                        <size>
                            <width>60</width>
                            <height>0</height>
                        </size>
                    </property>
                    <property name="maximumSize">
                        <size>
                            <width>32767</width>
                            <height>30</height>
                        </size>
                    </property>
                    <property name="text">
                        <string>o&amp;k</string>
                    </property>
                    <property name="default">
                        <bool>true</bool>
                    </property>
                    <property name="associations" stdset="0">
                        <stringlist>
                            <string>echo OK
echo \"@infile.text
echo \"@outfile.text
echo @resolution.text
echo @color.checked
echo @transparent.checked
echo @dcopid
</string>
                        </stringlist>
                    </property>
                </widget>
                <spacer>
                    <property name="name">
                        <cstring>Spacer7</cstring>
                    </property>
                    <property name="orientation">
                        <enum>Horizontal</enum>
                    </property>
                    <property name="sizeType">
                        <enum>Fixed</enum>
                    </property>
                    <property name="sizeHint">
                        <size>
                            <width>41</width>
                            <height>30</height>
                        </size>
                    </property>
                </spacer>
                <widget class="CloseButton">
                    <property name="name">
                        <cstring>CloseButton2</cstring>
                    </property>
                    <property name="minimumSize">
                        <size>
                            <width>60</width>
                            <height>0</height>
                        </size>
                    </property>
                    <property name="maximumSize">
                        <size>
                            <width>32767</width>
                            <height>30</height>
                        </size>
                    </property>
                    <property name="text">
                        <string>cance&amp;l</string>
                    </property>
                    <property name="associations" stdset="0">
                        <stringlist>
                            <string>echo CANCEL</string>
                        </stringlist>
                    </property>
                </widget>
                <spacer>
                    <property name="name">
                        <cstring>Spacer8_2</cstring>
                    </property>
                    <property name="orientation">
                        <enum>Horizontal</enum>
                    </property>
                    <property name="sizeType">
                        <enum>Expanding</enum>
                    </property>
                    <property name="sizeHint">
                        <size>
                            <width>61</width>
                            <height>0</height>
                        </size>
                    </property>
                </spacer>
            </hbox>
        </widget>
    </vbox>
</widget>
<connections>
    <connection>
        <sender>CloseButton2</sender>
        <signal>clicked()</signal>
        <receiver>pdf2oo</receiver>
        <slot>reject()</slot>
    </connection>
</connections>
<layoutdefaults spacing="6" margin="11"/>
</UI>

____END____
    oldifs=$IFS
    IFS="
"
    result=($(kmdr-executor ~/.pdf2oo_dialog.kmdr)) 
    rm ~/.pdf2oo_dialog.kmdr
    IFS=$oldifs
    if [ "${result[0]}" != "OK" ]
	then exit 0
    else
	srcfile="${result[1]}"
	srcfile=$(normalize "${srcfile:1}")
	destfile="${result[2]}"
	destfile=$(normalize "${destfile:1}")
	resolution="${result[3]}"
	color="${result[4]}"
	transparent="${result[5]}"
	if [ "$color" = "1" ]
	    then color="true"
	    else color="false"
	fi
	if [ "$transparent" = "1" ]
	    then transparent="true"
	    else transparent="false"
	fi
    fi
fi

############# choosing parameters based on user options

color_opts=""
ppm_opts=""

case "$transparent,$color" in
    "false,false")
	color_opts="-type Grayscale"
	;;
    "false,true")
	;;
    "true,false")
	color_opts="-type Grayscale -transparent white"
	ppm_opts="-aa no"
	;;
    "true,true")
	color_opts="-transparent white"
	ppm_opts="-aa no"
	;;
esac

phase="opening source file"

if ! [ -f "$srcfile" ]
    then my_exit ": file not existent"
fi

phase="checking destination file"

if [ "$destfile" = "" ]
    then my_exit ": no destination file given"
fi

destfile=$(normalize "$destfile") || my_exit

dirdestfile=$(dirname "$destfile")
if ! [ -d "$dirdestfile" ]
    then my_exit ": destination directory not existent"
fi

size=($(pdfinfo "$srcfile"|grep "Page size:" |sed 's@[^0-9]*\([0-9.]*\)[^0-9]*\([0-9.]*\).*@\1pt \2pt@' || my_exit))
height=${size[1]}
width=${size[0]}

phase="creating temporary directory structure"

destdir="$(mktemp -d)" || my_exit
pushd "$destdir" > /dev/null || my_exit 

mkdir document || my_exit 
mkdir document/META-INF || my_exit
mkdir document/Pictures || my_exit

phase="converting pdf file into images"

if [ "$interactive" = "true" ]
    then pbar=$(kdialog --title "Progress" --progressbar "Creating images...")
fi

pushd document/Pictures > /dev/null || my_exit
pages=$(pdfinfo "$srcfile"|grep "Pages:" | cut -b 17-) || my_exit
if [ "$interactive" = "true" ]
    then dcop "$pbar" setTotalSteps $pages
fi
for page in $(seq 1 $pages)
  do pdftoppm $ppm_opts -r $resolution -f $page -l $page "$srcfile" pg || my_exit
     convert +dither +antialias -density $resolution $color_opts *.ppm "$(basename "$srcfile" .pdf)-$page.$imgtype" || my_exit
     rm -f *.ppm
     if [ "$interactive" = "true" ]
	 then dcop "$pbar" setProgress $(($(dcop "$pbar" progress)+1))
     fi
done
popd > /dev/null

if [ "$interactive" = "true" ]
    then dcop "$pbar" setLabel "Creating output file..."
    dcop "$pbar" setTotalSteps 1
    dcop "$pbar" setProgress 0
fi

phase="creating document templates"

genodp(){
echo "application/vnd.oasis.opendocument.presentation" > document/mimetype

cat > document/styles.xml << ____END____
<?xml version="1.0" encoding="utf-8"?>
<office:document-styles xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0"
xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
xmlns:math="http://www.w3.org/1998/Math/MathML"
xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
xmlns:ooo="http://openoffice.org/2004/office"
xmlns:ooow="http://openoffice.org/2004/writer"
xmlns:oooc="http://openoffice.org/2004/calc"
xmlns:dom="http://www.w3.org/2001/xml-events"
xmlns:smil="urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0"
xmlns:anim="urn:oasis:names:tc:opendocument:xmlns:animation:1.0"
office:version="1.0">
<office:styles>
<draw:marker draw:name="Arrow" svg:viewBox="0 0 20 30"
svg:d="m10 0-10 30h20z" />
<style:default-style style:family="graphic">
<style:paragraph-properties style:text-autospace="ideograph-alpha"
style:punctuation-wrap="simple" style:line-break="strict"
style:writing-mode="lr-tb"
style:font-independent-line-spacing="false">
<style:tab-stops />
</style:paragraph-properties>
<style:text-properties style:use-window-font-color="true"
fo:font-family="'Thorndale AMT'" style:font-family-generic="roman"
style:font-pitch="variable" fo:font-size="24pt" fo:language="it"
fo:country="IT" style:font-family-asian="'Albany AMT'"
style:font-pitch-asian="variable" style:font-size-asian="24pt"
style:language-asian="none" style:country-asian="none"
style:font-family-complex="Lucidasans"
style:font-pitch-complex="variable" style:font-size-complex="24pt"
style:language-complex="none" style:country-complex="none" />
</style:default-style>
<style:style style:name="standard" style:family="graphic">
<style:graphic-properties draw:stroke="solid"
svg:stroke-width="0cm" svg:stroke-color="#000000"
draw:marker-start-width="0.3cm" draw:marker-start-center="false"
draw:marker-end-width="0.3cm" draw:marker-end-center="false"
draw:fill="solid" draw:fill-color="#99ccff"
fo:padding-top="0.125cm" fo:padding-bottom="0.125cm"
fo:padding-left="0.25cm" fo:padding-right="0.25cm"
draw:shadow="hidden" draw:shadow-offset-x="0.3cm"
draw:shadow-offset-y="0.3cm" draw:shadow-color="#808080">
<text:list-style>
<text:list-level-style-bullet text:level="1"
text:bullet-char="&#226;&#8212;">
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="2"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="0.6cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="3"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="1.2cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="4"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="1.8cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="5"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="2.4cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="6"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="3cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="7"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="3.6cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="8"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="4.2cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="9"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="4.8cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="10"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="5.4cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
</text:list-style>
</style:graphic-properties>
<style:paragraph-properties fo:margin-left="0cm"
fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm"
fo:line-height="100%" text:enable-numbering="false"
fo:text-indent="0cm" />
<style:text-properties style:use-window-font-color="true"
style:text-outline="false" style:text-line-through-style="none"
fo:font-family="'Albany AMT'" style:font-family-generic="roman"
style:font-pitch="variable" fo:font-size="18pt"
fo:font-style="normal" fo:text-shadow="none"
style:text-underline-style="none" fo:font-weight="normal"
style:font-family-asian="'Albany AMT'"
style:font-pitch-asian="variable" style:font-size-asian="18pt"
style:font-style-asian="normal" style:font-weight-asian="normal"
style:font-family-complex="Lucidasans"
style:font-pitch-complex="variable" style:font-size-complex="18pt"
style:font-style-complex="normal"
style:font-weight-complex="normal" style:text-emphasize="none"
style:font-relief="none" />
</style:style>
<style:style style:name="objectwitharrow" style:family="graphic"
style:parent-style-name="standard">
<style:graphic-properties draw:stroke="solid"
svg:stroke-width="0.15cm" svg:stroke-color="#000000"
draw:marker-start="Arrow" draw:marker-start-width="0.7cm"
draw:marker-start-center="true" draw:marker-end-width="0.3cm" />
</style:style>
<style:style style:name="objectwithshadow" style:family="graphic"
style:parent-style-name="standard">
<style:graphic-properties draw:shadow="visible"
draw:shadow-offset-x="0.3cm" draw:shadow-offset-y="0.3cm"
draw:shadow-color="#808080" />
</style:style>
<style:style style:name="objectwithoutfill" style:family="graphic"
style:parent-style-name="standard">
<style:graphic-properties draw:fill="none" />
</style:style>
<style:style style:name="text" style:family="graphic"
style:parent-style-name="standard">
<style:graphic-properties draw:stroke="none" draw:fill="none" />
</style:style>
<style:style style:name="textbody" style:family="graphic"
style:parent-style-name="standard">
<style:graphic-properties draw:stroke="none" draw:fill="none" />
<style:text-properties fo:font-size="16pt" />
</style:style>
<style:style style:name="textbodyjustfied" style:family="graphic"
style:parent-style-name="standard">
<style:graphic-properties draw:stroke="none" draw:fill="none" />
<style:paragraph-properties fo:text-align="justify" />
</style:style>
<style:style style:name="textbodyindent" style:family="graphic"
style:parent-style-name="standard">
<style:graphic-properties draw:stroke="none" draw:fill="none">
<text:list-style>
<text:list-level-style-bullet text:level="1"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="0.6cm"
text:min-label-width="-0.6cm" text:min-label-distance="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="2"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="0.6cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="3"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="1.2cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="4"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="1.8cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="5"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="2.4cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="6"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="3cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="7"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="3.6cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="8"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="4.2cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="9"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="4.8cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="10"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="5.4cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
</text:list-style>
</style:graphic-properties>
<style:paragraph-properties fo:margin-left="0cm"
fo:margin-right="0cm" fo:text-indent="0.6cm" />
</style:style>
<style:style style:name="title" style:family="graphic"
style:parent-style-name="standard">
<style:graphic-properties draw:stroke="none" draw:fill="none" />
<style:text-properties fo:font-size="44pt" />
</style:style>
<style:style style:name="title1" style:family="graphic"
style:parent-style-name="standard">
<style:graphic-properties draw:stroke="none" draw:fill="solid"
draw:fill-color="#008080" draw:shadow="visible"
draw:shadow-offset-x="0.2cm" draw:shadow-offset-y="0.2cm"
draw:shadow-color="#808080" />
<style:paragraph-properties fo:text-align="center" />
<style:text-properties fo:font-size="24pt" />
</style:style>
<style:style style:name="title2" style:family="graphic"
style:parent-style-name="standard">
<style:graphic-properties svg:stroke-width="0.05cm"
draw:fill-color="#ffcc99" draw:shadow="visible"
draw:shadow-offset-x="0.2cm" draw:shadow-offset-y="0.2cm"
draw:shadow-color="#808080">
<text:list-style>
<text:list-level-style-bullet text:level="1"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="0.2cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="2"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="0.6cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="3"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="1.2cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="4"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="1.8cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="5"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="2.4cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="6"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="3cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="7"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="3.6cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="8"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="4.2cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="9"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="4.8cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
<text:list-level-style-bullet text:level="10"
text:bullet-char="&#226;&#8212;">
<style:list-level-properties text:space-before="5.4cm"
text:min-label-width="0.6cm" />
<style:text-properties fo:font-family="StarSymbol"
style:use-window-font-color="true" fo:font-size="45%" />
</text:list-level-style-bullet>
</text:list-style>
</style:graphic-properties>
<style:paragraph-properties fo:margin-left="0.2cm"
fo:margin-right="0.2cm" fo:margin-top="0.1cm"
fo:margin-bottom="0.1cm" fo:text-align="center"
fo:text-indent="0cm" />
<style:text-properties fo:font-size="36pt" />
</style:style>
<style:style style:name="headline" style:family="graphic"
style:parent-style-name="standard">
<style:graphic-properties draw:stroke="none" draw:fill="none" />
<style:paragraph-properties fo:margin-top="0.42cm"
fo:margin-bottom="0.21cm" />
<style:text-properties fo:font-size="24pt" />
</style:style>
<style:style style:name="headline1" style:family="graphic"
style:parent-style-name="standard">
<style:graphic-properties draw:stroke="none" draw:fill="none" />
<style:paragraph-properties fo:margin-top="0.42cm"
fo:margin-bottom="0.21cm" />
<style:text-properties fo:font-size="18pt" fo:font-weight="bold" />
</style:style>
<style:style style:name="headline2" style:family="graphic"
style:parent-style-name="standard">
<style:graphic-properties draw:stroke="none" draw:fill="none" />
<style:paragraph-properties fo:margin-top="0.42cm"
fo:margin-bottom="0.21cm" />
<style:text-properties fo:font-size="14pt" fo:font-style="italic"
fo:font-weight="bold" />
</style:style>
<style:style style:name="measure" style:family="graphic"
style:parent-style-name="standard">
<style:graphic-properties draw:stroke="solid"
draw:marker-start="Arrow" draw:marker-start-width="0.2cm"
draw:marker-end="Arrow" draw:marker-end-width="0.2cm"
draw:fill="none" />
<style:text-properties fo:font-size="12pt" />
</style:style>
</office:styles>
<office:automatic-styles>
<style:page-layout style:name="PM0">
<style:page-layout-properties fo:margin-top="0.0cm"
fo:margin-bottom="0cm" fo:margin-left="0cm"
fo:margin-right="0cm" fo:page-width="$width"
fo:page-height="$height" style:print-orientation="$portrait" />
</style:page-layout>
<style:style style:name="dp1" style:family="drawing-page">
<style:drawing-page-properties draw:background-size="border"
draw:fill="none" />
</style:style>
<style:style style:name="dp2" style:family="drawing-page">
<style:drawing-page-properties presentation:display-header="true"
presentation:display-footer="true"
presentation:display-page-number="false"
presentation:display-date-time="true" />
</style:style>
</office:automatic-styles>
<office:master-styles>
<draw:layer-set>
<draw:layer draw:name="layout" />
<draw:layer draw:name="background" />
<draw:layer draw:name="backgroundobjects" />
<draw:layer draw:name="controls" />
<draw:layer draw:name="measurelines" />
</draw:layer-set>
<style:master-page style:name="Standard"
style:page-layout-name="PM0" draw:style-name="dp1" />
</office:master-styles>
</office:document-styles>
____END____

cat > document/META-INF/manifest.xml << ____END____
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE manifest:manifest PUBLIC "-//OpenOffice.org//DTD Manifest 1.0//EN" "Manifest.dtd">
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0">
 <manifest:file-entry manifest:media-type="application/vnd.oasis.opendocument.presentation" manifest:full-path="/"/>
 <manifest:file-entry manifest:media-type="application/vnd.sun.xml.ui.configuration" manifest:full-path="Configurations2/"/>
 <manifest:file-entry manifest:media-type="" manifest:full-path="Pictures/"/>
 <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="content.xml"/>
 <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="styles.xml"/>
 <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="meta.xml"/>
 <manifest:file-entry manifest:media-type="" manifest:full-path="Thumbnails/thumbnail.png"/>
 <manifest:file-entry manifest:media-type="" manifest:full-path="Thumbnails/"/>
 <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="settings.xml"/>
</manifest:manifest>
____END____

cat > document/content.xml << ____END____
<?xml version="1.0" encoding="utf-8"?>
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0"
xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
xmlns:math="http://www.w3.org/1998/Math/MathML"
xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
xmlns:ooo="http://openoffice.org/2004/office"
xmlns:ooow="http://openoffice.org/2004/writer"
xmlns:oooc="http://openoffice.org/2004/calc"
xmlns:dom="http://www.w3.org/2001/xml-events"
xmlns:xforms="http://www.w3.org/2002/xforms"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:smil="urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0"
xmlns:anim="urn:oasis:names:tc:opendocument:xmlns:animation:1.0"
office:version="1.0">
<office:scripts />
<office:automatic-styles>
<style:style style:name="gr1" style:family="graphic"
style:parent-style-name="standard">
<style:graphic-properties draw:stroke="none" draw:fill="none"
draw:textarea-horizontal-align="center"
draw:textarea-vertical-align="middle" draw:color-mode="standard"
draw:luminance="0%" draw:contrast="0%" draw:gamma="100%"
draw:red="0%" draw:green="0%" draw:blue="0%"
fo:clip="rect(0cm 0cm 0cm 0cm)" draw:image-opacity="100%"
style:mirror="none" style:protect="position size" />
</style:style>
</office:automatic-styles>
<office:body>
<office:presentation>
____END____

for page in $(seq 1 $pages)
  do cat >> document/content.xml << ____END____
<draw:page draw:name="page$page" draw:style-name="dp1"
draw:master-page-name="Standard">
<draw:frame draw:style-name="gr1" draw:text-style-name="P1"
draw:layer="layout" svg:width="$width" svg:height="$height"
svg:x="0cm" svg:y="0cm">
<draw:image xlink:href="Pictures/$(basename "$srcfile" .pdf)-$page.$imgtype"
xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad">
<text:p />
</draw:image>
</draw:frame>
</draw:page>
____END____
done

cat >> document/content.xml << ____END____
# </office:presentation>
</office:body>
</office:document-content>
____END____
}

case "$type" in
    "odp")
        genodp
	;;
esac

phase="creating destination file"

pushd document > /dev/null
rm "$destfile" -f || my_exit
zip -r "$destfile" * > /dev/null || my_exit
popd > /dev/null
popd > /dev/null

rm -rf "$destdir"

if [ "$interactive" = "true" ]
    then dcop "$pbar" close
fi
