#!/bin/bash

user=`whoami`

if [[ "x$user" = "xroot" ]]; then
    echo "OK, you are root user or running as sudo"
else
    echo "Error: you must run the script as root or from sudo"
    exit 1
fi

dname=`dirname $0`

RUN_USER=$1

if [[ "x$RUN_USER" = "x" ]]; then
    echo "Usage:  $0  <runuser>   ### <runuser> represents the use name who runs the JagaurDB server process"
    exit 2
fi


HMDIR=$(getent passwd "$RUN_USER" | cut -d: -f6)

if [[ -z "$HMDIR" ]]; then
    echo "Error: user not found $RUN_USER  exit"
    exit 1
fi

if [[ -f "$HMDIR/.jaguarhome" ]]; then
    hm=$(cat "$HMDIR/.jaguarhome")
else
    hm=""
fi

if [[ -z "$hm" ]]; then
    JAGUAR_HOME="$HMDIR"
else
    JAGUAR_HOME="$hm"
fi




sed -e "s|RUN_USER|$RUN_USER|g" -e "s|JAGUAR_HOME|$JAGUAR_HOME|g"  $dname/jaguar-monitor.service.template > /etc/systemd/system/jaguar-monitor.service

systemctl daemon-reload
systemctl enable jaguar-monitor
systemctl start jaguar-monitor

systemctl status jaguar-monitor

