#!/bin/bash
SVER='1.0.0'

##############################################################################
# usesc - Changes the owner and permissions in archives for patdevel
# Copyright (C) 2023 SUSE LLC
#
# Description:  Changes owner to root.users and permissions to 664.
# Modified:     2023 Aug 07
#
##############################################################################
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; version 2 of the License.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, see <http://www.gnu.org/licenses/>.
#
#  Authors/Contributors:
#     Jason Record <jason.record@suse.com>
#
##############################################################################

echo "##############################################################################"
echo "# SCA Pattern Archives Owner/Mode Tool, v${SVER}"
echo "##############################################################################"
LCONF='/etc/opt/patdevel/patdev.conf'
[[ -s $LCONF ]] || { echo "ERROR: File not found, $LCONF"; echo; exit 1; }
OWNER='root.users'
DIR_MODE='775'
FILE_MODE='664'

echo "Changing to user permissions in archive directory"
SCA_BASE_DIR=$(grep "^sca_base_dir" $LCONF | awk -F= '{print $NF}' | sed -e "s/[',\"]//g;s/^[[:space:]]*//g;s/[[:space:]]*$//g")
SCA_ARCH_DIR=$(grep "^sca_arch_dir" $LCONF | awk -F= '{print $NF}' | sed -e "s/[',\"]//g;s/^[[:space:]]*//g;s/[[:space:]]*$//g")
PATDEV_ARCH_DIR=$(echo $SCA_ARCH_DIR | sed -e "s!\${sca_base_dir}!${SCA_BASE_DIR}!g")
echo "Archive Directory: ${PATDEV_ARCH_DIR}"
echo
if [[ -n $PATDEV_ARCH_DIR ]]; then
	if [[ -d $PATDEV_ARCH_DIR ]]; then
		find $PATDEV_ARCH_DIR -exec chown $OWNER {} \+ &> /dev/null
		find $PATDEV_ARCH_DIR -type d -exec chmod $DIR_MODE {} \+ &> /dev/null
		find $PATDEV_ARCH_DIR -type f -exec chmod $FILE_MODE {} \+ &> /dev/null
		cd $PATDEV_ARCH_DIR
		ls -l $LS_OPTIONS | grep -v total
	else
		echo "Archive directory not found - $PATDEV_ARCH_DIR"
	fi
else
	echo " + Archive directory not defined, check [Common] sca_arch_dir in $LCONF"
fi	
echo

