#!/bin/bash
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#v426 nikin: bugfix.
#120202 rodin.s: internationalized.
#120329 Xdialog bug --yesno supposed to "yes" "No" buttons, but they are "OK" "Cancel".

export TEXTDOMAIN=puppypdf
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8
eval_gettext () {
  local myMESSAGE=$(gettext "$1")
  eval echo \"$myMESSAGE\"
}

Yes_lbl="$(gettext 'YES')"
No_lbl="$(gettext 'NO')"

#info screen
#choose input file
#specify output file name
#result notification
#
##----------- convert file to pdf ---------->
#
# author: thoughtjourney, 13/06/2005
#
##------------------------------------------>


#------------------------------variables--------------------------------->

INPUT=""
OUTPUT=""
TEMP="temp.ps"
GUI=0
SUPP="$(gettext 'The Puppy PDF Conversion Wizard (puppyPDF) takes an input file\nand converts it to PDF format. The wizard relies on 3 external programs:\n\nbash, Abiword, and ps2pdf\n\nand is recommended for use with Puppy version 1.03 or later,\nsince these are included with the standard iso.\n\n=== SUPPORTED FILE FORMATS ===\n\nAbiword Documents (.abw, .awt)\n\nMicrosoft Word Documents (.doc, .dot)\n\nRich Text Format Documents (.rtf)\n\nText Documents (.txt, .text)')\n\n=============================="

#-------------------------------functions-------------------------------->

#---prints puppyPDF help--->
function usage  
{
   echo
   echo -e "puppyPDF [OPTIONS... ]\n"
   echo -e "-i                   $(gettext 'input file')"
   echo -e "-o                   $(gettext 'output file')"
   echo -e "-h, --help           $(gettext 'prints help')"
   echo
   echo -e "$SUPP"
}

#---gui version of events--->
function gui
{
   splash
   exit 0
}

#---checks to ensure cli arguments are valid--->
function checkArgs  
{
   if [ -f "$INPUT" ]; then

        #--- check to ensure file is not already pdf --->
        file "$INPUT" > /tmp/type
        test=`grep -n PDF /tmp/type`


        if [ "$test" = "" ]; then
           rm -f /tmp/type #not a pdf file

        else
           #--- if gui is running... --->
           if [ $GUI -eq 1 ]; then
                Xdialog --title "$(gettext 'PDF file selected')" --ok-label "$Yes_lbl" --cancel-label "$No_lbl" --yesno "$(gettext 'A PDF file was selected.\n\nPress YES to try again,\n\n or NO to quit')\n\n" 0 0
                case $? in
                 0)
                   chooseFile
                   exit 0;;
                 1)
                   exit 0;;
                 255)
                  echo "";;
                esac
           else 
                #--- for cli interface --->
                echo -e "\n$(gettext 'Please specify a valid input file')\n"
                usage
                exit 0
          fi
        fi
        rm -f /tmp/type

   else

      #--- input is not a file --->
      if [ $GUI -eq 1 ]; then
           Xdialog --title "$(gettext 'No file selected')" --ok-label "$Yes_lbl" --cancel-label "$No_lbl" --yesno "$(gettext 'Invalid file selected.\n\nPress YES to try again,\n\n or NO to quit')\n\n" 0 0
         case $? in
           0)
             chooseFile
             exit 0;;
           1)
             exit 0;;
           255)
             echo "";;
         esac
      else 
         echo -e "\n$(gettext 'Please specify a valid input file')\n"
         usage
         exit 0
      fi
   fi


   #--- no output filename specified --->
   if [ "$OUTPUT" = "" ]; then
      OUTPUT="$INPUT.pdf"
      echo -e "$(gettext 'no output filename specified, using') $INPUT.pdf\n"
      if [ $GUI -eq 1 ]; then
         Xdialog --title "INFO BOX" \
                 --infobox "$(gettext 'no output filename specified,\nusing') $INPUT.pdf" 13 45 20000
      fi         
   fi


   #--- specified output file already exists --->
   if [ -f "$OUTPUT" ]; then
       if [ $GUI -eq 1 ]; then
            Xdialog --title "$(gettext 'Filename already exists')" --ok-label "$Yes_lbl" --cancel-label "$No_lbl" --yesno "$OUTPUT $(gettext 'already exists\n\nPress YES to overwrite,\n\nor NO to change')\n\n" 0 0
         case $? in
           0)
             echo "";;
           1)
             outputName;;
           255)
             echo "";;
         esac

       else      
         echo -e "$OUTPUT $(gettext 'already exists! Press ENTER to continue or CTRL-C to quit')\n"
         read in
       fi
   fi

}

#---splashscreen--->
function splash
{
   Xdialog --title "$(gettext 'Puppy PDF Conversion Wizard')" --help "$SUPP" --ok-label "$Yes_lbl" --cancel-label "$No_lbl"\
           --yesno "$(gettext 'WELCOME to the Puppy PDF Conversion Wizard!')\n\n\
$(gettext 'Press YES to choose the file to convert,\n\n NO to exit,\n\n or HELP for more info')\n\n" 0 0

   case $? in
     0)
       chooseFile;;
     1)
       echo ""
       exit 1;;
     255)
       echo "";;
   esac
}

#---choose file to convert to pdf--->
function chooseFile
{
#Fixed so that the program doesnt crashon GTK error messages apperaring changed /root to ~ for compatibility reasons
   INPUT=`Xdialog --stdout --title "Choose File to Convert" --fselect ~ 28 60 2>/dev/null`

   case $? in
	   0)
             Xdialog --title "$(gettext 'Next step: save as...')"\
                     --infobox "$(gettext 'The next step is to specify the name of your pdf file.\n\n Press OK to continue.')\n\n" 0 0 10000
             outputName;;
	   1)
             splash;;
	   255)
             echo "";;
   esac

}


#---select output filename--->
function outputName
{
   OUTPUT=`Xdialog --stdout --title "Save As..." --fselect "$INPUT.pdf" 28 60 2>/dev/null`

   case $? in
	   0)
             finalConfirm;;
	   1)
             splash;;
	   255)
             echo "";;
   esac
}

#---final confirmation--->
function finalConfirm
{
   checkArgs
   Xdialog --wrap --title "$(gettext 'Confirm...')" --ok-label "$Yes_lbl" --cancel-label "$No_lbl" \
           --yesno "$(gettext 'The Puppy PDF Conversion Wizard') \n\
`eval_gettext \"will now convert\n\n\\\$INPUT\n\nto the pdf file\"`\n\n$OUTPUT\n\n\
$(gettext 'If this is correct, choose YES\n To quit, choose NO')\n\n" 0 0

   case $? in
     0)
       convert
       display;;
     1)
       splash;;
     255)
       echo "";;
   esac 
}

#---performs the file conversion--->
function convert 
{
   echo printing...

   #-- convert to ps--->
   res=`abiword --to=ps --to-name="$TEMP" "$INPUT"`
 
   if [ "$res" = "" ]; then

       #-- convert to pdf--->
       res2=`ps2pdf "$TEMP" "$OUTPUT"`

       if [ "$res2" = "" ]; then

          #--- cleanup --->
          rm -f "$TEMP"

          echo -e "done!\n"

       else
          echo RES2 equals $res2
          echo -e "$(gettext 'ps2pdf error! Exiting...')"
          Xdialog --title "$(gettext 'File Conversion error!')"\
                  --infobox "$(gettext 'There was an error in the file conversion process!\n\nCheck that your input file format is supported.\n\n\
Press OK to exit').\n\n" 0 0 20000
          rm -f "$OUTPUT"	
          exit 1
       fi
   else
      echo RES equals $res
      echo -e "$(gettext 'abiword error! Exiting...')"
      Xdialog --title "$(gettext 'File Conversion error!')"\
                  --infobox "$(gettext 'There was an error in the file conversion process!\n\nCheck that your input file format is supported.\n\n\
Press OK to exit').\n\n" 0 0 20000
      rm -f "$OUTPUT"
      exit 1
   fi	
}

#---opens the pdf document--->
function display
{
   ghostview "$OUTPUT" &
}

#---commandline sequence--->
function cli
{
   checkArgs
   convert
   display
}
#---------------------------------main-------------------------------->

if [ "$1" ]; then
   #cli
   while [ "$1" != "" ]; do
    case $1 in
        -i | -I)                shift
                                INPUT=$1
                                ;;
        -o | -O)                shift
                                OUTPUT=$1
                                ;;	 
        -h | --help )           usage
                                exit
                                ;;
        * )                     gui
                                exit 1
    esac
    shift
   done

   cli

else
   GUI=1
   gui
   exit 0
fi

exit 0

#------------------------------------------------------------------->


