| 1 |
#!/bin/bash |
| 2 |
# $Id: fimgs 1921 2023-03-25 14:49:04Z dezperado $ |
| 3 |
|
| 4 |
# (c) 2007-2023 Michele Martone |
| 5 |
|
| 6 |
# This is a modified version of the original fbgs script by Gerd Hoffman. |
| 7 |
# Note: it probably won't work on bash version < 3.0. |
| 8 |
|
| 9 |
# This script uses a temporary directory to stores rendered/wget'ed files |
| 10 |
# This directory is named after the process ID |
| 11 |
|
| 12 |
# Possible improvements: |
| 13 |
# - regexp usage |
| 14 |
# - adapt to work withour rar or unzip |
| 15 |
# - give option to wget |
| 16 |
# - may add a "-o" option to specify $FIMTDPFX |
| 17 |
|
| 18 |
FIMSHMEM=/dev/shm |
| 19 |
FIMVARTD=/var/tmp |
| 20 |
if test -d ${FIMSHMEM} -a -w ${FIMSHMEM} ; then |
| 21 |
FIMTMPDIR=$FIMSHMEM |
| 22 |
else |
| 23 |
FIMTMPDIR=$FIMVARTD |
| 24 |
fi |
| 25 |
FIMTDPFX=fbps |
| 26 |
DIR="${TMPDIR-$FIMTMPDIR}/${FIMTDPFX}-$$" |
| 27 |
#GSRESOLUTION="" |
| 28 |
#GSRESOLUTION=${GSRESOLUTION:--r240x240} |
| 29 |
GSRESOLUTION=${GSRESOLUTION:-120x120} |
| 30 |
shopt -s nocasematch |
| 31 |
|
| 32 |
|
| 33 |
# we must have write permissions in that directory |
| 34 |
mkdir -p "$DIR" || exit 1 |
| 35 |
|
| 36 |
# we want that directory clean after this script execution, no matter what happens |
| 37 |
trap 'rm -rf $DIR' EXIT |
| 38 |
|
| 39 |
# useless for now :) |
| 40 |
# declare -f |
| 41 |
|
| 42 |
# useless, too |
| 43 |
# export -f |
| 44 |
|
| 45 |
# as advised in man which, for mysterious reasons ( C compatible ) |
| 46 |
# which () |
| 47 |
# { |
| 48 |
# ( alias; declare -f ; ) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot $@ |
| 49 |
# } |
| 50 |
|
| 51 |
#export -f which |
| 52 |
FIMERRCV=255 |
| 53 |
|
| 54 |
# checks if the first supplied argument is in the current $PATH |
| 55 |
function check_in_path() |
| 56 |
{ |
| 57 |
local cmd="$1"; |
| 58 |
[[ -z "$cmd" ]] && return 0 |
| 59 |
[[ -z $(which "$cmd" 2>/dev/null) ]] && return ${FIMERRCV} |
| 60 |
return 0 |
| 61 |
} |
| 62 |
|
| 63 |
# output functions |
| 64 |
function info() { echo ' [+]' "$@" 1>&2 ; } |
| 65 |
function einfo() { echo ' [-]' "$@" 1>&2 ; } |
| 66 |
function die() { einfo "ERROR : $*" 1>&2 ; exit ${FIMERRCV}; } |
| 67 |
|
| 68 |
# our favourite framebuffer viewing program :) |
| 69 |
FBI=fim |
| 70 |
|
| 71 |
check_in_path sort || die "no sort in \$PATH" |
| 72 |
check_in_path echo || die "no echo in \$PATH" |
| 73 |
check_in_path find || die "no find in \$PATH" |
| 74 |
#check_in_path wget || die "no wget in \$PATH" |
| 75 |
check_in_path gs || die "no gs (ghostscript) in \$PATH" |
| 76 |
#check_in_path xz || die "no xz in \$PATH" |
| 77 |
#check_in_path bunzip2 || die "no bunzip2 in \$PATH" |
| 78 |
check_in_path tar || die "no tar in \$PATH" |
| 79 |
check_in_path basename || die "no basename in \$PATH" |
| 80 |
check_in_path md5sum || die "no md5sum in \$PATH" |
| 81 |
#check_in_path rar || die "no rar in \$PATH" |
| 82 |
#check_in_path unzip || die "no unzip in \$PATH" |
| 83 |
#check_in_path scp || die "no scp in \$PATH" |
| 84 |
|
| 85 |
|
| 86 |
# parse options |
| 87 |
fbi_read_opts="--autotop --autowidth" |
| 88 |
fbi_default_opts="--autozoom" |
| 89 |
fbiopts="$fbi_default_opts" |
| 90 |
gsopts="-q" |
| 91 |
#passwd="" |
| 92 |
opt=1 |
| 93 |
|
| 94 |
# in this way odd filenames are kept intact, be WARNED |
| 95 |
while test "$opt" = "1" |
| 96 |
#set -- `getopt "" "$@"` |
| 97 |
#while [ ! -z "$1" ] |
| 98 |
do |
| 99 |
case "$1" in |
| 100 |
# -l) gsopts="$gsopts -r100x100" |
| 101 |
# shift |
| 102 |
# ;; |
| 103 |
# -xl) gsopts="$gsopts -r120x120" |
| 104 |
# shift |
| 105 |
# ;; |
| 106 |
# -xxl) gsopts="$gsopts -r150x150" |
| 107 |
# shift |
| 108 |
# ;; |
| 109 |
# -q | -a) |
| 110 |
# fbiopts="$fbiopts $1" |
| 111 |
# shift |
| 112 |
# ;; |
| 113 |
# -d | -m | -t | -g | -f) |
| 114 |
# fbiopts="$fbiopts $1 $2" |
| 115 |
# shift; shift |
| 116 |
# ;; |
| 117 |
# explicit option passing |
| 118 |
# -t) fbiopts="$fbiopts --no-framebuffer" |
| 119 |
# shift |
| 120 |
# ;; |
| 121 |
-r) GSRESOLUTION="$2"; |
| 122 |
shift ; shift |
| 123 |
;; |
| 124 |
# -c) fbiopts="$fbiopts --execute-commands $2" |
| 125 |
# shift; shift |
| 126 |
# ;; |
| 127 |
# -F) fbiopts="$fbiopts --final-commands $2" |
| 128 |
# shift; shift |
| 129 |
# ;; |
| 130 |
-p) GSPASSWORD="$2" |
| 131 |
shift; shift |
| 132 |
;; |
| 133 |
-m) shift |
| 134 |
cat << EOF |
| 135 |
.TH fimgs 1 "(c) 2007-2023 Michele Martone" |
| 136 |
.SH NAME |
| 137 |
fimgs - poor man's [http://]PostScript/pdf/dvi/cbr/rar/cbz/zip viewer based on fim |
| 138 |
.SH SYNOPSIS |
| 139 |
.B fimgs [ {fimgs-options} ] file [-- [{fim-options}]] |
| 140 |
.SH DESCRIPTION |
| 141 |
.B fimgs |
| 142 |
is a wrapper script which takes a PostScript or pdf or .cbr or .rar or .cbz or .zip or .dvi or any of the above prefixed with http:// or https:// or ssh: as input, renders the pages using ghostscript into a temporary directory and finally calls \fB $FBI\fP to display them. |
| 143 |
|
| 144 |
In case of compressed archives (in ZIP or RAR formats), the images are decompressed into a directory and displayed using \fB fim\fP. |
| 145 |
In this case, only images contained in the archive will be displayed (no nested archives or pdf's or ps's or dvi's). |
| 146 |
|
| 147 |
The temporary directory name will be of the form \$TMPDIR/$FIMTDPFX-\$\$. |
| 148 |
If the \$TMPDIR environment variable is unset, $FIMSHMEM and $FIMVARTD will be checked for existence and permissions. |
| 149 |
The \$\$ above is the script process ID. |
| 150 |
The script deletes the temporary directory when \fB fim\fP terminates. |
| 151 |
|
| 152 |
In order to uncompress RAR archives, fimgs will use 'unrar-nonfree' or 'rar' or 'unrar-free'. |
| 153 |
|
| 154 |
In order to uncompress ZIP archives, fimgs will use 'zip'. |
| 155 |
|
| 156 |
In order to uncompress BZ2 files, 'bunzip2' will be used, if present. |
| 157 |
|
| 158 |
In order to uncompress TAR.GZ or TAR.BZ2 or TAR.XZ archives, fimgs will use 'tar'. |
| 159 |
|
| 160 |
In order to fetch http:// or https:// prefixed URLS, 'wget' will be used, if present. |
| 161 |
|
| 162 |
In order to fetch ssh: prefixed host:path locations, 'scp' will be used. |
| 163 |
|
| 164 |
In case of a CBZ, CBR, PDF, PS, or DVI file, will invoke \fB fim\fP with '$fbi_read_opts'. |
| 165 |
|
| 166 |
.SH OPTIONS |
| 167 |
To pass through options to \fB fim\fP, you may specify them after "--". |
| 168 |
|
| 169 |
Default options are '$fbi_default_opts'. |
| 170 |
|
| 171 |
.B -r {resolution} |
| 172 |
Specify resolution for the 'gs' -r option (e.g.: 96x96; default $GSRESOLUTION). |
| 173 |
.TP |
| 174 |
|
| 175 |
.B -m |
| 176 |
Dump a man page for fimgs. |
| 177 |
.TP |
| 178 |
|
| 179 |
.B -p {password} |
| 180 |
Specify password for the 'gs' -p (password) option. |
| 181 |
.TP |
| 182 |
|
| 183 |
.B -h |
| 184 |
Will display a help message. |
| 185 |
.\" Additionally you can specify -l, -xl or -xxl to get the pages |
| 186 |
.\" rendered with 100, 120 or 150 dpi (default is 75). |
| 187 |
.SH SEE ALSO |
| 188 |
fim(1), fimrc(1), gs(1), fbi(1), fbgs(1), bash(1) |
| 189 |
,zip(1), rar(1), rar-free(1), unrar-free(1), tar(1), gzip(1) |
| 190 |
.SH AUTHOR |
| 191 |
Michele Martone <dezperado _ GUESS _ autistici.org>. |
| 192 |
EOF |
| 193 |
exit; |
| 194 |
;; |
| 195 |
-h) |
| 196 |
#help text |
| 197 |
echo "$0 [{fimgs-options}] {files} [-- [fim-options]] " |
| 198 |
echo -e "\nNote that any file in {files} may be a (appropriately named) :" |
| 199 |
echo -e " PDF (.pdf) file" |
| 200 |
echo -e " Postscript (.ps) file" |
| 201 |
echo -e " Encapsulated Postscript (.eps) file" |
| 202 |
echo -e " DeVice Independent (.dvi) file" |
| 203 |
echo -e " TAR (.tar) archive" |
| 204 |
echo -e " BZ2 (.bz2) archive" |
| 205 |
echo -e " XZ (.xz) archive" |
| 206 |
echo -e " GZIP (.gz) archive" |
| 207 |
echo -e " https:// resource (web resource)" |
| 208 |
echo -e " http:// resource (web resource)" |
| 209 |
echo -e "\nNote that you cannot view files named like options (no -- meta-option apply to fimgs)." |
| 210 |
echo -e "\n \`man fimgs\` will give you more info." |
| 211 |
exit 1 |
| 212 |
;; |
| 213 |
--) |
| 214 |
break;; # -- is *not* to be passed to fim |
| 215 |
*) |
| 216 |
[[ "$1" =~ ^-.* ]] && fbiopts="$fbiopts $1"; |
| 217 |
break;; |
| 218 |
# -*) echo "unknown option: $1" |
| 219 |
# exit 1 |
| 220 |
# ;; |
| 221 |
# *) opt=0 |
| 222 |
# ;; |
| 223 |
esac |
| 224 |
# shift |
| 225 |
done |
| 226 |
############################################################################### |
| 227 |
check_in_path $FBI || die "no fim in \$PATH" |
| 228 |
############################################################################### |
| 229 |
## HANDLING OF CBR AND CBZ ARCHIVES |
| 230 |
############################################################################### |
| 231 |
UNCBZ="unzip" ; |
| 232 |
# shellcheck disable=SC2089 |
| 233 |
ZOPT="-x '*/*'" # suits for Info-ZIP implementation |
| 234 |
#UNRAR="unrar-free" |
| 235 |
#UNRAR="rar" |
| 236 |
UNRAR="unrar-nonfree" |
| 237 |
check_in_path $UNRAR || UNRAR="rar" |
| 238 |
check_in_path $UNRAR || UNRAR="unrar-free" |
| 239 |
#check_in_path $UNRAR || UNRAR="unrar-nonfree" |
| 240 |
UNCBR="$UNRAR x" # suits for Alexander Roshal's RAR 3.51 |
| 241 |
UNTAR="tar xf" |
| 242 |
UNTGZ="tar xzf" |
| 243 |
UNBZ="tar xjf" |
| 244 |
UNXZ="tar xJf" |
| 245 |
#while [[ "$1" != "" ]] |
| 246 |
#while [[ "$#" -gt "0" ]] |
| 247 |
while test x"$opt" = x"1" |
| 248 |
#while shift |
| 249 |
do |
| 250 |
f="$1"; |
| 251 |
if test x"$f" = x"--" ; then shift; while test "$#" -gt "0" ; do f="$1"; fbiopts="$fbiopts $1" ; shift; done; f=/dev/null ; fi |
| 252 |
shift; |
| 253 |
[[ "$f" =~ 'https://' ]] && check_in_path wget && { fn="$DIR"/"`basename $f`" && wget "$f" -O "$fn"; f="$fn"; } |
| 254 |
[[ "$f" =~ 'http://' ]] && check_in_path wget && { fn="$DIR"/"`basename $f`" && wget "$f" -O "$fn"; f="$fn"; } |
| 255 |
[[ "$f" =~ 'ssh:' ]] && { bfn="`basename ${f/#ssh:/}`" && fn="$DIR"/"`echo $bfn | sed s/^.*://`" && scp "${f/#ssh:/}" "$fn" ; f="$fn"; } |
| 256 |
[[ -z "$f" ]] && break ; |
| 257 |
if ! [[ -e "$f" ]] ; then exit 255; fi |
| 258 |
#while [[ ! -f "$f" ]] ; do shift ; [[ "$#" -lt 0 ]] && break 2 ; done |
| 259 |
############################################################################### |
| 260 |
## HANDLING OF DVI,PS AND PDF FILES |
| 261 |
############################################################################### |
| 262 |
#[[ "$f" =~ .dvi$ ]] && f="/tmp/$$.ps" && dvips -q "$f" -o "$f" ; rm -fR $DIR" EXIT |
| 263 |
#[[ -f "$f" ]] || { echo "an option!" ; fbiopts="$fbiopts $f" ; break ; } # not a file, but an option |
| 264 |
|
| 265 |
|
| 266 |
[[ "$f" =~ .dvi$ ]] && fbiopts=" -P $fbiopts" |
| 267 |
[[ "$f" =~ .pdf$ ]] && fbiopts=" -P $fbiopts" |
| 268 |
[[ "$f" =~ .ps$ ]] && fbiopts=" -P $fbiopts" |
| 269 |
[[ "$f" =~ .eps$ ]] && fbiopts=" -P $fbiopts" |
| 270 |
|
| 271 |
if ( [[ "$f" =~ .ps$ ]] || [[ "$f" =~ .pdf$ ]] ) || ( [[ "$f" =~ .eps$ ]] || [[ "$f" =~ .dvi$ ]] ) |
| 272 |
then |
| 273 |
|
| 274 |
BDIR='' |
| 275 |
if check_in_path md5sum |
| 276 |
then |
| 277 |
DIRS=$(echo "$f" | md5sum ) |
| 278 |
DIRS=${DIRS// /} |
| 279 |
DIRS=${DIRS//-/} |
| 280 |
BDIR="$DIR/$DIRS" |
| 281 |
# this means that giving duplicate input files will overwrite the original render directory... |
| 282 |
else |
| 283 |
BDIR="$DIR/"$(basename "$f") |
| 284 |
fi |
| 285 |
mkdir -p $BDIR || die "failed mkdir $BDIR" |
| 286 |
info "rendering $f to $BDIR.." |
| 287 |
|
| 288 |
#this is a workaround for gs's .. bug .. |
| 289 |
|
| 290 |
[[ "$f" =~ ^\.\. ]] && f="$PWD/$f" |
| 291 |
|
| 292 |
# note : we cannot render gs renderable documents with more than 1000 pages... |
| 293 |
gs -dSAFER -dNOPAUSE -dBATCH \ |
| 294 |
-sPDFPassword="$GSPASSWORD" \ |
| 295 |
-sDEVICE=png256 -r${GSRESOLUTION} -sOutputFile=$BDIR/ps%03d.png \ |
| 296 |
$gsopts \ |
| 297 |
"$f" || die "ghostscript failed rendering!" |
| 298 |
# still unused options : |
| 299 |
# -sDEVICE=tiffpack \ |
| 300 |
# -sOutputFile=$DIR/ps%03d.tiff |
| 301 |
|
| 302 |
# tell the user we are done :-) |
| 303 |
#echo -ne "\\007" |
| 304 |
#echo stuff : `ls -l $DIR` |
| 305 |
#echo "contents of $DIR:" |
| 306 |
#$FBI $fbiopts -P $DIR/ps*.png |
| 307 |
#$FBI $fbiopts -P $DIR/ps*.png 2>/dev/null |
| 308 |
#fbi $fbiopts -P $DIR/ps*.tiff |
| 309 |
# sanity check |
| 310 |
#pages=`ls $DIR/ 2>/dev/null | wc -l` |
| 311 |
#if test "$pages" -eq "0"; then |
| 312 |
# echo |
| 313 |
# echo "Oops: ghostscript wrote no pages ($pages)?" |
| 314 |
# echo |
| 315 |
# exit 1 |
| 316 |
#elif ( ( [[ "$f" =~ \\.cbr$ ]] || [[ "$f" =~ \\.cbz$ ]] ) || ( [[ "$f" =~ \\.rar$ ]] || [[ "$f" =~ \\.zip$ ]] ) || ( [[ "$f" =~ \\.tgz$ ]] || [[ "$f" =~ \\.tar.gz$ ]] ) || ( [[ "$f" =~ \\.tar$ ]] || [[ "$f" =~ \\.tar.bz2$ ]] ) ) && ! [[ "$f" =~ '^http://' ]] |
| 317 |
elif ( ( [[ "$f" =~ .CBR$ ]] || [[ "$f" =~ .CBZ$ ]] || [[ "$f" =~ .cbr$ ]] || [[ "$f" =~ .cbz$ ]] ) || ( [[ "$f" =~ .rar$ ]] || [[ "$f" =~ .zip$ ]] ) || ( [[ "$f" =~ .tgz$ ]] || [[ "$f" =~ .tar.gz$ ]] ) || ( [[ "$f" =~ .tar$ ]] || [[ "$f" =~ .tar.bz2$ ]] || [[ "$f" =~ .tar.xz$ ]] ) ) && ! [[ "$f" =~ 'http://' ]] && ! [[ "$f" =~ 'https://' ]] |
| 318 |
then |
| 319 |
# in the case of an archive ... |
| 320 |
# an ideal fimgs script would unpack recursively in search for interesting files.. |
| 321 |
|
| 322 |
|
| 323 |
# 20070925 there were problems with '\\.' so i put '.' only :/ |
| 324 |
[[ "$f" =~ .cbr$ ]] && ( $UNCBR "$f" "$DIR" ) |
| 325 |
#[[ "$f" =~ .CBR$ ]] && ( $UNCBR "$f" "$DIR" ) |
| 326 |
[[ "$f" =~ .rar$ ]] && ( $UNCBR "$f" "$DIR" ) |
| 327 |
# shellcheck disable=SC2086,SC2090 |
| 328 |
[[ "$f" =~ .cbz$ ]] && ( $UNCBZ "$f" $ZOPT -d "$DIR" ) |
| 329 |
# shellcheck disable=SC2086,SC2090 |
| 330 |
#[[ "$f" =~ .CBZ$ ]] && ( $UNCBZ "$f" $ZOPT -d "$DIR" ) |
| 331 |
[[ "$f" =~ .zip$ ]] && ( $UNCBZ "$f" $ZOPT -d "$DIR" ) |
| 332 |
[[ "$f" =~ .tgz$ ]] && ( $UNTGZ "$f" -C "$DIR" ) |
| 333 |
[[ "$f" =~ .tar.gz$ ]] && ( $UNTGZ "$f" -C "$DIR" ) |
| 334 |
[[ "$f" =~ .tar$ ]] && ( $UNTAR "$f" -C "$DIR" ) |
| 335 |
check_in_path bunzip2 && [[ "$f" =~ .tar.bz2$ ]] && ( $UNBZ "$f" -C "$DIR" ) |
| 336 |
check_in_path xz && [[ "$f" =~ .tar.xz$ ]] && ( $UNXZ "$f" -C "$DIR" ) |
| 337 |
|
| 338 |
function add_fbi_read_opts() { fbiopts="$fbiopts $fbi_read_opts"; } |
| 339 |
[[ "$f" =~ .cbr$ ]] && add_fbi_read_opts; |
| 340 |
[[ "$f" =~ .cbz$ ]] && add_fbi_read_opts; |
| 341 |
[[ "$f" =~ .ps$ ]] && add_fbi_read_opts; |
| 342 |
[[ "$f" =~ .dvi$ ]] && add_fbi_read_opts; |
| 343 |
[[ "$f" =~ .pdf$ ]] && add_fbi_read_opts; |
| 344 |
|
| 345 |
# ... but this fimgs script is lazy and it gets satisfied with a bare decompress ! |
| 346 |
|
| 347 |
# lone file handling.. |
| 348 |
#[[ "$?" = 0 ]] || echo "this script is not suited for file $f" |
| 349 |
else |
| 350 |
[[ -f "$DIR/`basename $f`" ]] || cp -- "$f" "$DIR" || die "problems copying $f to $DIR" |
| 351 |
fi |
| 352 |
#fi |
| 353 |
done |
| 354 |
# show pages |
| 355 |
#fbiopts= |
| 356 |
#$FBI $fbiopts -P -- $DIR/* $DIR/*/* $DIR/*/*/* $DIR/*/*/*/* $DIR/*/*/*/*/* $DIR/*/*/*/*/*/* $DIR/*/*/*/*/*/*/* |
| 357 |
#echo "options are $fbiopts" |
| 358 |
|
| 359 |
# there should be some filter to avoid feed garbage to fim ... |
| 360 |
# shellcheck disable=SC2086 |
| 361 |
find "$DIR/" -type f -iname '*.bmp' -or -iname '*.gif' -or -iname '*.jpg' -or -iname '*.jpeg' -or -iname '*.pcx' -or -iname '*.pbm' -or -iname '*.pgm' -or -iname '*.ppm' -or -iname '*.png' -or -iname '*.tif' -or -iname '*.tiff' -or -iname '*.webp' | sort -n | $FBI $fbiopts -- - |
| 362 |
|
| 363 |
# when no framebuffer device is available, we could invoke another console viewer, couldn't we ? |
| 364 |
# cacaview $DIR/* |
| 365 |
|
| 366 |
# the old syntax |
| 367 |
# $FBI $fbiopts -P `find $DIR -iname '*.png' -or -iname '*.jpg' -or -iname '*.gif' -or -iname '*.jpeg' -or -iname '*.tiff' -or -iname '*.bmp'` |
| 368 |
|
| 369 |
# or the older one .. |
| 370 |
#$FBI $fbiopts -P -- $files |
| 371 |
|
| 372 |
# or the ancient one |
| 373 |
#fbi $fbiopts -P $DIR/ps*.tiff |
| 374 |
|
| 375 |
|