#!/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
#
  if [ $same_source ]; then
    $PENCIL_HOME/bin/pc_setupsrc --min
  else
    $PENCIL_HOME/bin/pc_setupsrc
  fi
#
#  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 higgsless.dat ] && cp higgsless.dat $targetdir
[ -f r_vs_m13.dat ] && cp r_vs_m13.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 power_profile.dat ] && cp power_profile.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 chiral_list.dat ] && cp chiral_list.dat $targetdir
[ -f a_vs_eta.dat ] && cp a_vs_eta.dat $targetdir
[ -f chiral_listZZ.dat ] && cp chiral_listZZ.dat $targetdir
[ -f data/emftensors.h5 ] && ln -s data/emftensors.h5 $targetdir/data/emftensors.h5
if [ -n "`ls *.py 2>/dev/null`" ]; then cp *.py $targetdir; fi
[ -f FAKE_PARALLEL_IO ] && touch $targetdir/FAKE_PARALLEL_IO
[ -f pc_commands.log ] && cp pc_commands.log $targetdir && echo -e "\n# `date +'%Y-%m-%d %H:%M:%S'`\n# copied from '$olddir'" >>  $targetdir/pc_commands.log
[ -f src/.moduleinfo ] && cp src/.moduleinfo $targetdir/src
[ -f src/.config-files ] && cp src/.config-files $targetdir/src
#
#  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
    lnk=`readlink $olddir/src/combine_videofiles.x`
    if [ "$lnk" != "" ]; then ln -s $lnk .; fi
    lnk=`readlink $olddir/src/read_vectorfiles.x` 
    if [ "$lnk" != "" ]; then ln -s $lnk .; fi
    lnk=`readlink $olddir/src/read_videofiles.x` 
    if [ "$lnk" != "" ]; then ln -s $lnk .; fi
    lnk=`readlink $olddir/src/read_all_videofiles.x`
    if [ "$lnk" != "" ]; then ln -s $lnk .; fi

    ln -s `readlink $olddir/src/start.x` .
    ln -s `readlink $olddir/src/run.x` .

    lnk=`readlink $olddir/src/special.so`
    if [ "$lnk" != "" ]; then ln -s $lnk .; fi
  else
    cd $olddir;
    if [ -e src/.config-files ]; then 
      pc_build -l -q -s -t helper_min >& /dev/null;
    else
      make helper_min >& /dev/null;
    fi
    cd $targetdir/src 
    if [ -n "`ls $olddir/src/*.x 2>/dev/null`" ]; then ln -s $olddir/src/*.x . ; fi
    if [ -n "`ls $olddir/src/*.so 2>/dev/null`" ]; then ln -s $olddir/src/*.so . ; fi
    if [ -n "`ls $olddir/src/astaroth/*.so 2>/dev/null`" ]; then ln -s $olddir/src/astaroth/*.so ./astaroth ; fi
    if [ -n "`ls $olddir/src/astaroth/submodule/build/src/core/*.so 2>/dev/null`" ]; then
      if [ ! -n "`ls astaroth/submodule/build/src/core 2>/dev/null`" ]; then mkdir -p astaroth/submodule/build/src/core ; fi
      ln -s $olddir/src/astaroth/submodule/build/src/core/*.so astaroth/submodule/build/src/core
    fi
    if [ -n "`ls $olddir/src/astaroth/submodule/build/src/core/kernels/*.so 2>/dev/null`" ]; then
      if [ ! -n "`ls astaroth/submodule/build/src/core/kernels 2>/dev/null`" ]; then mkdir -p astaroth/submodule/build/src/core/kernels ; fi
      ln -s $olddir/src/astaroth/submodule/build/src/core/kernels/*.so astaroth/submodule/build/src/core/kernels
    fi
  fi
  cp $olddir/src/*.inc .
  [ -f $olddir/src/revision.txt ] && cp $olddir/src/revision.txt .
fi