#!/bin/sh

# default
dcors=mu_ic4
nusim=nu_ic4

if [ $# -eq 0 ]; then
  echo "Default detector: IceCueb-S4"
elif [ $# -eq 1 ]; then
  det="$1"
  dcors=`echo "mu_ic4" | sed s/ic4/"$det"/`
  nusim=`echo "nu_ic4" | sed s/ic4/"$det"/`
else
  echo "Usage: chain.sh [det]"
  echo "    det: ic4  (IceCube-S4)"
  echo "    det: ic16 (IceCube-S16)"
  echo "    det: ai4  (AMANDA + IceCube-S4)"
  echo "    det: ai16 (AMANDA + IceCube-S16)"
  exit 1
fi
echo "$dcors, $nusim"

ls $dcors | grep hbook | awk '{print "chain dcors mu_ic4/" $1}' > chain_mu.kumac
echo 'dcors --> ' `ls $dcors | grep hbook | wc -l`

echo "(requires equal number of files for numu and numubar)"
n_nu=`ls $nusim | grep hbook | grep numu.0 | wc -l`
n_nb=`ls $nusim | grep hbook | grep numubar.0 | wc -l`
echo "nu: " $n_nu", nubar: " $n_nb

if [ "$n_nu" -lt "$n_nb" ]; then
  n=$n_nu
else
  n=$n_nb
fi

echo 'nusim --> ' $n
ls $nusim | grep hbook | grep numu.0 > .tmp
head -n $n < .tmp | awk '{print "chain nusim nu_ic4/" $1}' > chain_nu.kumac

ls $nusim | grep hbook | grep numubar.0 > .tmp
head -n $n < .tmp | awk '{print "chain nusim nu_ic4/" $1}' >> chain_nu.kumac
rm -f .tmp
