#!/bin/bash
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#v426 nikin: bugfix.
# jamesbond June 2014 - convert directly to PDF, bypassing ps2pdf

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


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

PDFVIEWER=evince
INPUT=""
OUTPUT=""
TEMP="temp.ps"
GUI=0
SUPP="The Puppy PDF Conversion Wizard (puppyPDF) takes an input file\nand converts it to PDF format.\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                   input file"
   echo -e "-o                   output file"
   echo -e "-h, --help           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 "PDF file selected"\
                        --yesno "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 "\nPlease 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 "No file selected"\
                   --yesno "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 "\nPlease specify a valid input file\n"
         usage
         exit 0
      fi
   fi


   #--- no output filename specified --->
   if [ "$OUTPUT" = "" ]; then
      OUTPUT="$INPUT.pdf"
      echo -e "no output filename specified, using $INPUT.pdf\n"
      if [ $GUI -eq 1 ]; then
         Xdialog --title "INFO BOX" \
                 --infobox "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 "Filename already exists"\
                    --yesno "$OUTPUT 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 already exists! Press ENTER to continue or CTRL-C to quit\n"
         read in
       fi
   fi

}

#---splashscreen--->
function splash
{
   Xdialog --title "Puppy PDF Conversion Wizard"\
           --help "$SUPP"\
           --yesno "WELCOME to the Puppy PDF Conversion Wizard!\n\n\
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 "Next step: save as..."\
                     --infobox "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 "Confirm..."\
           --yesno "The Puppy PDF Conversion Wizard \n\
will now convert\n\n$INPUT\n\nto the pdf file\n\n$OUTPUT\n\n\
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--->
   if abiword --to=pdf --to-name="$OUTPUT" "$INPUT"; then
		echo -e "done!\n"
   else
      echo -e "abiword error! Exiting..."
      Xdialog --title "File Conversion error!"\
                  --infobox "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
{
   $PDFVIEWER "$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

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


