Added quotes around filenames.
[scripts.git] / viewer.sh
1 #!/bin/sh
2
3 #########################################################################
4 # This program is free software: you can redistribute it and/or modify  #
5 # it under the terms of the version 3 of the GNU General Public License #
6 # as published by the Free Software Foundation.                         #
7 #                                                                       #
8 # This program is distributed in the hope that it will be useful, but   #
9 # WITHOUT ANY WARRANTY; without even the implied warranty of            #
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      #
11 # General Public License for more details.                              #
12 #                                                                       #
13 # You should have received a copy of the GNU General Public License     #
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.  #
15 #                                                                       #
16 # Written by and Copyright (C) Francois Fleuret                         #
17 # Contact <francois@fleuret.org> for comments & bug reports             #
18 #########################################################################
19
20 set -e
21
22 function getgeometry () {
23     XSETUP="/tmp/xsetup"
24
25     if [[ -f "${XSETUP}" ]]; then
26
27         ACTIVE_SCREEN_GEOMETRY=($(awk '{
28             if(0>= $5 && 0 < $5+$3) { print $2" "$3" "$4" "$5 }
29                 }' < ${XSETUP}))
30
31         ACTIVE_WIDTH=${ACTIVE_SCREEN_GEOMETRY[0]}
32         ACTIVE_HEIGHT=${ACTIVE_SCREEN_GEOMETRY[1]}
33         ACTIVE_X=${ACTIVE_SCREEN_GEOMETRY[2]}
34         ACTIVE_Y=${ACTIVE_SCREEN_GEOMETRY[3]}
35
36         GEOMETRY="${ACTIVE_WIDTH}x${ACTIVE_HEIGHT}+${ACTIVE_X}+${ACTIVE_Y}"
37     fi
38 }
39
40 # This is my universal file viewer
41
42 # If no argument is given, we pick the most recent file
43
44 if [[ ! "$1" ]]; then
45     file=$(\ls -tQ | head -1 | sed -e s/\"//g)
46     echo "Most recent is ${file}"
47     $0 "${file}"
48     exit 0
49 fi
50
51 # If there were arguments, we loop through them
52
53 echo "Viewing $@"
54
55 while [[ "$1" ]]; do
56
57     file="$1"
58
59     # file=$(\ls -tQd $1 | head -1 | sed -e s/\"//g)
60
61     # if [[ ! -a "${file}" ]]; then
62         # echo "Can not find file $1"
63         # exit 1
64     # fi
65
66     type=$(file -L "${file}")
67
68     case ${type} in
69
70         *"FIG image text"*)
71             echo "${file}"
72             temp=$(mktemp /tmp/view.XXXXXX)
73             fig2dev -L pdf "${file}" ${temp}
74             # xpdf -g 800x600+8+8 ${temp}
75             getgeometry
76             xpdf -g ${GEOMETRY} ${temp}
77             rm ${temp}
78             ;;
79
80         *"image"*)
81             echo "${file}"
82             # feh -g 800x600 "${file}"
83             getgeometry
84             feh -g ${GEOMETRY} "${file}"
85             ;;
86
87         *"TeX DVI"*)
88             echo "${file}"
89             xdvi "${file}"
90             ;;
91
92         # *"FIG image"*)
93             # echo "${file}"
94             # xfig "${file}"
95             # ;;
96
97         *"PostScript"*)
98             echo "${file}"
99             gv "${file}"
100             ;;
101
102         *"PDF"*)
103             echo "${file}"
104             # xpdf -g 800x600+8+8 "${file}"
105             getgeometry
106             xpdf -g ${GEOMETRY} "${file}"
107             ;;
108
109         *"WAVE audio"*)
110             echo "${file}"
111             esdplay "${file}"
112             ;;
113
114         *"MP3"*|*"MPEG"*|*"movie"*|*"video"*|*"AVI"*|*"Microsoft ASF"*)
115             mplayer -quiet "${file}"
116             ;;
117
118         *" text"*)
119             HEIGHT=$(stty size | awk '{print $1}')
120             if [[ $(wc -l "${file}" | cut -f 1 -d' ') -gt $((HEIGHT-2)) ]]; then
121                 less "${file}"
122             else
123                 cat "${file}"
124             fi
125             ;;
126
127         *"tar archive"*)
128             tar ztf "${file}"
129             ;;
130
131         *"Microsoft Word Document"*)
132             antiword "${file}" | less
133             ;;
134
135         *"directory"*)
136             echo ""${file}":"; ls -lt "${file}"
137             ;;
138
139         *"gzip compressed"*)
140             temp=$(mktemp /tmp/view.XXXXXX)
141             echo "Decompressing to ${temp}"
142             zcat "${file}" > ${temp}
143             $0 ${temp}
144             echo "Removing ${temp}"
145             rm ${temp}
146             ;;
147
148         *)
149             echo "Unable to handle ${type}"
150             ;;
151
152     esac
153
154     shift
155
156 done