#!/bin/csh -f
set EVOLDIR = Work/evol/code/twin/devel/
### set above to the directory (relative to your home) where the evolution 
### package is located.

# Set path, EV programme name and metalicity Z if these haven't been specified
if ( !($?evpath) ) then
   set evpath = $HOME/$EVOLDIR
   echo Warning: evpath not set, using default $evpath.
endif
if ( !($?Z) ) then
   set Z = 02
   echo Warning: Z not set, using default Z=$Z.
endif
if ( !($?EV) ) set EV = $evpath/code/ev

set name = $1

# remove old files
# Do we actually need this? Doesn't appear so to me... (EG)
# At worst, we can try to empty the files without asking. Removing them does
#  not, for instance, play nice with tail -f...
if (0) then
   if (-e $name.out1) rm -i $name.out1
   if (-e $name.out2) rm -i $name.out2
   if (-e $name.log) rm -i $name.log
   if (-e $name.out) rm -i $name.out
   if (-e $name.last1) rm -i $name.last1
   if (-e $name.last2) rm -i $name.last2
   if (-e $name.mod) rm -i $name.mod
   if (-e $name.plt1) rm -i $name.plt1
   if (-e $name.plt2) rm -i $name.plt2
   if (-e $name.mdl1) rm -i $name.mdl1
   if (-e $name.mdl2) rm -i $name.mdl2
else
   if (-e $name.out1) echo "" > $name.out1
   if (-e $name.log) echo "" > $name.log
   if (-e $name.out) echo "" > $name.out
   if (-e $name.last1) echo "" > $name.last1
   if (-e $name.last2) echo "" > $name.last2
   if (-e $name.mod) echo "" > $name.mod
   if (-e $name.plt1) echo "" > $name.plt1
   if (-e $name.mdl1) echo "" > $name.mdl1
endif

# config files
echo 0 > fort.11
ln -sf init.dat fort.22
ln -sf init.run fort.23

#touch the out files; convenient if they exist for running `tail -f' on them
touch $name.out1

# Backup init.dat and init.run
cp init.dat $name.dat
cp init.run $name.run

# input files
ln -sf $evpath/input/zahb{$Z}.mod fort.12
ln -sf $evpath/input/zahb.dat fort.24
ln -sf $evpath/input/zams/zams{$Z}.mod fort.16
ln -sf $evpath/input/zams/zams{$Z}.out fort.18
ln -sf $evpath/input/zams/zams{$Z}.mas fort.19
ln -sf $evpath/input/metals/z{$Z}/phys.z{$Z} fort.20
ln -sf $evpath/input/lt2ubv.dat fort.21
ln -sf $evpath/input/nucdata.dat fort.26
ln -sf $evpath/input/mutate.dat fort.63

### uncomment below to start from initial model in 'init.mod'
### then change init.run to read from unit 51.
#ln -sf init.mod fort.51

# output files
ln -sf $name.out1 fort.1
ln -sf $name.out2 fort.2
ln -sf $name.io12 fort.3
ln -sf $name.log fort.8
ln -sf $name.out fort.9
ln -sf $name.last1 fort.13
ln -sf $name.last2 fort.14
ln -sf $name.mod fort.15
ln -sf $name.plt1 fort.31
ln -sf $name.plt2 fort.32
ln -sf $name.mdl1 fort.33
ln -sf $name.mdl2 fort.34
ln -sf $name.mutate fort.64

# run code
#nice +19 $evpath/code/ev $name
echo Using $EV
$EV $name

tail -n 2 $name.log

# remove links
rm -f fort.16
rm -f fort.17
rm -f fort.18
rm -f fort.19
rm -f fort.20
rm -f fort.21
rm -f fort.22
rm -f fort.23
rm -f fort.24
rm -f fort.25
rm -f fort.26
rm -f fort.29
rm -f fort.63

rm -f fort.1
rm -f fort.2
rm -f fort.3
rm -f fort.8
rm -f fort.9
rm -f fort.10
rm -f fort.11
rm -f fort.12
rm -f fort.13
rm -f fort.14
rm -f fort.15
rm -f fort.31
rm -f fort.32
rm -f fort.33
rm -f fort.34
rm -f fort.64
