home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 August - Disc 1 / PCNET_CD_2006_08_1.iso / linux / puppy-barebones-2.01r2.iso / pup_201.sfs / usr / sbin / puppypdf < prev    next >
Encoding:
Text File  |  2005-06-26  |  7.0 KB  |  296 lines

  1. #!/bin/bash
  2.  
  3. #info screen
  4. #choose input file
  5. #specify output file name
  6. #result notification
  7. #
  8. ##----------- convert file to pdf ---------->
  9. #
  10. # author: thoughtjourney, 13/06/2005
  11. #
  12. ##------------------------------------------>
  13.  
  14.  
  15. #------------------------------variables--------------------------------->
  16.  
  17. INPUT=""
  18. OUTPUT=""
  19. TEMP="temp.ps"
  20. GUI=0
  21. SUPP="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=============================="
  22.  
  23. #-------------------------------functions-------------------------------->
  24.  
  25. #---prints puppyPDF help--->
  26. function usage  
  27. {
  28.    echo
  29.    echo -e "puppyPDF [OPTIONS... ]\n"
  30.    echo -e "-i                   input file"
  31.    echo -e "-o                   output file"
  32.    echo -e "-h, --help           prints help"
  33.    echo
  34.    echo -e "$SUPP"
  35. }
  36.  
  37. #---gui version of events--->
  38. function gui
  39. {
  40.    splash
  41.    exit 0
  42. }
  43.  
  44. #---checks to ensure cli arguments are valid--->
  45. function checkArgs  
  46. {
  47.    if [ -f "$INPUT" ]; then
  48.  
  49.         #--- check to ensure file is not already pdf --->
  50.         file "$INPUT" > /tmp/type
  51.         test=`grep -n PDF /tmp/type`
  52.  
  53.  
  54.         if [ "$test" = "" ]; then
  55.            rm -f /tmp/type #not a pdf file
  56.  
  57.         else
  58.            #--- if gui is running... --->
  59.            if [ $GUI -eq 1 ]; then
  60.                 Xdialog --title "PDF file selected"\
  61.                         --yesno "A PDF file was selected.\n\nPress YES to try again,\n\n or NO to quit\n\n" 0 0
  62.                 case $? in
  63.                  0)
  64.                    chooseFile
  65.                    exit 0;;
  66.                  1)
  67.                    exit 0;;
  68.                  255)
  69.                   echo "";;
  70.                 esac
  71.            else 
  72.                 #--- for cli interface --->
  73.                 echo -e "\nPlease specify a valid input file\n"
  74.                 usage
  75.                 exit 0
  76.           fi
  77.         fi
  78.         rm -f /tmp/type
  79.  
  80.    else
  81.  
  82.       #--- input is not a file --->
  83.       if [ $GUI -eq 1 ]; then
  84.            Xdialog --title "No file selected"\
  85.                    --yesno "Invalid file selected.\n\nPress YES to try again,\n\n or NO to quit\n\n" 0 0
  86.          case $? in
  87.            0)
  88.              chooseFile
  89.              exit 0;;
  90.            1)
  91.              exit 0;;
  92.            255)
  93.              echo "";;
  94.          esac
  95.       else 
  96.          echo -e "\nPlease specify a valid input file\n"
  97.          usage
  98.          exit 0
  99.       fi
  100.    fi
  101.  
  102.  
  103.    #--- no output filename specified --->
  104.    if [ "$OUTPUT" = "" ]; then
  105.       OUTPUT="$INPUT.pdf"
  106.       echo -e "no output filename specified, using $INPUT.pdf\n"
  107.       if [ $GUI -eq 1 ]; then
  108.          Xdialog --title "INFO BOX" \
  109.                  --infobox "no output filename specified,\nusing $INPUT.pdf" 13 45 20000
  110.       fi         
  111.    fi
  112.  
  113.  
  114.    #--- specified output file already exists --->
  115.    if [ -f "$OUTPUT" ]; then
  116.        if [ $GUI -eq 1 ]; then
  117.             Xdialog --title "Filename already exists"\
  118.                     --yesno "$OUTPUT already exists\n\nPress YES to overwrite,\n\nor NO to change\n\n" 0 0
  119.          case $? in
  120.            0)
  121.              echo "";;
  122.            1)
  123.              outputName;;
  124.            255)
  125.              echo "";;
  126.          esac
  127.  
  128.        else      
  129.          echo -e "$OUTPUT already exists! Press ENTER to continue or CTRL-C to quit\n"
  130.          read in
  131.        fi
  132.    fi
  133.  
  134. }
  135.  
  136. #---splashscreen--->
  137. function splash
  138. {
  139.    Xdialog --title "Puppy PDF Conversion Wizard"\
  140.            --help "$SUPP"\
  141.            --yesno "WELCOME to the Puppy PDF Conversion Wizard!\n\n\
  142. Press YES to choose the file to convert,\n\n NO to exit,\n\n or HELP for more info\n\n" 0 0
  143.  
  144.    case $? in
  145.      0)
  146.        chooseFile;;
  147.      1)
  148.        echo ""
  149.        exit 1;;
  150.      255)
  151.        echo "";;
  152.    esac
  153. }
  154.  
  155. #---choose file to convert to pdf--->
  156. function chooseFile
  157. {
  158.    INPUT=`Xdialog --title "Choose File to Convert" --fselect /root/my-documents 28 60 2>&1`
  159.  
  160.    case $? in
  161.        0)
  162.              Xdialog --title "Next step: save as..."\
  163.                      --infobox "The next step is to specify the name of your pdf file.\n\n Press OK to continue.\n\n" 0 0 10000
  164.              outputName;;
  165.        1)
  166.              splash;;
  167.        255)
  168.              echo "";;
  169.    esac
  170.  
  171. }
  172.  
  173.  
  174. #---select output filename--->
  175. function outputName
  176. {
  177.    OUTPUT=`Xdialog --title "Save As..." --fselect "$INPUT.pdf" 28 60 2>&1`
  178.  
  179.    case $? in
  180.        0)
  181.              finalConfirm;;
  182.        1)
  183.              splash;;
  184.        255)
  185.              echo "";;
  186.    esac
  187. }
  188.  
  189. #---final confirmation--->
  190. function finalConfirm
  191. {
  192.    checkArgs
  193.    Xdialog --wrap --title "Confirm..."\
  194.            --yesno "The Puppy PDF Conversion Wizard \n\
  195. will now convert\n\n$INPUT\n\nto the pdf file\n\n$OUTPUT\n\n\
  196. If this is correct, choose YES\n To quit, choose NO\n\n" 0 0
  197.  
  198.    case $? in
  199.      0)
  200.        convert
  201.        display;;
  202.      1)
  203.        splash;;
  204.      255)
  205.        echo "";;
  206.    esac 
  207. }
  208.  
  209. #---performs the file conversion--->
  210. function convert 
  211. {
  212.    echo printing...
  213.  
  214.    #-- convert to ps--->
  215.    res=`abiword --print="$TEMP" "$INPUT"`
  216.  
  217.    if [ "$res" = "" ]; then
  218.  
  219.        #-- convert to pdf--->
  220.        res2=`ps2pdf "$TEMP" "$OUTPUT"`
  221.  
  222.        if [ "$res2" = "" ]; then
  223.  
  224.           #--- cleanup --->
  225.           rm -f "$TEMP"
  226.  
  227.           echo -e "done!\n"
  228.  
  229.        else
  230.           echo RES2 equals $res2
  231.           echo -e "ps2pdf error! Exiting..."
  232.           Xdialog --title "File Conversion error!"\
  233.                   --infobox "There was an error in the file conversion process!\n\nCheck that your input file format is supported.\n\n\
  234. Press OK to exit.\n\n" 0 0 20000
  235.           rm -f "$OUTPUT"    
  236.           exit 1
  237.        fi
  238.    else
  239.       echo RES equals $res
  240.       echo -e "abiword error! Exiting..."
  241.       Xdialog --title "File Conversion error!"\
  242.                   --infobox "There was an error in the file conversion process!\n\nCheck that your input file format is supported.\n\n\
  243. Press OK to exit.\n\n" 0 0 20000
  244.       rm -f "$OUTPUT"
  245.       exit 1
  246.    fi    
  247. }
  248.  
  249. #---opens the pdf document--->
  250. function display
  251. {
  252.    ghostview "$OUTPUT" &
  253. }
  254.  
  255. #---commandline sequence--->
  256. function cli
  257. {
  258.    checkArgs
  259.    convert
  260.    display
  261. }
  262. #---------------------------------main-------------------------------->
  263.  
  264. if [ "$1" ]; then
  265.    #cli
  266.    while [ "$1" != "" ]; do
  267.     case $1 in
  268.         -i | -I)                shift
  269.                                 INPUT=$1
  270.                                 ;;
  271.         -o | -O)                shift
  272.                                 OUTPUT=$1
  273.                                 ;;     
  274.         -h | --help )           usage
  275.                                 exit
  276.                                 ;;
  277.         * )                     gui
  278.                                 exit 1
  279.     esac
  280.     shift
  281.    done
  282.  
  283.    cli
  284.  
  285. else
  286.    GUI=1
  287.    gui
  288.    exit 0
  289. fi
  290.  
  291. exit 0
  292.  
  293. #------------------------------------------------------------------->
  294.  
  295.  
  296.