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