#!/bin/bash
#
#  $Id$
#
#  This script uses pc_mkdatadir which links the subdirectory data
#  to some data disk (which is machine dependent and therefore
#  you may want to adapt pc_mkdatadir for your particular machine).
#
#  Usage:
#     pc_newrun run1 run2
#  Alternative usage, if one is already in run1
#     pc_newrun run2
#  Options: -s or --same_source also mirrors the source directory
#

case "$1" in
   -h|--help)
   echo "Usage: pc_newrun [-s]"
   exit;;

   -s|--same_source)
   echo "Using same source directory"
   same_source=1
   shift;;
esac

if [ $# -eq 2 ]; then
  #
  #  check whether absolute or relative path
  #  by checking whether target exists
  #
  if [ -d `pwd`/$1 ]; then
    olddir=`pwd`/$1
  else
    olddir=$1
  fi
  #
  #  same for newdir
  #
  if [ -d `pwd`/$2 ]; then
    newdir=`pwd`/$2
  else
    newdir=$2
  fi
else
  olddir=`pwd`
  cd ..
  #
  #  same for newdir
  #
  if [ -d `pwd`/$1 ]; then
    newdir=`pwd`/$1
  else
    newdir=$1
  fi
fi

#
#  save current working directory and make new run directory
#  and go there.
#
parentdir=`pwd`
echo "Parent directory: " $parentdir
if [ -d $newdir ]; then
  echo "The directory $newdir already exists!"
  echo "You had better stop and check..."
  exit
else
  mkdir $newdir
  echo "Created directory $newdir"
fi
cd $newdir
targetdir=`pwd`
#
#  In the new run directory, call pc_mkdatadir, and
#  write file with name of directory from which
#  the new one is generated
#
$PENCIL_HOME/bin/pc_mkdatadir

#
#  write source directory name into file new_from.dir
#
[ -e data/new_from.dir ] || touch data/new_from.dir
(echo $olddir; /bin/date) >> data/new_from.dir
#
#  make src dir and go there
#
  mkdir src
  cd src
#
#  use *.local files from previous src directory
#  and run pc_setupsrc
#
  echo "old source directory: " $olddir/src
  cp $olddir/src/*.local .
  echo "Use *.local files from:" `pwd`; ls -l *.local
  cd ..
#
  if [ -d $olddir/initial_condition ]; then
     echo "copying existing initial condition directory"
     mkdir ./initial_condition
     ln -s $olddir/initial_condition/*f90 ./initial_condition
  fi
#
#  if there exists a bin/pc_setupsrc.local file
#  it is being executed prior to pc_setupsrc
#  This gives the possibility to link your own routines
#  into src (pc_setupsrc below won't overwrite those)
#

  [ -f $parentdir/bin/pc_setupsrc.local ] && $parentdir/bin/pc_setupsrc.local
#
#  continue with the usual setup procedure
#
  $PENCIL_HOME/bin/pc_setupsrc
#
#  go back into source directory and write the name
#  of the new target directory into file new_to.dir
#
cd $olddir
[ -e data/new_to.dir ] || touch data/new_to.dir
(echo $newdir; /bin/date; echo "") >> data/new_to.dir
#
#  copy *.in and k.dat files into new directory
#  the k.dat file is actually only needed for forced
#  turbulence runs
#
echo "Use *.in files from:" `pwd`; ls -l *.in
cp *.in $targetdir
[ -f k.dat ] && cp k.dat $targetdir
[ -f k_double.dat ] && cp k_double.dat $targetdir
[ -f kvect.dat ] && cp kvect.dat $targetdir
[ -f chemistry.dat ] && cp chemistry.dat $targetdir
[ -f chemistry_m.dat ] && cp chemistry_m.dat $targetdir
[ -f chemistry_p.dat ] && cp chemistry_p.dat $targetdir
[ -f particles_initial.dat ] && cp particles_initial.dat $targetdir
[ -f particles_initial_header.pro ] && cp particles_initial_header.pro $targetdir
[ -f parameters.pro ] && cp parameters.pro $targetdir
[ -f stratification.dat ] && cp stratification.dat $targetdir
[ -f hcond_glhc.dat ] && cp hcond_glhc.dat $targetdir
[ -f chem.inp ] && cp chem.inp $targetdir
[ -f tran.dat ] && cp tran.dat $targetdir
[ -f nnu2.dat ] && cp nnu2.dat $targetdir
[ -f air.dat ] && cp air.dat $targetdir
[ -f alpha_in.dat ] && cp alpha_in.dat $targetdir
[ -f *.py ] && cp *.py $targetdir
[ -f FAKE_PARALLEL_IO ] && touch  $targetdir/FAKE_PARALLEL_IO
[ -f .pc_build_history ] && cp .pc_build_history $targetdir && echo -e "#\n# Copied from $olddir on `date`" >>  $targetdir/.pc_build_history
#

#
#  if same_source, then link all *.x files from the original directory.
#  (We don't want to use a link to the entire src directory, because
#  then a run directory that is checked in would not possess a record
#  of it's own src/*.local files.)
#  Also: copy $olddir/src/*.inc, which are needed for remeshing.
#
if [ $same_source ]; then
  cd $targetdir/src
  pwd

  if [ -h $olddir/src/run.x ]; then
    ln -s `readlink $olddir/src/combine_videofiles.x` .
    ln -s `readlink $olddir/src/read_vectorfiles.x` .
    ln -s `readlink $olddir/src/read_videofiles.x` .
    ln -s `readlink $olddir/src/read_all_videofiles.x` .
    ln -s `readlink $olddir/src/start.x` .
    ln -s `readlink $olddir/src/run.x` .
  else
    ln -s $olddir/src/*.x .
  fi
  cp $olddir/src/*.inc .
fi
