#! /bin/sh -
#SCRIPT TO RUN EMPIRE (ONLY X-SEC CALCULATIONS) 
echo '                                                           '
echo '          EEEEE  M    M  PPPP   I  RRRR   EEEEE      H   H '
echo '          E      MM  MM  P   P  I  R   R  E          H   H '
echo '          EEE    M MM M  PPPP   I  RRRR   EEE    =   H   H ' 
echo '          E      M    M  P      I  R  R   E          H   H '
echo '          EEEEE  M    M  P      I  R   R  EEEEE      H   H '
echo '                                                           '
echo '                     V E R S I O N  2.19.beta24            '
echo '                                                           '
echo '                       marching towards LODI               '
echo '                                                           '
echo ' '
file=$1
work=`pwd`
if [ "$1" = "" ]
   then
    echo 'Inputs ready to run'
    echo ' '
    ls *inp
    echo ' '
    echo -n 'Choose one of the above (without .inp extention!): '
    read file
fi                          
if [ -f $file.inp ]; then
   echo 'Using existing file with input:'  $file.inp
   else
   echo 'Input file :' $file.inp ' does not exist'
   exit
fi

# CLEAN ALL THE FILES WHICH MIGHT BE LEFT FROM THE PREVIOUS RUN

rm $file.out $file.lst $file.endf $file.ps  2>/dev/null
rm ecis03.tlj ecis03.cs ecis03.ics ecis03.ang 2>/dev/null
rm INCIDENT.CS INCIDENT.ICS INCIDENT.ANG INCIDENT.TLJ 2>/dev/null
rm  OUTPUT.DAT LIST.DAT 2>/dev/null
rm  OMPAR.RIPL GAMMA.DAT fort.* FUSION 2>/dev/null
rm  OMPAR.DIR 2>/dev/null
rm  FISSION.INP 2>/dev/null
rm  FISSION.OUT 2>/dev/null
rm  CUMULPLOT.PS 2>/dev/null
rm  ecVIB.inp ecROT.inp ecVIBROT.inp 2>/dev/null
rm  ECIS_ROT.out ECIS_VIB.out ECIS_VIBROT.out  2>/dev/null
rm  TARGET_COLL.DAT 2>/dev/null
rm  TARGET_COLL.RIPL 2>/dev/null
rm  TARGET-*.tl 2>/dev/null
rm  warnings 2>/dev/null
rm  INPUT 2>/dev/null

# COPY INPUT FILES FOR A GIVEN CASE

cp $file.inp INPUT.DAT
if [ -s $file.lev ]; then
   cp $file.lev LEVELS
   echo 'Using existing file with levels:'  $file.lev
fi
if [ -s $file-omp.ripl ]; then
   cp $file-omp.ripl OMPAR.RIPL
   echo 'Using existing file with optical model par.:'  $file-omp.ripl
fi
if [ -s $file-omp.dir ]; then
   cp $file-omp.dir OMPAR.DIR
   echo 'Using existing file with optical model par.:'  $file-omp.dir
fi
if [ -d $file-tl ]; then
   cd ../
   ln -sf $work/$file-tl TL
   cd $work
   echo 'Using transmission coefficients stored in:'  $file-tl
else
   rm -r ../TL/ 2>/dev/null
   mkdir ../TL
fi
if [ -s $file-lev.col ]; then
   cp $file-lev.col TARGET_COLL.DAT
   echo 'Using existing file with collective levels:'  $file-lev.col
fi
if [ -s $file.fus ]; then
   cp $file.fus FUSION
   echo 'Using existing file with fusion cross sections:'  $file.fus
fi
if [ -s $file-inp.fis ]; then
   cp $file-inp.fis FISSION.INP 
   echo 'Using existing file with fission input:'  $file-inp.fis
fi
if [ -s $file.exf ]; then
   cp $file.exf EXFOR.dat
   echo 'Using existing file with EXFOR data:'  $file.exf
fi
echo ' '
echo $file ' R U N N I N G  !! !  !   !    !     !'
../source/empire
echo 'DONE !!!'

#MOVE CASE-RELATED FILES TO THE RIGHT PALCE

mv OUTPUT.DAT $file.out
mv LIST.DAT $file.lst
mv LEVELS $file.lev
if [ ! -d $file-tl ]; then
   mv ../TL $file-tl 
fi
rm -r ../TL 2>/dev/null
if [ ! -s $file-omp.ripl ]; then
   cp OMPAR.RIPL $file-omp.ripl  2>/dev/null
fi
if [ ! -s $file-omp.dir ]; then
   cp OMPAR.DIR $file-omp.dir  2>/dev/null
fi
if [ ! -s $file-inp.fis ]; then
   cp FISSION.INP $file-inp.fis  2>/dev/null
fi
if [ ! -s $file-lev.col ]; then
   if [ -s TARGET_COLL.RIPL ]; then
      mv TARGET_COLL.RIPL $file-lev.col 2>/dev/null
      echo ' ' >>$file-lev.col
      echo ' ' >>$file-lev.col
      echo ' ' >>$file-lev.col
      echo 'NOTE: this line and those below will not be used by Empire. ' >>$file-lev.col
      echo '      The collective levels and ground state band deformation ' >>$file-lev.col
      echo '      printed below are inferred from the discrete level file   ' >>$file-lev.col
      echo '      and provided for comparison with the RIPL data above.' >>$file-lev.col
      echo ' ' >>$file-lev.col
      echo ' ' >>$file-lev.col
   fi 
   if [ -s TARGET_COLL.DAT ]; then
      cat TARGET_COLL.DAT >>$file-lev.col
   fi 
fi
#if [ -s CUMULPLOT.PS ]; then
#   ps2ps -r9200 CUMULPLOT.PS $file-cum.ps
#   gv -landscape $file-cum.ps &
#fi

mv EXFOR.dat $file.exf
mv ECIS_ROT.out $file-ecis.out 2>/dev/null
mv ECIS_VIB.out $file-ecis.out 2>/dev/null
mv ECIS_VIBROT.out $file-ecis.out 2>/dev/null
mv ecVIB.inp $file-ecis.in 2>/dev/null
mv ecROT.inp $file-ecis.in 2>/dev/null
mv ecVIBROT.inp $file-ecis.in 2>/dev/null

# EXTRACT WARNINGS

grep WARNING $file.lst >$file.war

# DELETE TEMPORARY FILES

rm FUSION fort.70 fort.66 fort.16 2>/dev/null
rm  ecVIB.inp ecROT.inp ecVIBROT.inp 2>/dev/null
rm ECIS_ROT.out ECIS_VIB.out ECIS_VIBROT.out 2>/dev/null
rm OMPAR.RIPL TARGET-*.tl TARGET_COLL.RIPL TARGET_COLL.DAT 2>/dev/null
rm TARGET.LEV  2>/dev/null
rm EXFOR.datp  2>/dev/null
rm INPUT  2>/dev/null
rm INPUT.DAT  2>/dev/null
rm FISSION.INP  2>/dev/null
#rm FISSION.OUT  2>/dev/null
rm OMPAR.DIR  2>/dev/null
rm OMPdata.dat  2>/dev/null
rm mt_x4r.txt  2>/dev/null
rm TAPE15 2>/dev/null
rm TAPE16 2>/dev/null
rm TAPE66 2>/dev/null
rm file99 2>/dev/null
rm DEGASRESULT 2>/dev/null
rm angdis.{pnt,cur} 2>/dev/null
rm {ccm,dwba,INCIDENT}.{CS,TLJ,ANG,ICS} 2>/dev/null
rm ecis03.{ang,cs,dat,ics,pol,tlj} 2>/dev/null
rm ecSPH.inp 2>/dev/null
rm ECIS_SPH.out 2>/dev/null


#if  [ -s $file.exf ]; then
#    if  [ ! -s $file.c4 ]; then
#        # RUN X4TOC4 TO TRANSLATE EXFOR FILE INTO COMPUTATIONAL FORMAT

#        ../scripts/c4 $file

        # RUN c4sort TO SORT C4 DATA   

#        ../scripts/sortc4 $file  

#    fi
#fi

# REMOVE EMPTY FILES


if [ ! -s $file-omp.dir ]; then 
       rm $file-omp.dir  2>/dev/null
fi
if [ ! -s $file-lev.col ]; then 
       rm $file-lev.col  2>/dev/null
fi
if [ ! -s $file.war ]; then 
       rm $file.war  2>/dev/null
fi
if [ ! -s $file-ecis.in ]; then 
       rm $file-ecis.in  2>/dev/null
fi
if [ ! -s $file-ecis.out ]; then 
       rm $file-ecis.out  2>/dev/null
fi
if [ ! -s $file-inp.fis ]; then 
       rm $file-inp.fis  2>/dev/null
fi
if [ ! -s FISSION.OUT ]; then 
       rm FISSION.OUT  2>/dev/null
fi

exit
