#!/bin/bash
# plb2x - Translate a plt or plb file to some general format
outtype="scr"
format="scr to screen output"
term="x11"
del=true
expl=true
topdf=$PLB2PDF
 
while [ -n "$*" ]; do
  expl=false
  case $1 in
    -size)
      outs1=$2
      outs2=$3
      setoutsize=true
      shift
      shift;;
    -fontsize)
      fontsize=$2
      shift;;
    -scr)
      outtype=scr
      format="scr to screen output"
      term="x11";;
    -wxt)
      outtype=wxt
      format="wxt to screen output"
      term="wxt";;
    -emf)
      outtype=emf
      format="emf in monochrome emf format"
      term="emf monochrome"
      if [ -n "$outs1" ]; then
        term="$term size $outs1,$outs2"
        setoutsize=false
      fi;;
    -emfc)
      outtype=emf
      format="emf in color emf format"
      term="emf color"
      if [ -n "$outs1" ]; then
        term="$term size $outs1,$outs2"
        setoutsize=false
      fi;;
    -eps)
      outtype=eps
      format="eps in encapsulated postscript format"
      term="postscript eps monochrome";;
    -epsc)
      outtype=eps;
      format="eps in color encapsulated postscript format"
      term="postscript eps color";;
    -topdf)
      topdf=true;;
    -pdf)
      echo Warning: You might be better off using eps output and epstopdf conversion
      outtype=pdf;
      format="pdf in monochrome pdf format"
      term="pdf monochrome dashed"
      if [ -n "$outs1" ]; then
#        echo Before: outs1: $outs1, outs2: $outs2
        outs1=$(echo "$outs1*5" | bc)
        outs2=$(echo "$outs2*2" | bc)
        term="$term size $outs1,$outs2"
#        echo Found term: $term, outs1: $outs1, outs2: $outs2
        setoutsize=false
      fi;;
    -pdfc)
      echo Warning: You might be better off using eps output and epstopdf conversion
      outtype=pdf;
      format="pdf in colour pdf format"
      term="pdf"
      if [ -n "$outs1" ]; then
        outs1=$(echo "$outs1*5" | bc)
        outs2=$(echo "$outs2*2" | bc)
        term="$term size $outs1,$outs2"
        setoutsize=false
      fi;;
    -fig)
      outtype=fig
      format="fig in fig format"
      term="fig color"
      if [ -n "$outs1" ]; then
        term="$term size $outs1 $outs2"
        setoutsize=false
      fi;;
    -gif)
      outtype=gif
      format="gif in gif format"
      term="gif small"
      if [ -n "$outs1" ]; then
        term="$term size $outs1,$outs2"
        setoutsize=false
      fi;;
    -png)
      outtype=png;
      format="png in png format"
      term="pngcairo"
      if [ -n "$outs1" ]; then
        term="$term size $outs1 $outs2"
        setoutsize=false
      fi;;
    -tex)
      outtype=tex
      format="tex in PSLaTeX format"
      term="pslatex";;
    -etex)
      outtype=tex
      format="tex in EPSLaTeX format"
      term="epslatex";;
    -plb)
      ;; 
    -nodel)
      del=false;;   
    *)
#        echo Working on graph $1

      # Extract the filename and LAST extension
      full=$1
      ext="${full##*.}"
#      if [ "$ext" != "plb" -a "$ext" != "plt" ]; then
#        if [ -f $full.plt ]; then
#          full=$full.plt
#          ext=plt
#        fi
      # Add plb if necessary; don't add .plt as this might lead to
      # double display of dir/graph.eps.plt  
      if [ "$ext" != "plb" ]; then
        if [ -f $full.plb ]; then
          full=$full.plb
          ext=plb
        fi
      fi  
        
      short="${full%.*}"
      base=`basename $short`

# Find the size (does not work)
#      if [ "$ext" = "plb" ]; then
#         plbsize=`grep "#set size" $full | sed 's/#//'`
#      fi   
      
#      echo Full: $full, short: $short, ext: $ext, base: $base

      if [ -f $full ]; then 
        if [ "$ext" = "plb" -o "$ext" = "plt" ]; then
  #          echo Working on file $full
          echo "# Writing file $short.$format">$short.$outtype.tmp
          echo "reset">>$short.$outtype.tmp
          if [ "$term" = "x11"  -o "$term" = "wxt" ]; then
            echo "set terminal $term title 'Gnuplot: $base'">>$short.$outtype.tmp
  #            echo "set label '$short.plb' at screen 1,0.98 right">>$short.$outtype.tmp
          else  
            echo "set terminal $term">>$short.$outtype.tmp
            echo "set output '$short.$outtype'">>$short.$outtype.tmp
          fi            
          if [ -n "$fontsize" ]; then
            echo "set termoption font \"arial,$fontsize\"">>$short.$outtype.tmp
          fi  

          if [ "$setoutsize" = "true" ]; then
            echo "set size $outs1,$outs2">>$short.$outtype.tmp
#          else 
#            if [ "$plbsize" != "" ]; then
#              echo Setting size using 
#              echo   $plbsize
#              echo $plbsize >> $short.$outtype.tmp 
#            fi              
          fi

          cat $full | grep -v "set terminal" | grep -v "set term " | grep -v "set output" >>$short.$outtype.tmp
          if [ "$term" = "x11"  -o "$term" = "wxt" ]; then
            gnuplot -persist $short.$outtype.tmp
          else
            echo "set terminal unknown">>$short.$outtype.tmp
            echo "set output">>$short.$outtype.tmp

            # Remove existing pdf, if eps output is requested
            if [ "$outtype" = "eps" -a -f $short.pdf ]; then
              echo Removing old $short.pdf
              rm $short.pdf
            fi  

            echo "Writing $short.$outtype"
            gnuplot $short.$outtype.tmp

            if [ "$outtype" = "eps" -a "$topdf" = "true" ]; then
              echo "Writing $short.pdf"
              epstopdf $short.$outtype
            fi  
          fi
          if [ "$GNU2PLT" = "ON" ]; then
            cp $short.$outtype.tmp $short.$outtype.plt
          fi
          if [ "$del" = "true" ]; then
  # bash/csh syntax:        
  #          rm `basename $short`*.log >& /dev/null
            rm `basename $short`*.log >/dev/null 2>&1
            rm $short.$outtype.tmp
          fi  
  #        else
  #          echo File $short.plb not found  
        fi
      fi;;  
  esac    

  shift  
done

if [ "$expl" = "true" ]; then
  echo "plb2x [-size x y] [-fontsize z] [-type] <filename1> [<filename2>]"
  echo "Call this script with optional size information for the graph."
fi
