#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
    DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
    SOURCE="$(readlink "$SOURCE")"
    [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

cd $DIR/../
. ./meta/env

name=`cat meta/name`
sessionName=$name-session

tmux has-session -t $sessionName
hasSession=$?

if [ "$hasSession" = "0" ];then
    tmux attach -t $sessionName
    exit 0
fi
echo "waiting for initia build..."
tsc -p server/tsconfig.json

tmux new-session -d -s $sessionName -n "compile" "trap '' 2;. meta/env;cd webapp;./node_modules/watch-compile/bin/watchcompile -s -f ./Watchfile.webapp;bash"
tmux split-window -t $sessionName:"compile" "trap '' 2;. meta/env;cd server;./node_modules/watch-compile/bin/watchcompile -s -f ./Watchfile.server;bash"
tmux new-window -a -t $sessionName:"compile" -n "server" "trap '' 2;. meta/env;node ../cli/index.js --composer --composer-style-path style/main.css webapp/src/R.d.ts;bash"
tmux new-window -a -t $sessionName:"server" -n "bash" "trap '' 2;. meta/env;bash"

tmux attach -t $sessionName

