#!/bin/bash

if [[ -d "$HOME/fwww/venv_fwww" ]]; then
    echo "Found $HOME/fwww/venv_fwww, activate it ..."
    source $HOME/fwww/venv_fwww/bin/activate
fi

echo "Stop netmap_server ..."
./stop_netmap_server.sh
sleep 4
echo "Start netmap_server ..."
./start_netmap_server.sh &
sleep 4


echo "Stop pyai_server ..."
./stop_pyai_server.sh
sleep 3
echo "Start pyai_server ..."
./start_pyai_server.sh &
sleep 4
 

if [[ -S /tmp/fwww_netmap_server_socket ]]; then
    chmod 777 /tmp/fwww_netmap_server_socket
fi

if [[ -S /tmp/pyai_server_socket ]]; then
    chmod 777 /tmp/pyai_server_socket
fi

echo "Stop lighttpd and fwww ..."
./stop_prod_http.sh 
sleep 5
echo "Start lighttpd and fwww ..."
./start_prod_http.sh &

sleep 4
pid=`/bin/ps aux|grep watch_cleanup |grep -v grep|awk '{print $2}'`
if [[ "x$pid" = "x" ]]; then
    echo "watch_cleanup_files.sh is not running, OK. Restarting it now ..."
else
    kill -9 $pid
fi

sleep 3
./watch_cleanup_files.sh > /tmp/watch_cleanup_files.log &

