#!/bin/bash


ROOT_DATA=unknown



#  Please edit the line above and specify the ROOT_DATA by replacing
#  ~~~~~~          the word 'unknown' by your ROOT_DATA directory name.
#
#  Make sure *not* to add a trailing slash behind the directory name.
#
#  see http://www.indeview.org/preparations.html#usage_install_root_data
#  see http://www.indeview.org/preparations_es.html#usage_install_root_data
#  see http://www.indeview.org/preparations_de.html#usage_install_root_data
#
##############################################################################





##############################################################################
#
#   IndeViewExport.bas - part of the IndeView project: http://www.indeview.org
#
#
#   This macro exports the currently active presentation in KPresenter
#   ------------------------------------------------------------------
#   and it creates an IndeView format version of it: this enables you
#   to display it using IndeView on PCs not having KPresenter installed.
#
#   Copyright (C) 2003..2004 Michael & Karl-Heinz Zimmer.
#   All rights reserved.
#
#   This file is distributed under the terms of the
#   GNU Lesser General Public License version 2.1 as published by the
#   Free Software Foundation and appearing in the file LICENSE included
#   in the packaging of this file.
#
#   This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
#   THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
#   PURPOSE.
#
#   See http://www.indeview.org for further information.
#
#   Contact info@indeview.org if any conditions of this
#   licensing are not clear to you.
#
#
#   Please consider sending
#   a little picture postcard to   Michael & Karl-Heinz Zimmer
#                                  Butzengasse 15
#                                  54343 Föhren
#                                  Germany
#
#   Your feedback will be very much appreciated: we would be glad about
#   receiving a visual representation of where on earth you are living
#   using our little module.
#
##############################################################################



function exportPage {
  FILENAME="P`echo $2`W`echo $5`.PNG"
  PATHNAME="`echo $1`/$FILENAME"
  dcop $KPRESENTER $VIEW exportPage $2 $3 $4 "file:$PATHNAME" "PNG" 100 $7
  echo "File=$FILENAME `echo $3`x`echo $4`" >> $6
}

function makeDir {
  if [ ! -d $1 ] ; then
    mkdir $1 2> /dev/null
    if [ ! -d $1 ] ; then
      echo ""
      echo "ERROR: Could not create $1/"
      echo ""
      echo "Are you sure you have write permission there?"
      echo ""
      echo ""
      exit 1
    fi
  fi
}

function fileCopy {
  if [ ! -e $1 ] ; then
    echo "ERROR: Can not copy file."
    echo "Source file $1 was not found."
    exit 1
  fi
  echo "source: $1"
  echo "target: $2"
  cp $1 $2 2> /dev/null
  if [ ! -e $2 ] ; then
    echo "ERROR: Could not copy file."
    echo "Destination file $2 was not created."
    echo ""
    echo "Either your disk is full or you have no write permission to this directory."
    echo ""
    echo ""
    exit 1
  fi
}



# use the first KPresenter instance found

KPRESENTER=`dcop | grep -m 1 ^kpresenter`
VIEW=`dcop $KPRESENTER | grep ^View`


if [ "xx--help" = xx$1 ] || [ "xx-h" = xx$1 ] ; then
  echo
  echo
  echo "BaSH script to convert a KOffice KPresenter presentation"
  echo "to the IndeView format ready-to-burn your presentation CD"
  echo
  echo "usage:"
  echo
  echo "    $0   =   save into the directory where you started this script"
  echo
  echo "    $0 DIRECTORYNAME   =   save into an existing directory"
  echo
  exit 0
fi



# We do NOT use `pwd` as default base directory
# because we prefer relative paths or absolute ones.
if [ x$1 = x ] || [ x$1 = x--overwrite-old ]; then
  RUNFILE=INDEVIEW.RUN
  TMPFILE=TEMPFILE.001
  DATADIR=DATA
# Note: The BASEDIR variable specified below
#       is /not/ used for the exportPage function
#       but only for copying the IndeView runtime files.
#       Therefor our DATADIR is /not/ called "./DATA" but just "DATA".
  BASEDIR=.
else
  RUNFILE=$1/INDEVIEW.RUN
  TMPFILE=$1/TEMPFILE.001
  DATADIR=$1/DATA
  BASEDIR=$1
fi

if [ x$ROOT_DATA = x ] || [ "unknown" = $ROOT_DATA ] ; then
  echo ""
  echo ""
  echo ""
  echo ""
  echo ""
  echo "Sorry, can not run this script."
  echo ""
  echo "Problem: environment variable ROOT_DATA is not set or unknown"
  echo ""
  echo "see http://www.indeview.org/preparations.html#usage_install_root_data"
  echo "see http://www.indeview.org/preparations_es.html#usage_install_root_data"
  echo "see http://www.indeview.org/preparations_de.html#usage_install_root_data"
  echo ""
  echo "Please edit $0 and specify the ROOT_DATA at the very top of the script"
  echo "       by replacing the word 'unknown' by your ROOT_DATA directory name."
  echo ""
  echo "Make sure to *not* add a trailing slash behind the directory name."
  echo ""
  echo ""
  echo ""
  exit 1
fi
if [ ! -d $ROOT_DATA ] ; then
  echo "ERROR: $DATADIR is not a directory."
  exit 1
fi
echo ""
echo ""
echo "Preparing your IndeView CD base directory by copying runtime data"
echo "from $ROOT_DATA/"
echo "to $BASEDIR/"
echo ""

if [ -e $RUNFILE ] ; then
  if [ x$1 = x--overwrite-old ] || [ x$2 = x--overwrite-old ]; then
    echo ""
    echo "Old IndeView data files found, erasing them."
    rm $BASEDIR/DATA/P*W*.PNG
    rm $BASEDIR/DATA/PAGE*.INF
    echo ""
    echo "Data erased, proceeding with new files."
    echo ""
  else
    echo ""
    echo ""
    echo "Your target directory $BASEDIR/"
    echo "seems to contain an IndeView presentation already."
    echo ""
    echo "In case you want to replace this old data restart the"
    echo "script specifying the extra parameter --overwrite-old"
    echo "like this:"
    echo ""
    echo "     $0 $1 --overwrite-old"
    echo ""
    echo ""
    exit 1
  fi
fi

cp -a $ROOT_DATA/* $BASEDIR/

if [ $? != 0 ] ; then
  echo ""
  echo ""
  echo ""
  echo ""
  echo "ERROR: Could not copy files to $BASEDIR/"
  echo ""
  echo "Either your disk is full or you have no write permission to this directory."
  echo ""
  echo ""
  exit 1
fi



echo ""
echo ""
echo "Converting your active KPresenter presentation to the IndeView format:"
echo ""

if [ ! -e $DATADIR ] ; then
  mkdir $DATADIR
fi

if [ ! -d $DATADIR ] ; then
  echo "ERROR: Could not create data directory $DATADIR"
  exit 1
fi

echo "Storing pages in directory $DATADIR"
echo ""

echo "# IndeView presentation run file" > $RUNFILE
echo "#" >> $RUNFILE
echo "# visit: http://www.indeview.org" >> $RUNFILE
echo "" >> $RUNFILE
echo "[COMMON]" >> $RUNFILE
echo "Version=IndeView 1.0" >> $RUNFILE
echo "start=Page1" >> $RUNFILE
echo "" >> $RUNFILE


# export all slides

# PAGENUM is the user visible 1-based page number
PAGENUM=1
PREVPAGENUM=$PAGENUM

dcop $KPRESENTER $VIEW screenFirst
while true; do
  echo processing page $PAGENUM
  INFONAME="`echo $DATADIR`/PAGE`echo $PAGENUM`.INF"
  echo "[Page`echo $PAGENUM`]" >> $RUNFILE
  echo "info=$INFONAME" >> $RUNFILE
  echo "# IndeView page information file" > $INFONAME
  echo "#" >> $INFONAME
  echo "# visit: http://www.indeview.org" >> $INFONAME
  echo "" >> $INFONAME
  exportPage $DATADIR $PAGENUM 640 480 6 $INFONAME 1 > $TMPFILE
  echo "."
  exportPage $DATADIR $PAGENUM 704 576 7 $INFONAME 0 > /dev/null
  echo "."
  exportPage $DATADIR $PAGENUM 800 600 8 $INFONAME 0 > /dev/null
  echo "."
  exportPage $DATADIR $PAGENUM 1024 768 10 $INFONAME 0 > /dev/null
  echo "."
  exportPage $DATADIR $PAGENUM 1152 864 11 $INFONAME 0 > /dev/null
  echo "."
  exportPage $DATADIR $PAGENUM 1280 1024 12 $INFONAME 0 > /dev/null
  echo "."
  exportPage $DATADIR $PAGENUM 1600 1200 16 $INFONAME 0 > /dev/null
  echo "."
  cat $TMPFILE
  echo "" >> $INFONAME
  cat $TMPFILE >> $INFONAME
  echo "" >> $INFONAME
  echo "ChangeToNext=USER" >> $INFONAME
  echo "" >> $INFONAME
  echo "--------------------"
  echo ""
  dcop $KPRESENTER $VIEW screenNext
  PAGENUM=`dcop $KPRESENTER $VIEW getCurrentPageNum`
  if [ $PAGENUM -le $PREVPAGENUM ] ; then
    echo "next=NONE" >> $RUNFILE
    echo "" >> $RUNFILE
    break
  fi
  echo "next=Page`echo $PAGENUM`" >> $RUNFILE
  echo "" >> $RUNFILE
  PREVPAGENUM=$PAGENUM
done
rm $TMPFILE

echo "Conversion finished.  Thank you for using IndeView."
echo ""
echo ""
echo "Burn the complete contents of $BASEDIR/"
echo "onto a CD to get an autostarting IndeView presentation CD."
echo ""
echo ""

# end of script.
