#!/bin/bash

# szb2filler is a part of SZARP SCADA software
#
# 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA  02110-1301, USA
#
# Author: Tomasz Pieczerak <tph AT newterm.pl>
# Copyright (C) 2014-2015 Newterm

VERSION="1.0"

# display usage info function
usage(){
	cat 1>&2 <<EOT
Usage: `basename $0` [OPTION] [DIR/FILE]...

Try '`basename $0` --help' for more options.
EOT
}

# display help on "-h" or "--help" option
if [ "x$1" = "x--help" -o "x$1" = "x-h" ]; then
	cat <<EOT
szb2filler $VERSION, set permissions and ownership of szbase files for Filler 2

Usage: `basename $0` [OPTION] [DIR/FILE]...

This script sets appropriate file and directory permissions and ownership
to enable Filler 2 modifications. If you want to modify 10-sec probes,
remember to set permissions of '/var/cache/szarp'.

Startup:
    -h, --help             print this help.
    -V, --version          display the version of szb2filler and exit.
    -u, --unset-current    assure current month files does not have
                                            permission to modify data.

Options:
    -e, --exclude-current  do not set permissions for current month files.

Mail bug reports and suggestions to <coders AT newterm.pl>.
EOT
	exit 0
fi

# print program version on "-V" or "--version" option
if [ "x$1" = "x--version" -o "x$1" = "x-V" ]; then
	cat <<EOT
szb2filler $VERSION

Copyright (C) 2015 Newterm
License GPLv3: GNU General Public License, version 3
<http://www.gnu.org/licenses/>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written and maintained by Tomasz Pieczerak <tph AT newterm.pl>.
Please send bug reports and questions to <coders AT newterm.pl>.
EOT
	exit 0
fi

# check if running as root
CUID=`id -u`
if [ $CUID -ne 0 ]; then
	echo "`basename $0`: error: must be run as root" 1>&2
	echo 1>&2
	usage
	exit 1
fi

# default options
ACTION="set"
EXCLUDE_CURRENT=false

# parse options
while [ "x$1" != "x" ]
do
	case $1 in
		--)
			shift; break;;	# end of options
		-u|--unset-current)
			ACTION="unset-current"
			shift
			;;
		-e|--exclude-current)
			EXCLUDE_CURRENT=true
			shift
			;;
		-*)
			echo "`basename $0`: unknown OPTION $1" 1>&2
			usage
			exit 1
			;;
		*)
			break;;
	esac
done

# check number of arguments
if [ $# -eq 0 ]; then
	echo "`basename $0`: error: missing DIR/FILE or OPTION" 1>&2
	echo 1>&2
	usage
	exit 1
fi

# perform action
CURRENT_MONTH="`date +%Y%m`"

case $ACTION in
	set)
		# set permissions and ownership to enable Filler 2 modifications
		while [ "x$1" != "x" ]
		do
			if [ -d "$1" ]; then
				# directories
				chown root:szbase "$1"
				find "$1" -type d -print0 | xargs -0 -r chmod 775
				find "$1" -type d -print0 | xargs -0 -r chown root:szbase

				# files
				if $EXCLUDE_CURRENT; then
					find "$1" -type f -name "*.sz[bc]" \
						 -a "!" -name "${CURRENT_MONTH}.sz[bc]" -print0 | xargs -0 -r chown root:szbase
					find "$1" -type f -name "*.sz[bc]" \
						 -a "!" -name "${CURRENT_MONTH}.sz[bc]" -print0 | xargs -0 -r chmod 664
				else
					find "$1" -type f -name "*.sz[bc]" -print0 | xargs -0 -r chown root:szbase
					find "$1" -type f -name "*.sz[bc]" -print0 | xargs -0 -r chmod 664
				fi
			elif [ -f "$1" ]; then
				if $EXCLUDE_CURRENT; then
					FN="`basename $1`"
					if [ "$FN" != "${CURRENT_MONTH}.szb" -a "$FN" != "${CURRENT_MONTH}.szc" ]; then
						chown root:szbase "$1"
						chmod 664 "$1"
					fi
				else
					chown root:szbase "$1"
					chmod 664 "$1"
				fi
			else
				echo "`basename $0`: warning: $1 is neither file nor directory, omitted" 1>&2
			fi

			shift
		done
		exit 0
		;;
	unset-current)
		# unset permissions for files from current month
		# TODO
		echo "`basename $0`: error: option not implemented" 1>&2
		exit 1
		;;
esac
