#!/bin/bash
PATH=/bin:/usr/bin:/sbin:/usr/sbin

result_dir="/opt/share/security-config/result"
log_dir="/opt/share/security-config/log"
cur_dir="/usr/share/security-config/test/new_service_test"
not_permitted_unit_path="/opt/share/security-config/not_permitted_service/"
system_unit_dir="/usr/lib/systemd/system/"
dbus_service_dir="/usr/share/dbus-1/system-services/"
systemd_service_list_csv="$cur_dir/systemd_service.csv"
systemd_socket_list_csv="$cur_dir/systemd_socket.csv"
systemd_service_list="$cur_dir/systemd_service.list"
systemd_socket_list="$cur_dir/systemd_socket.list"
# asterisk represents configuration subdirectories for ISU packages
# their names must directly correspond to the package names
# (e.g., dlog for the dlog package, deviced for the deviced package etc.)
isu_system_unit_dir="/etc/isu/*/system-services/"

# function to move service file to another directory. This socket cannot be used.
# $1 : file path, $2 : file name
function move_systemd_unit
{
	if [ ! -d $not_permitted_unit_path ]
	then
		mkdir $not_permitted_unit_path
	fi
	mv "$1" "$not_permitted_unit_path$2"
}


# init test (move unit file in not_permitted_list to real dir and remove log and result files)
# $1 : log path, $2 : result path
function init_test
{
	if [ -e "$1" ]
	then
		# Restore not permitted service
		for line in `cat $1`
		do
			unit_name=$(echo $line | rev | cut -f1 -d "/" | rev)
			if [ -e "$not_permitted_unit_path$unit_name" ]
			then
				mv $not_permitted_unit_path$unit_name $line
			fi
		done
		rm $1
	fi
	if [ -e "$2" ]
	then
		rm $2
	fi
}

# check a log file and make a result
# $1 : log path, $2 : test name, $3 : result path
function check_result
{
	if [ ! -e $1 ]
	then
		echo "Success! All $2 are in the list"
		echo "YES" > $3
	else
		echo "Fail! Please check the log and "$not_permitted_unit_path
		echo "NO" > $3
	fi
}


# check exception
# $1 : target name, $2 : exception path
function check_exception
{
	temp=$(/usr/bin/grep $1 <<< /usr/bin/cat $2)
	if [ -n "$temp" ]
	then
		return 1
	fi
	return 0
}
