#!/bin/bash

source /usr/local/pf/containers/systemd-service

name=ntlm-auth-api

conf_dir="/usr/local/pf/var/conf/${name}.d/"
env_files=$(ls $conf_dir 2>/dev/null)
option=$1

if [ -z "$env_files" ]; then
    exit 1
fi

if [ "$option" == "" ]; then
    echo "No option given, terminated"
    exit 1
fi

if [ "$option" = "start" ]; then
    for env_file in $env_files; do
        iden=$(echo $env_file | awk -F '.' '{print $1}')
        echo "starting ntlm auth api domain service for: $iden using env file: $env_file"
        systemctl start packetfence-ntlm-auth-api-domain@"$iden"
    done
    sleep infinity
elif [ "$option" = "stop" ]; then
    for env_file in $env_files; do
        iden=$(echo $env_file | awk -F '.' '{print $1}')
        echo "stopping ntlm auth api domain service for: $iden"
        systemctl stop packetfence-ntlm-auth-api-domain@"$iden"
    done
    echo "stopped"
    sleep 1
fi
