#!/bin/bash
module load gcc lib/hdf5

dir_rockstar="/mnt/ceph/users/camels/Codes/rockstar-galaxies_SB28" # rockstar repo directory
dir_constree="/mnt/ceph/users/camels/Codes/consistent-trees" # consistent-trees directory
dir_catalogues="/mnt/ceph/users/camels/Rockstar//IllustrisTNG/BE/BE_21" # snapshot directory

$dir_rockstar/rockstar-galaxies -c $dir_catalogues/rockstar_params.cfg &> $dir_catalogues/server.dat &

while [ ! -f "$dir_catalogues/auto-rockstar.cfg" ]; do echo "sleeping"; sleep 1; done

# Start client runs
$dir_rockstar/rockstar-galaxies -c $dir_catalogues/auto-rockstar.cfg >> $dir_catalogues/output.dat 2>&1

# run consistent tree 
perl $dir_rockstar/scripts/gen_merger_cfg.pl $dir_catalogues/rockstar.cfg

cd $dir_constree
perl do_merger_tree.pl $dir_catalogues/outputs/merger_tree.cfg
perl halo_trees_to_catalog.pl $dir_catalogues/outputs/merger_tree.cfg
ls $dir_catalogues/trees/tree_0_0_0.dat >& /dev/null
if [ $? -ne 0 ]; then
  echo "no $dir_catalogues/trees/tree_0_0_0.dat, probably failed"
  exit 666
else
  echo "$dir_catalogues/trees/tree_0_0_0.dat exists, probably succeeded"
  exit 0
fi