#!/bin/sh # Copyright (c) 2008 David R. Forrest (Forrest) # # Permission to use, copy, modify, and distribute this material # for any purpose and without fee is hereby granted, provided # that the above copyright notice and this permission notice # appear in all copies, and that the name of Forrest not be # used in advertising or publicity pertaining to this # material without the specific, prior written permission # of an authorized representative of Forrest. FORREST # MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY # OF THIS MATERIAL FOR ANY PURPOSE. IT IS PROVIDED "AS IS", # WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. # Converts octet-stream to display calls # Idea from James Budiono 6/15/06 from Hyla Fax List posting # D. R. Forrest 6/15/06 revised for my pine application # DRF 6/20/07 updated and clarified and added more types. # DRF 12/21/07 added compressed and Opendocument types # DRF 4/6/08 converted to a "case" from "elif" structure # DRF 5/15/08 added some Open Office links under "data" types # DRF 7/7/08 added .docx detection under "compressed" types # FUNCTIONS START HERE function cmd_exist { which $1 >/dev/null 2>&1 && return # yes it does exist echo "Need the command \"$1\" in the executable path to view this file. Sorry." case $1 in tnef) echo "See http://sourceforge.net/project/showfiles.php?group_id=57057 for a free download of tnef." ;; OdfConverter) echo "See http://www.oooninja.com/2008/01/convert-openxml-docx-etc-in-linux-using.html" ;; soffice) echo "See http://www.libreoffice.org/" ;; xv) echo "See http://www.trilon.com/xv/" ;; *) ;; esac sleep 10 exit } # SCRIPT STARTS HERE cmd_exist file cmd_exist awk cmd_exist cp cmd_exist rm cmd_exist octet-stream cmd_exist grep cmd_exist cut cmd_exist sleep # Be especially careful that a target does NOT match a prior one in this list # as FILETYPE is determined by FIRST match. FILETYPE=$(file -b "$1" | awk '\ /(Microsoft Office Document|Rich Text|OpenDocument|Corel|OpenOffice|CDF V2 Document)/ { print "office";next};\ /Zip/ {print "compressed";next};\ /gzip/ {print "gzipped";next};\ /JPEG image data/ {print "jpg";next};\ /Targa image data/ {print "Targa";next};\ /bzip2/ {print "bzipped"; next};\ /PDF.*4/ {print "pdfV4";next};\ /PDF/ {print "pdf";next};\ /(ASCII|shell script)/ {print "ascii";next};\ /(PGP armored data|mail text|output text|calendar)/ {print "ascii";next};\ /(UTF-8 Unicode English text|Unicode text)/ {print "ascii";next};\ /HTML document text/ {print "html";next};\ /PostScript/ {print "ps";next};\ /vCard/ {print "vcard";next};\ /Transport Neutral Encapsulation Format/ { print "tnef";next};\ /(Microsoft ASF|Motion JPEG|MPEG|ISO Media)/ {print "video";next};\ /(LSB relocatable|LSB executable|ELF 64-bit LSB|magic binary)/ {print "binexe";next};\ /(image data|PNG image)/ {print "image";next};\ /(JPEG|GIF|Netpbm PBM)/ {print "jpg";next};\ /data/ {print "data";next};\ /Qemu Image/ {print "Qemu";next};') #echo FILETYPE IS: \"$FILETYPE\" and the Work file is named: \""$1"\" # for debuging #cat "$1" >~/attachment # for debuging #env # for debuging [ -n "$SSH_CONNECTION" ] && { echo "Remote display - try save" sleep 6 ; exit; } echo -e "\n...Viewing file $1 using octet-stream display script" case $FILETYPE in office) # it's MS-Office or OpenDocument group, use soffice cmd_exist soffice soffice -view "$1" ;; compressed) # Microsoft Office 2007 (*.[doc,ppt,xls]x) is also seen as a zip file case "${1##*.}" in # check only the filename extension docx|pptx|xlsx|pps) # LibreOffice v3.0 + will handle these (comment out ooV2 entries following) cmd_exist soffice soffice -view "$1" ;; # docx) # Office 2007+ # # Uses OdfConverter (this script x86_64) from: # # http://www.oooninja.com/2008/01/convert-openxml-docx-etc-in-linux-using.html # cmd_exist OdfConverter # cmd_exist soffice # echo "Processing .docx file- May take a minute......" # OUTFILE=${1%.*}.odt # Get converter output file name for soffice # OdfConverter /docx2odt /i "$1" >/dev/null 2>&1 # soffice -view "$OUTFILE" # rm "$OUTFILE";; # pptx) # Office 2007+ # # Uses OdfConverter (this script x86_64) from: # # http://www.oooninja.com/2008/01/convert-openxml-docx-etc-in-linux-using.html # cmd_exist OdfConverter # cmd_exist soffice # echo "Processing .pptx file- May take a minute......" # OUTFILE=${1%.*}.odp # Get converter output file name for soffice # OdfConverter /pptx2odp /i "$1" >/dev/null 2>&1 # soffice -view "$OUTFILE" # rm "$OUTFILE";; # xlsx) # Office 2007+ # # Uses OdfConverter (this script x86_64) from: # # http://www.oooninja.com/2008/01/convert-openxml-docx-etc-in-linux-using.html # cmd_exist OdfConverter # cmd_exist soffice # echo "Processing .xlsx file- May take a minute......" # OUTFILE=${1%.*}.ods # Get converter output file name for soffice # OdfConverter /xlsx2ods /i "$1" >/dev/null 2>&1 # soffice -view "$OUTFILE" # rm "$OUTFILE";; *) # a real Zip file cp "$1" "$1.zip" cmd_exist file-roller file-roller "$1.zip" rm "$1.zip" ;; esac ;; bzipped) cmd_exist tar tar -jtf "$1" >/dev/null 2>&1 && { # yes - tarred cp "$1" "$1.tar.bz2" cmd_exist file-roller file-roller "$1.tar.bz2" rm "$1.tar.bz2" exit } cp "$1" "$1.1.bz2" # no - untarred cmd_exist bunzip2 bunzip2 "$1.1.bz2" octet-stream "$1.1" rm "$1.1.bz2" "$1.1" ;; gzipped) cmd_exist tar tar -ztf "$1" >/dev/null 2>&1 && { # yes - tarred cp "$1" "$1.tar.gz" cmd_exist file-roller file-roller "$1.tar.gz" rm "$1.tar.gz" exit } cp "$1" "$1.1.gz" # no - untarred cmd_exist gunzip gunzip "$1.1.gz" octet-stream "$1.1" rm "$1.1.gz" "$1.1" ;; html) cmd_exist firefox firefox "$1" ;; pdf) # it is PDF, then display it cmd_exist evince evince "$1" ;; image) # image uses eog cmd_exist eog eog "$1" ;; pdfV4) # it is PDF version 1.4 cmd_exist evince evince "$1" ;; data | ascii) # bin/file returns "data" for .od? in addition to ascii types case "${1##*.}" in # only the filename extension od[c,b,f,g,i,p,s,m,t] | ot[g,p,t] | sx[c,w] ) # soffice extensions cmd_exist soffice soffice -view "$1" ;; *) # it's an ascii file or shell script, show it in a viewer cmd_exist gedit gedit "$1" ;; esac ;; ps) # display it cmd_exist xv xv "$1" ;; jpg) # display it cmd_exist xv xv "$1" ;; video) # play it cmd_exist mplayer mplayer -vo x11 -zoom "$1" ;; tnef) # Microsoft tnef cmd_exist tnef # See http://sourceforge.net/project/showfiles.php?group_id=57057 for a free download of tnef. # Get the files into /tmp/tnef-docs [ -d /tmp/tnef_docs ] || mkdir -p /tmp/tnef_docs >/dev/null 2>&1 tnef --body-pref=all --number-backups --directory=/tmp/tnef_docs $1 # Display and delete files found by tnef using octet-stream DIRSIZE=`ls -l /tmp/tnef_docs/|grep ^total |cut -b7-` test $DIRSIZE = 0 && { echo "No files in $1" ; sleep 5 ; exit 1; } # bail if empty for i in /tmp/tnef_docs/*; do octet-stream "$i" && rm "$i" ; done ;; binexe) # binary, do not run it here - view only cmd_exist xv xv "$1";; vcard) # We don't have any use for these yet - so ignore echo "Got a vCard! --ignoring...." sleep 10 ;; Targa) cmd_exist xv xv "$1" && exit # first try it with eog, exit if it works echo "jbig image format -- needs libraries we do not have. Try save" file -b "$1" sleep 10 ;; Qemu) echo "A Qemu image file - large virtual memory image. Try save." file -b "$1" sleep 10 ;; * ) echo " " cmd_exist xterm xterm -e echo -e "Sorry. Don't know how to handle $1. Try save. \n" file "$1" sleep 10 ;; esac exit 0