#!/bin/sh

#set x

S1=020; S2=040; S3=080
####################
PIC=U_vertical
#PIC=V_vertical
#PIC=U_horizon
PIC=V_horizon
####################

if [ $# -eq 0 ]; then
   ls fort.*; exit
else
   FILE="$*"
fi
cat /dev/null > pic.made

if [ "$1" = "fort.${S1}" ]; then
   cat OUT${S1}_1/fort.$PIC OUT${S1}_2/fort.$PIC > fort.${S1}
elif [ "$1" = "fort.${S2}" ]; then
   cat OUT${S2}_1/fort.$PIC OUT${S2}_2/fort.$PIC > fort.${S2}
elif [ "$1" = "fort.${S3}" ]; then
   cat OUT${S3}_1/fort.$PIC OUT${S3}_2/fort.$PIC > fort.${S3}
elif [ "$1" = "fort.all1" ]; then
   cat OUT${S1}_1/fort.$PIC OUT${S1}_2/fort.$PIC \
       OUT${S2}_1/fort.$PIC OUT${S2}_2/fort.$PIC > fort.all1
elif [ "$1" = "fort.all2" ]; then
   cat OUT${S2}_1/fort.$PIC OUT${S2}_2/fort.$PIC \
       OUT${S3}_1/fort.$PIC OUT${S3}_2/fort.$PIC > fort.all2
elif [ "$1" = "fort.all3" ]; then
   cat OUT${S1}_1/fort.$PIC OUT${S1}_2/fort.$PIC \
       OUT${S2}_1/fort.$PIC OUT${S2}_2/fort.$PIC \
       OUT${S3}_1/fort.$PIC OUT${S3}_2/fort.$PIC > fort.all3
else
   PIC="$1"
fi

linecolor=black
titlecolor=black
x1beg=0
x1end=1
d1num=0.25

for i in $FILE; do
   case $i in
   *.ps|*.eps|*.out) ;;
   *.c|*.cpp|*.f|*.f90) ;;
   *.o|*.h) ;;
   fort.0|fort.1|fort.2) ;;
   xx|pic) ;;
   *)
#---------------------------------------------

   if [ "`echo $PIC | grep vertical`" != "" ]; then
      label1="Y"
   else
      label1="X"
   fi
   if [ "`echo $PIC | grep U`" != "" ]; then
      label2="U"
   else
      label2="V"
   fi

   title="`echo $i | sed 's/fort\.//g'`"
   echo "pic: $title"

   n=`wc -l $i | awk '{print $1}'`
   nplot=1; linewidth=2
   lineon=1; lineoff=0

   if [ "$i" = "fort.${S1}" ]; then
      title="$PIC: n=${S1}"
      n=`wc -l OUT${S1}_1/fort.$PIC | awk '{print $1}'`
      n="$n,$n"
      nplot=2; linewidth="1.5,1.5"
      lineon=4,1; lineoff=4,0
   elif [ "$i" = "fort.${S2}" ]; then
      title="$PIC: n=${S2}"
      n=`wc -l OUT${S2}_1/fort.$PIC | awk '{print $1}'`
      n="$n,$n"
      nplot=2; linewidth="1.5,1.5"
      lineon=4,1; lineoff=4,0
   elif [ "$i" = "fort.${S3}" ]; then
      title="$PIC: n=${S3}"
      n=`wc -l OUT${S3}_1/fort.$PIC | awk '{print $1}'`
      n="$n,$n"
      nplot=2; linewidth="1.5,1.5"
      lineon=4,1; lineoff=4,0
   elif [ "$i" = "fort.all1" ]; then
      title="$PIC(${S1}:${S2})"
      n1=`wc -l OUT${S1}_1/fort.$PIC | awk '{print $1}'`
      n2=`wc -l OUT${S2}_1/fort.$PIC | awk '{print $1}'`
      n="$n1,$n1,$n2,$n2"
      nplot=4; linewidth="1,1,2,2"
      lineon=4,1,4,1; lineoff=4,0,4,0
   elif [ "$i" = "fort.all2" ]; then
      title="$PIC(${S2}:${S3})"
      n1=`wc -l OUT${S2}_1/fort.$PIC | awk '{print $1}'`
      n2=`wc -l OUT${S3}_1/fort.$PIC | awk '{print $1}'`
      n="$n1,$n1,$n2,$n2"
      nplot=4; linewidth="1,1,2,2"
      lineon=4,1,4,1; lineoff=4,0,4,0
   elif [ "$i" = "fort.all3" ]; then
      title="$PIC(${S1}:${S2}:${S3})"
      n1=`wc -l OUT${S1}_1/fort.$PIC | awk '{print $1}'`
      n2=`wc -l OUT${S2}_1/fort.$PIC | awk '{print $1}'`
      n3=`wc -l OUT${S3}_1/fort.$PIC | awk '{print $1}'`
      n="$n1,$n1,$n2,$n2,$n3,$n3"
      nplot=6; linewidth="1,1,1.5,1.5,2,2"
      lineon=4,1,4,1,4,1; lineoff=4,0,4,0,4,0
   fi

   a2b < $i n1=2 > $i.bin
   psgraph < $i.bin n=$n nplot=$nplot linecolor=$linecolor \
         linewidth=$linewidth \
         x1beg=$x1beg x1end=$x1end d1num=$d1num \
         wbox=6.5 hbox=6.5 xbox=1.25 ybox=2.25 \
         lineon=$lineon lineoff=$lineoff \
         titlecolor=$titlecolor style=normal \
         title="$title" label1="$label1" label2="$label2" > ${i}.ps


   echo $i.ps >> pic.made
   rm $i.bin

#---------------------------------------------
   ;;
   esac
done

