#!/bin/csh -e

# Plot various percentiles for several runs 

# Arguments:  directory name title file-prefix file-suffix  { tag file-middle }+

set nonomatch
rm -f XX*.res

set dir = $argv[1]
shift
set name = $argv[1]
shift
set title = "$argv[1]"
shift
set range = "$argv[1]"
shift
set fileprefix = "$argv[1]"
shift
set filesuffix = "$argv[1]"
shift

set files
set tags
set n = 1
while ( ${#argv} != 0 )
  set tags = ( $tags $argv[1] )
  set files = ( $files $argv[2] )
  set file = ( ${dir}/"${fileprefix}${argv[2]}${filesuffix}"-*.res )
  if ( ${#file}>0 && -e "${file[1]}" ) then
    ./extract "${file[1]:r}".trace > "${file[1]:r}".res2
    ln -s "${file[1]:r}".res2 XX$n.res
  endif
  shift
  shift
  @ n = $n + 1
end

echo set terminal postscript color dashed 14 		> /tmp/foo
echo set data style linespoints 			>> /tmp/foo
echo 'set xlabel "L/N"'		 			>> /tmp/foo
echo 'set xrange [1:'${range}']' 			>> /tmp/foo

echo 'set ylabel "Fraction Sat/UnSat"'	 		>> /tmp/foo
echo "set title "'"'"${title}  sat/unsat" '"' 		>> /tmp/foo
echo -n "plot "						>> /tmp/foo

set n = 1
set sep = ''
while ( ${#files} >= $n )
  if ( -e XX${n}.res ) then 
    echo -n ${sep} '"'"XX${n}.res"'"'" using 1:2 title 'N=${tags[$n]} Sat' with linespoints ${n} 1" >> /tmp/foo
    echo -n ',"'"XX${n}.res"'"'" using 1:3 title 'N=${tags[$n]} UnS' with linespoints ${n} 2" >> /tmp/foo
    set sep = ', '
  endif
  @ n = $n + 1
end
echo ''							>> /tmp/foo

echo 'set ylabel "Fraction Trivial Sat/Trivial UnSat"'	 		>> /tmp/foo
echo "set title "'"'"${title}  trivial sat/trivial unsat" '"' 		>> /tmp/foo
echo 'set yrange[0:1]'					>> /tmp/foo
echo -n "plot "						>> /tmp/foo

set n = 1
set sep = ''
while ( ${#files} >= $n )
  if ( -e XX${n}.res ) then 
    echo -n ${sep} '"'"XX${n}.res"'"'" using 1:16 title 'N=${tags[$n]} Triv Sat' with linespoints ${n} 1" >> /tmp/foo
    echo -n ',"'"XX${n}.res"'"'" using 1:17 title 'N=${tags[$n]} Triv UnS' with linespoints ${n} 2" >> /tmp/foo
    set sep = ', '
  endif
  @ n = $n + 1
end
echo ''							>> /tmp/foo



echo set logscale y 					>> /tmp/foo
echo 'set yrange[0.009:1000]'				>> /tmp/foo
echo 'set ylabel "CPU time - Sat only (s)"'		>> /tmp/foo

foreach plot ( "( median 10 )" "( 60% 11 )" "( 70% 12 )" "( 80% 13 )" "( 90% 14 )" "( worst 15 )" )
    set plt = $plot

    echo "set title "'"'"${title}  ${plt[1]}" '"'	>> /tmp/foo
    echo -n "plot "					>> /tmp/foo
    set n = 1
    set sep = ''
    while ( ${#files} >= $n )
      if ( -e XX${n}.res ) then 
        echo -n ${sep} '"'"XX${n}.res"'"'" using 1:${plt[2]} title 'N=${tags[$n]}' with linespoints ${n} 1" >> /tmp/foo
        set sep = ', '
      endif
      @ n = $n + 1
    end
    echo ''						>> /tmp/foo
end

gnuplot < /tmp/foo > plot-${name}.ps


echo set terminal postscript eps dashed 22				> /tmp/foo
echo set data style linespoints 			>> /tmp/foo
echo 'set xlabel "L/N"'		 			>> /tmp/foo
echo 'set xrange [1:'${range}']' 			>> /tmp/foo

echo 'set ylabel "Fraction Sat/UnSat"'	 		>> /tmp/foo
echo -n "plot "						>> /tmp/foo

set n = 1
set sep = ''
while ( ${#files} >= $n )
  if ( -e XX${n}.res ) then 
    echo -n ${sep} '"'"XX${n}.res"'"'" using 1:2 title 'N=${tags[$n]} Sat' with linespoints ${n} 1" >> /tmp/foo
    echo -n ',"'"XX${n}.res"'"'" using 1:3 title 'N=${tags[$n]} UnS' with linespoints ${n} 2" >> /tmp/foo
    set sep = ', '
  endif
  @ n = $n + 1
end
echo ''							>> /tmp/foo

gnuplot < /tmp/foo > plot-${name}-sat.ps

echo set terminal postscript eps dashed 22				> /tmp/foo
echo set data style linespoints 			>> /tmp/foo
echo 'set xlabel "L/N"'		 			>> /tmp/foo
echo 'set xrange [1:'${range}']' 			>> /tmp/foo

echo 'set ylabel "Fraction Trivial Sat/Trivial UnSat"'	 		>> /tmp/foo
echo 'set yrange[0:1]'					>> /tmp/foo
echo -n "plot "						>> /tmp/foo

set n = 1
set sep = ''
while ( ${#files} >= $n )
  if ( -e XX${n}.res ) then 
    echo -n ${sep} '"'"XX${n}.res"'"'" using 1:16 title 'N=${tags[$n]} Triv Sat' with linespoints ${n} 1" >> /tmp/foo
    echo -n ',"'"XX${n}.res"'"'" using 1:17 title 'N=${tags[$n]} Triv UnS' with linespoints ${n} 2" >> /tmp/foo
    set sep = ', '
  endif
  @ n = $n + 1
end
echo ''							>> /tmp/foo

gnuplot < /tmp/foo > plot-${name}-triv.ps


foreach plot ( "( median 10 median )" "( 60% 11 60 )" "( 70% 12 70 )" "( 80% 13 80 )" "( 90% 14 90 )" "( worst 15 worst )" )
    set plt = $plot

echo set terminal postscript eps dashed 22				> /tmp/foo
echo set data style linespoints 			>> /tmp/foo
echo 'set xlabel "L/N"'		 			>> /tmp/foo
echo 'set xrange [1:'${range}']' 			>> /tmp/foo

echo set logscale y 					>> /tmp/foo
echo 'set yrange[0.009:1000]'				>> /tmp/foo
echo 'set ylabel "CPU time (s)"'		>> /tmp/foo

    echo -n "plot "					>> /tmp/foo
    set n = 1
    set sep = ''
    while ( ${#files} >= $n )
      if ( -e XX${n}.res ) then 
        echo -n ${sep} '"'"XX${n}.res"'"'" using 1:${plt[2]} title 'N=${tags[$n]}' with linespoints ${n} 1" >> /tmp/foo
        set sep = ', '
      endif
      @ n = $n + 1
    end
    echo ''						>> /tmp/foo

    gnuplot < /tmp/foo > plot-${name}-${plt[3]}.ps

end

rm -f XX*.res
