#!/bin/bash
if [ _$CB_ENVDIR == '_' ]; then
    echo AIP runtime environment is not set. Please source the profile.
    exit 1
fi

admin=`lsid | grep admin | awk '{print $3}'`
if [ _$admin == '_' ]; then
    echo AIP system is not configured or down
    exit 1
fi

sed -i "s|ADMIN|$admin|g" $CB_ENVDIR/olmon
cat <<EOF > /lib/systemd/system/olmon.service
[Unit]
Description=SkyForm AIP ES Monitoring Service
After=network.target remote-fs.target

[Service]
Type=oneshot
ExecStartPre=/bin/bash -c 'while [ ! -f $CB_ENVDIR/olmon.conf ];do sleep 5;done'
ExecStart=$CB_ENVDIR/olmon start
ExecStop=$CB_ENVDIR/olmon stop
RemainAfterExit=yes
TimeoutStartSec=300

[Install]
WantedBy=multi-user.target
EOF
systemctl enable olmon
echo The installation is done!
echo
echo '***' Please configure $CB_ENVDIR/olmon.conf '***'
echo
echo "IMPORTANT!"
echo If you use the AIP Web Portal to view reports from ES,
echo on the AIP Web Portal server, please modify IP addresses
echo of Kibana and Grafana in echo /etc/nginx/nginx.conf,
echo then restart the nginx service: systemctl restart nginx
