#!/usr/bin/env bash
set -u

# make sure directory is empty
nstuff=$(ls -alF | wc -l)
[[ $nstuff -eq 3 ]] || { echo "error: directory is not empty!" 1>&2 ; exit 255; }

# check args
[[ $# -eq 1 ]] || { echo "usage: $0 <source project dir>" 1>&2; exit 255; }
dir="$1"

# common file of original project
common="$dir/common.sh"
[[ -f $common ]] || { echo "error: no local <common.sh> found" 1>&2; exit 255; }

# copy .sh
echo "Copying common.sh and pipeline.sh"
cp "$dir/common.sh" ./
cp "$dir/pipeline.sh" ./

echo "Creating/linking directories"
# make directories
mkdir .scratch

# link directories
for d in todo Results Cluster JobLog PWC Rawdata Sequences bin sbin; do
    [[ -d "$dir$d" ]] || { error_msg "unable to find directory <$dir$d>"; continue; }
    ln -sf "$dir$d"
done

# source local common
echo "Source and update local common.sh"
source common.sh

# update project dir
DIR_PROJECT=$(pwd)
DIR_ORTHOLOGER_PATH=${DIR_ORTHOLOGER_PATH}:${DIR_PROJECT}/bin

# update common.sh
pl_dump_common_sh > common.sh

# orthologer
cp ../orthologer.sh ./

# done
echo "Done. Remember to check generated common.sh!"
