PageRenderTime 28ms CodeModel.GetById 17ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/oldtest/convert.sh

https://code.google.com/p/latex-makefile/
Shell | 149 lines | 116 code | 22 blank | 11 comment | 19 complexity | e846279f10e015a8fae61944db244274 MD5 | raw file
  1#!/bin/bash
  2
  3# strict mode
  4set -u
  5
  6# unmatching globs expand to the empty string
  7shopt -s nullglob
  8
  9function run {
 10    if [ "$JUSTPRINT" = "0" ]; then
 11	$*
 12    else
 13	echo $*
 14    fi
 15}
 16
 17function debug {
 18    [ "$DEBUG" != "0" ] && echo ". $@" >&2
 19}
 20
 21function info {
 22    echo "$@" >&2
 23}
 24
 25function warning {
 26    echo "WARNING: $@" >&2
 27}
 28
 29function error {
 30    echo "ERROR: $@" >&2
 31}
 32
 33function backup-file {
 34    local dfile=${1%.*}.backup.${1##*.}
 35    if [ -e $dfile ]; then
 36	warning "Backup file '$dfile' already exists.  Skipping."
 37    else
 38	cp $1 $dfile
 39    fi
 40    echo $dfile
 41}
 42
 43function convert-tex {
 44    local sfile=$1
 45    local dfile=$2
 46    if [ "$JUSTPRINT" = "1" ]; then
 47	echo "Converting tex file $sfile to $dfile"
 48    else
 49	sed \
 50	    -e 's/\.gpi\.eps/.eps/' \
 51	    -e 's/\.gz\.eps/.eps/' \
 52	    $sfile > $dfile
 53    fi
 54}
 55
 56function convert-graphics {
 57    for g in *.eps.gpi; do
 58	newg=${g//.eps.gpi/.gpi}
 59	info "Copying $g to $newg"
 60	run "${MV} $g $newg"
 61    done
 62}
 63
 64function get-graphics {
 65# We'll handle the .fig.gpi case separately, if ever.  I was only using it in
 66# one place, and it never really worked very well, so I doubt anyone else is
 67# using it.
 68    echo *.{eps.gpi,eps.gz}
 69}
 70
 71#------------------------------------------------------------------------------
 72# Main code
 73#------------------------------------------------------------------------------
 74
 75# Get command-line parameters
 76declare -a workdirs
 77
 78# Move command (for moving graphics files)
 79MV=mv
 80DEBUG=0
 81JUSTPRINT=0
 82
 83for parm in "$@"; do
 84    arg=${parm#*=}
 85    case $parm in
 86	--justprint)
 87	    JUSTPRINT=1
 88	;;
 89
 90	--debug=*)
 91	    if (( arg > 0 )); then
 92		DEBUG=1
 93	    fi
 94	    if (( arg > 1 )); then
 95		set -x
 96	    fi
 97	;;
 98
 99	--mv=*)
100	    cmdloc=`which $arg`
101	    if [ "$cmdloc" != "" -a -e "$cmdloc" ]; then
102		MV=$arg
103	    else
104		warning "Ignoring invalid move command '$arg'"
105	    fi
106	;;
107
108	*)  
109	    if [ -d $parm ]; then
110		workdirs[${#workdirs}]=$parm
111	    else
112		warning "Ignoring non-directory '$parm'"
113	    fi
114	;;
115    esac
116done
117
118if [ ${#workdirs} = '0' ]; then
119    error "No directories specified.  Exiting"
120    exit -1
121fi
122
123for workdir in ${workdirs[@]}; do
124    debug "Changing to directory '$workdir'"
125    pushd $workdir >/dev/null
126    graphics=`get-graphics`
127    debug "Found the following graphics files in '$workdir'"
128    for g in $graphics; do
129	debug $g;
130    done
131
132    if [ "$graphics" != "" ]; then
133	    convert-graphics
134    else
135	warning "No graphics to convert in '$workdir'"
136    fi
137    info "Converting .tex files"
138    for texfile in *.tex; do
139	if [ "$texfile" = "${texfile/.backup//}" ]; then
140	    debug "Converting '$texfile'"
141	    backupfile=`backup-file $texfile`
142	    convert-tex $backupfile $texfile "$graphics"
143	else
144	    debug "Ignoring backup '$texfile'"
145	fi
146    done
147    popd >/dev/null
148done
149