#!/bin/bash

if [ -z "$1" ]
then
>&2 echo "need conf file as parameter"
exit 1
fi

id -u clc &>/dev/null || useradd clc 

mkdir -p /etc/clc
cp "$1" /etc/clc/server.conf
chown -R clc:clc /etc/clc
chmod -f -R 760 /etc/clc || :
#rm /etc/clc/server.conf

DIRECTORY=`realpath $0`
DIRECTORY=`dirname $DIRECTORY`

export filename="/etc/clc/server.conf"
var_clc_data=`cat $filename | python -c "import sys, json; print json.load(sys.stdin)['server']['data']['path']"`
var_clc_tokens=`cat $filename | python -c "import sys, json; print json.load(sys.stdin)['server']['tokens']['path']"`

mkdir -p /var/clc
mkdir -p /var/clc/data
mkdir -p /var/clc/tokens
chown -R clc:clc /var/clc
chmod -f -R 744 "$var_clc_data" || :
chmod -f -R 700 "$var_clc_tokens" || :
chmod -f -R 700 /var/clc/*.conf || :

mkdir -p /var/clclog/
chown -R clc:clc /var/clclog
chmod -f -R 744 /var/clclog || :

systemctl stop CLCServer
cp $DIRECTORY/CLCServer.service /lib/systemd/system/CLCServer.service
systemctl daemon-reload
systemctl start CLCServer
systemctl enable CLCServer

