#!/bin/bash

#
# Copyright (c) 2016-2020 Samsung Electronics Co., Ltd. All rights reserved.
#
# This file is licensed under the terms of MIT License or the Apache License
# Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.
# See the LICENSE file or the notice below for Apache License Version 2.0
# details.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

PATH=/bin:/usr/bin:/sbin:/usr/sbin
SRC_PATH=/etc/smack/accesses.d
DSC_PATH=/opt/var/security-manager/rules
MRG_PATH=/opt/var/security-manager/rules-merged/rules.merged
SHARED_RO_PATH=$DSC_PATH/2x_shared_ro

echo "Running migration process"

if [ ! -e $MRG_PATH ]
then
	echo "Moving files from ${SRC_PATH} to ${DSC_PATH}"
	mv ${SRC_PATH}/app_* ${SRC_PATH}/pkg_* ${SRC_PATH}/author_* ${DSC_PATH}
	echo "Merging files from ${DSC_PATH} to ${MRG_PATH}"
	cat ${DSC_PATH}/* > ${MRG_PATH}
fi

if [ ! -e $SHARED_RO_PATH ]
then
	echo "Moving SharedRO rules to ${SHARED_RO_PATH}"
	cat ${DSC_PATH}/pkg_* | grep 'User::App.*SharedRO' >${SHARED_RO_PATH}
	sed '/User::App.*SharedRO/d' -i ${DSC_PATH}/pkg_*
fi

echo "Migration process was finished"
