#!/bin/sh

SCRIPT=$(readlink -f "$0")
DISTRPATH=$(dirname "$SCRIPT")

mkdir -p /opt/VideoGrace/Server/
mkdir -p /opt/VideoGrace/Server/bin
mkdir -p /opt/VideoGrace/Server/db
mkdir -p /opt/VideoGrace/Server/doc
mkdir -p /opt/VideoGrace/Server/content
cp -n $DISTRPATH/vgserver/vgserver.conf /opt/VideoGrace/Server/
cp $DISTRPATH/vgserver/VideoGraceServer /opt/VideoGrace/Server/
cp $DISTRPATH/vgserver/Consolidator /opt/VideoGrace/Server/
cp $DISTRPATH/vgserver/Recorder /opt/VideoGrace/Server/
chmod +x /opt/VideoGrace/Server/VideoGraceServer
chmod +x /opt/VideoGrace/Server/Consolidator
chmod +x /opt/VideoGrace/Server/Recorder
cp $DISTRPATH/vgserver/bin/* /opt/VideoGrace/Server/bin/
cp -n $DISTRPATH/vgserver/db/* /opt/VideoGrace/Server/db
cp $DISTRPATH/vgserver/doc/* /opt/VideoGrace/Server/doc/
if [ -d "$DISTRPATH/vgserver/content" ]; then
    cp -R $DISTRPATH/vgserver/content/. /opt/VideoGrace/Server/content/
fi
chmod 777 /opt/VideoGrace/Server/db
chmod 777 /opt/VideoGrace/Server/db/main.db
mkdir -p /var/log/vgserver
chmod 777 /var/log/vgserver

cp $DISTRPATH/vgserver/vgserver.service /etc/systemd/system/
systemctl enable vgserver
systemctl start vgserver

echo "\033[1m\033[32mVideoGrace Server was installed and runned successfully\033[m"
echo "Check the web panel: \033[1m\033[36mhttp://localhost:443\033[m\nFirst registered account will be \033[1m\033[31ma FULL OWNER GRANTED\033[m"
echo "More information on the our site: \033[1m\033[36mhttps://videograce.com\033[m \033[1m\033[31m🤝\033[m"
