#!/bin/bash

PATH=/bin:/usr/bin:/sbin:/usr/sbin

# Requested by sooyeon.kim@samsung.com (.voice) and dalton.lee@samsung.com (.multiassistant)
dir_list=(".voice" ".multiassistant")
for item in "${dir_list[@]}"
do
	if [ -e "/etc/skel/share/$item" ]
	then
		find /etc/skel/share/$item -print0 | xargs -0 chown app_fw:app_fw
		find /etc/skel/share/$item -print0 | xargs -0 chsmack -a 'User::App::Shared'
		find /etc/skel/share/$item -type d -print0 | xargs -0 chsmack -t
	fi

	for line in `find /opt/usr/home -maxdepth 1 -type d`
	do
		if [ -e "$line/share/$item" ]; then
			user=$(echo $line | cut -d"/" -f5)
			if getent passwd "$user"
			then
				find "$line/share/$item" -print0 | xargs -0 chown $user:users
				find "$line/share/$item" -print0 | xargs -0 chsmack -a 'User::App::Shared'
				find "$line/share/$item" -type d -print0 | xargs -0 chsmack -t
			fi
		fi
	done
done

# change permission to /opt/var/lib/misc
# This is needed to retrieve CAP_DAC_OVERRIDE from mobileap-agent & dnsmasq.
if [ -e "/opt/var/lib/misc" ]
then
	chown root:system_share /opt/var/lib/misc
	chmod 0775 /opt/var/lib/misc
fi

# Set SMACK label as "System::Privileged" in /opt/var/security-manager/rules
if [ -e "/opt/var/security-manager/rules" ]
then
	chsmack -r -a "System::Privileged" /opt/var/security-manager/rules
fi
