#! /bin/bash
# Last edited on 2012-05-06 02:44:51 by stolfilocal

usage="$0 [ -freqs | -counts ] [-noylabels] [-xmax NUM] [-ymax NUM] [-binom N S] AFILE ATITLE ASCALE ALT APT  BFILE BTITLE BSCALE BLT BPT ... OUTNAME" 

# Input files must have records of the form 
#   LENGTH COUNT FREQ ...
# Uses the "FREQ" column to plot.
#
# If "-binom N" is specified, plost also a binomial distribution for
# N even flips, shifted by S.

ylabfmt="%g"
relative=1
ymax="0.30"
xmax="20"
format="eps"
binN="0"
binS="0"
showbinom=0
color=1
show=1

while [[ ( $# -gt 0 ) && ( "/$1" =~ /-.* )  ]]; do
  if [[  ( $# -ge 1 ) && ( "/$1" == "/-noylabels" )  ]]; then
    ylabfmt="     "; shift
  elif [[  ( $# -ge 1 ) && ( "/$1" == "/-freqs" )  ]]; then
    ymax="0.30"; relative=1; shift
  elif [[  ( $# -ge 1 ) && ( "/$1" == "/-counts" )  ]]; then
    ymax=""; relative=0; shift
  elif [[  ( $# -ge 3 ) && ( "/$1" == "/-binom" )  ]]; then
    binN="$2"; binS="$3"; shift; shift; shift
  elif [[  ( $# -ge 2 ) && ( "/$1" == "/-ymax" )  ]]; then
    ymax="$2"; shift; shift;
  elif [[  ( $# -ge 2 ) && ( "/$1" == "/-xmax" )  ]]; then
    xmax="$2"; shift; shift;
  else 
    echo "bad option \"$1\"" 1>&2; 
    echo "usage: ${usage}" 1>&2; exit 1
  fi
done
if [[  $# -lt 6  ]]; then
  echo "usage: ${usage}" 1>&2; exit 1
fi

#Parse file/title pairs:
ifile=()
title=()
scale=()
ltx=()
ptx=()
i=0
while [[ $# -gt 1 ]]; do
  if [[ $# -lt 6 ]]; then
    echo "unpaired plot arg \"$1\" - usage: ${usage}" 1>&2; exit 1
  fi
  ifile[$i]="$1"; shift
  title[$i]="$1"; shift
  scale[$i]="$1"; shift
  ltx[$i]="$1"; shift
  ptx[$i]="$1"; shift
  echo "  ${ifile[$i]} \"${title[$i]}\" ${scale[$i]} ${ltx[$i]} ${ptx[$i]}" 1>&2
  if [[ ! -r ${ifile[$i]} ]]; then
    echo "** ${ifile[$i]} does not exist" 1>&2; exit 1
  fi
  i=$(( $i + 1 ))
done
nfiles=${i}
  
# Parse output filename prefix:
if [[ $# -ne 1  ]]; then
  echo "missing output prefix - usage: ${usage}" 1>&2; exit 1
fi
oname="$1"; shift;

tmp="/tmp/$$"

for format in png eps ; do

  pfile="${tmp}.gnuplot"
  tfile="${tmp}.${format}"
  ofile="${oname}.${format}"

  if [[ ${color} -ne 0 ]]; then
    linecolor=( ff1100 0055aa 5500ff aa3300 006655 8800aa )
    linespecs=(
      "set style line 1 lt 1 lc rgb '#" ${linecolor[0]} "';"
      "set style line 2 lt 1 lc rgb '#" ${linecolor[1]} "';"
      "set style line 3 lt 1 lc rgb '#" ${linecolor[2]} "';"
      "set style line 4 lt 1 lc rgb '#" ${linecolor[3]} "';"
      "set style line 5 lt 1 lc rgb '#" ${linecolor[4]} "';"
      "set style line 6 lt 1 lc rgb '#" ${linecolor[5]} "';"
    ) 
  else
    linecolor=( 000000 555555 000000 555555 000000 555555 )
    linespecs=(
      "set style line 1 lt 1 lc rgb '#" ${linecolor[0]} "';"
      "set style line 2 lt 2 lc rgb '#" ${linecolor[1]} "';"
      "set style line 3 lt 2 lc rgb '#" ${linecolor[2]} "';"
      "set style line 4 lt 3 lc rgb '#" ${linecolor[3]} "';"
      "set style line 5 lt 3 lc rgb '#" ${linecolor[4]} "';"
      "set style line 6 lt 4 lc rgb '#" ${linecolor[5]} "';"
    )
  fi

  if [[ "/${format}" == "/eps" ]]; then
    if [[ ${color} -ne 0 ]]; then
      colorspec="color solid"
      ptype=( 0 7 6 5 4 3 2 8 1 )
    else
      colorspec="mono"
      ptype=( 0 4 2 3 1 6 7 8 1 2 )
    fi
    size="1.20,0.60"
    pointsize="0.75"
    linewidth="2.50"
    termspec='postscript eps '"${colorspec}"' "TimesRoman" 24'
  elif [[  "/${format}" == "/png"  ]]; then
    if [[ ${color} -ne 0 ]]; then
      colorspec="truecolor"
      ptype=( 0 7 6 5 4 7 6 5 4 7 )
    else
      colorspec=""
      ptype=( 0 7 6 5 4 1 3 2 8 9 )
    fi
    size="1,1"
    pointsize="2.0"
    linewidth="2.0"
    termspec="png font 'arial,32' ${colorspec} rounded size 1800,600"
  else
    echo "invalid plot output format" 1>&2; exit 1
  fi

  cat > ${pfile} <<EOF
  # set term x11
  set term ${termspec}
  set output "${tfile}"
  set size ${size}
  ${linespecs[@]}
  set xrange [0:${xmax}]
  set yrange [0:${ymax}]
  set xtics 1
  set format y "${ylabfmt}"
  rel = ${relative}
  binN = ${binN}
  binS = ${binS}
EOF

  sep="plot"
  if [[ "/${binN}" != "/0" ]]; then
    printf '%s \\\n  (x<=-binS-1 ? 0 : (x >= binN-binS+1 ? 0 : gamma(binN+1)/gamma(x+binS+1)/gamma(binN-x-binS+1)/2**binN))' \
      "${sep}" >> ${pfile}
    printf ' \\\n    title "binom(%s,k%s)" with lines lt 0 lw (2*%s) lc rgb "#000000"' \
      "${binN}" "${binS}" "${linewidth}" >> ${pfile}
    sep=","
  fi
  i=0
  while [[ $i -lt ${nfiles} ]]; do
    printf '%s \\\n  "%s" using 1:((rel ? $3:$2)*(%s)) title "%s"' \
      "${sep}" "${ifile[$i]}" "${scale[$i]}" "${title[$i]}" >> ${pfile}
    printf ' \\\n     with linespoints ls %d lw %s pt %d ps %s' \
      "${ltx[$i]}" "${linewidth}" "${ptype[${ptx[$i]}]}" "${pointsize}" >> ${pfile}
    sep=","
    i=$(( $i + 1 ))
  done
  printf '\n' >> ${pfile}
  printf 'quit\n' >> ${pfile}

  xrdb -merge good-gnuplot-colors.xrm
  export GDFONTPATH=.
  gnuplot < ${pfile}

  if [[ "/${format}" == "/eps" ]]; then
    mv ${tfile} ${ofile}
    if [[ ${show} -ne 0 ]]; then
      ghostview ${ofile}
    fi
    /bin/rm -f ${pfile}
  elif [[ "/${format}" == "/png" ]]; then
    convert ${tfile} -resize '600x' ${ofile}
    if [[ ${show} -ne 0 ]]; then
      display ${ofile}
    fi
    /bin/rm -f ${tfile} ${pfile}
  else
    echo "invalid plot output format" 1>&2
  fi
done