#!/bin/bash

cp -f /data/Software/mydan/Connector/openc3.crontab /etc/cron.d/

if [ "X$1" == "Xreload" ];then
    set -e
    /data/Software/mydan/Connector/tools/reload
    /data/Software/mydan/CI/tools/reload
    /data/Software/mydan/AGENT/tools/reload
    /data/Software/mydan/JOB/tools/reload
    /data/Software/mydan/JOBX/tools/reload

    echo open-c3 reload done.
    exit;
fi

CTRL=restart

if [ "X$1" == "Xstart" ];then
    CTRL=start
fi

cd /data/Software/mydan/CI/tools    && ./SetEnv -e test
cd /data/Software/mydan/AGENT/tools && ./SetEnv -e test
cd /data/Software/mydan/JOB/tools   && ./SetEnv -e test
cd /data/Software/mydan/JOBX/tools  && ./SetEnv -e test

cd /data/Software/mydan/Connector/tools && ./SetEnv -e test
cd /data/Software/mydan/Connector/tools && ./$CTRL

sleep 10;

cd /data/Software/mydan/CI/tools    && ./SetEnv -e test
cd /data/Software/mydan/AGENT/tools && ./SetEnv -e test
cd /data/Software/mydan/JOB/tools   && ./SetEnv -e test
cd /data/Software/mydan/JOBX/tools  && ./SetEnv -e test

cd /data/Software/mydan/CI/tools    && ./$CTRL
cd /data/Software/mydan/AGENT/tools && ./$CTRL
cd /data/Software/mydan/JOB/tools   && ./$CTRL
cd /data/Software/mydan/JOBX/tools  && ./$CTRL

/data/Software/mydan/web-shell/tools/start

mkdir -p /data/open-c3-data/nginx.conf
touch    /data/open-c3-data/nginx.conf/allow.conf

cp /data/Software/mydan/c3-front/nginx.conf /etc/nginx/nginx.conf 
cp /data/Software/mydan/c3-front/nginxconf/* /etc/nginx/conf.d/

if [ -f /etc/openc3.allinon ];then
    c3mc-sys-dup 2>&1 1>/tmp/c3mc-sys-dup.log | C3APPNAME=c3mc-sys-dup c3mc-base-log-addtime >> /var/log/open-c3.error.log
    sed -i 's/#githubbook//' /etc/nginx/conf.d/open-c3.org.conf 
fi

nginx -s reload
echo open-c3 $CTRL done.
