apt update
apt install -y docker.io supervisor

VER=$(wget https://cmc-dl.futuredial.com/tools/smartgrade-server/ver -q -O -)
echo $VER

mkdir -p /var/www/sgsrv/log/
mkdir -p /var/www/sgsrv/$VER
cd /var/www/sgsrv/$VER
wget https://cmc-dl.futuredial.com/tools/smartgrade-server/sgsrv_mongo.tar
docker load -i sgsrv_mongo.tar

cat >/etc/supervisor/conf.d/sgsrv.conf <<EOF
[program:sgsrv-db]
command=docker run --rm \
    --name sgsrv_db \
    -v /var/www/sgsrv/database:/data/db \
    -p 27017:27017 \
    mongo:3.6.17
directory=/var/www/sgsrv/
redirect_stderr=True
stdout_logfile=/var/www/sgsrv/log/%(program_name)s.log
numprocs=1
autostart=true
autorestart=true

[program:sgsrv-web]
command=bash -c 'sleep 10 && /var/www/sgsrv/start_web.sh'
directory=/var/www/sgsrv/
redirect_stderr=True
stdout_logfile=/var/www/sgsrv/log/%(program_name)s.log
numprocs=1
autostart=true
autorestart=true
EOF

wget -O - https://cmc-dl.futuredial.com/tools/smartgrade-server/$VER/updater.sh | bash