Fixed multiple bugs related to spaces in filenames.
[scripts.git] / print.sh
index fc88668..c3815f4 100755 (executable)
--- a/print.sh
+++ b/print.sh
@@ -30,7 +30,13 @@ tmp=$(mktemp /tmp/print.ps.XXXXXX)
 opts=""
 printing_cmd="lpr"
 
-for arg in $*; do
+function cleanup-before-exit () {
+    rm ${tmp}
+}
+
+trap cleanup-before-exit EXIT
+
+for arg in "$@"; do
 
     if [ "${arg}" == "-h" ] ||  [ "${arg}" == "--help" ]; then
 
@@ -50,7 +56,6 @@ for arg in $*; do
         echo "Contact <francois.fleuret@epfl.ch>"
         echo "Version \$Id: print.sh,v 1.19 2006-11-10 15:53:23 fleuret Exp $"
 
-        \rm ${tmp}
         exit 0
 
     elif [[ ${arg} == "-"* ]]; then
@@ -63,42 +68,47 @@ for arg in $*; do
 
         # else, it is a filename to print
 
-        if [ -e ${arg} ]; then
+        if [ -e "${arg}" ]; then
 
-            TYPE=$(file -L ${arg})
+            TYPE=$(file -L "${arg}")
 
             noprint=""
 
             case $TYPE in
 
                 *"image data"*)
-                    convert ${arg} ps:${tmp}
+                    convert "${arg}" ps:${tmp}
                     ;;
 
                 *"TeX DVI"*)
-                    dvips -o ${tmp} ${arg}
+                    dvips -o ${tmp} "${arg}"
                     ;;
 
                 *"FIG image"*)
-                    fig2dev -L ps ${arg} > ${tmp}
+                    fig2dev -L ps "${arg}" > ${tmp}
                     ;;
 
                 *"PostScript"*)
-                    cp ${arg} ${tmp}
+                    cp "${arg}" ${tmp}
                     ;;
 
                 *"PDF"*)
-                    pdf2ps ${arg} ${tmp}
+                    pdf2ps "${arg}" ${tmp}
                     ;;
 
                 *" text"*)
-                    # a2ps -B -R --columns=1 -f 8 -o ${tmp} ${arg}
-                    a2ps --media=A4 --user-option=lp -o ${tmp} ${arg}
+                    # a2ps -B -R --columns=1 -f 8 -o ${tmp} "${arg}"
+                    # if isutf8 -q ${tmp}; then
+                        # echo "Can not print uf8 text."
+                        # exit 1
+                    # else
+                    a2ps --media=A4 --user-option=lp -o ${tmp} "${arg}"
+                    # fi
                     ;;
 
                 *"gzip compressed"*)
                     echo "Unzipping  "
-                    zcat ${arg} > ${tmp}
+                    zcat "${arg}" > ${tmp}
                     noprint=1
                     $0 ${opts} ${tmp}
                     ;;
@@ -152,7 +162,6 @@ for arg in $*; do
                             ;;
                         *)
                             echo "Unknown option $f"
-                            \rm ${tmp}
                             exit 1;;
                     esac
                 done
@@ -176,8 +185,6 @@ for arg in $*; do
 
 done
 
-\rm ${tmp}
-
 if [[ ${show_queue} ]]; then
     lpq
 fi