#!/bin/bash
#
# License: Copyright 2007 SpinetiX S.A. This file is licensed
#          under the terms of the GNU General Public License version 2.
#          This program is licensed "as is" without any warranty of any
#          kind, whether express or implied.
#
# Copyright 1999-2003 MontaVista Software, Inc.
# Copyright 2002, 2003, 2004 Sony Corporation
# Copyright 2002, 2003, 2004 Matsushita Electric Industrial Co., Ltd.
#
### BEGIN INIT INFO
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 5
# Default-Stop:
# Short-Description: Manipulates the raperca content dir
# Description: Installs default raperca content or restores rpmsaved content if necessary 
### END INIT INFO
# chkconfig: 235 01 0

# Init script information
INIT_NAME=raperca-content-fixup
DESC=""

case "$1" in
    start)

        echo -n "Fixing up raperca content"

        . /etc/raperca/init-functions
	init_dirs

	# Make sure that the shadow dir is mounted
	mount_shadow_dir

	# Make sure content dir exists and with appropriate perms
	make_content_dir "$SHADOWCONTENTDIR"

	# Rename .DAV dirs into .SPXDAV
	find "$CONTENTDIR" "$SHADOWCONTENTDIR" -type d -name .DAV -prune \
	    -exec mv -- \{\} \{\}/../.SPXDAV \;

	# Remove old status dir if it exists
	[ -e "$SRVDIR"/status ] && rm -rf "$SRVDIR"/status

	# Check and fix the group and perms of directories with special
	# permissions; some of them had different permissions on
	# pre-2.1.0 versions so they may be incorrect
	find "$CACHEDIR" "$CAPTUREDIR" "$LOGDIR" "$TMPDIR" \
	    -mindepth 0 -maxdepth 0 -print0 \! -group raperca | \
	    xargs -0r chown root:raperca --
	find "$CACHEDIR" "$CAPTUREDIR" "$LOGDIR" "$TMPDIR" \
	    -mindepth 0 -maxdepth 0 -print0 \! -perm 0775 | \
	    xargs -0r chmod 0775 --

	# Change the owner of temporary created by the raperca daemon
        find "$TMPDIR" "$CACHEDIR" -mindepth 1 -maxdepth 1 -print0 | \
	    xargs -0r chown -R -- raperca:raperca

	# Change the owner of log files created by the raperca daemon
        find "$LOGDIR" -type f -uid 0 \( -name "*.log" -o -name "*.log.[0-9]*" \) \
	    \! -name "uploader.log*"  -print0 | \
	    xargs -0r chown -- raperca:raperca

	# Change the owner of log files created by the uploader daemon
        find "$LOGDIR" -type f -uid 0 \
	    \( -name "uploader.log" -o -name "uploader.log.[0-9]*" \) -print0 | \
	    xargs -0r chown -- uploader:raperca

	# Change the owner of files created by the uploader daemon
        [ -e "$VARDIR/uploader_action.xml" ] && \
	    chown uploader:raperca "$VARDIR/uploader_action.xml"

	# Fix bad permissions on spxconfig.xml
	# (older versions sometimes set the file to 0600)
	chmod 0644 "$CONFDIR/spxconfig.xml"

	# Install default content if requested
	INSTALL_SENTINEL="$SPOOLDIR"/install-default-content
	if [ -f "$INSTALL_SENTINEL" ]; then
	    # First install, need to install the default content
	    copy_default_content
	    rm -f "$INSTALL_SENTINEL"
	fi

	# Be sure all changes are committed to stable storage
	sync

	# We just need to run once at boot
	initdconfig --del "$INIT_NAME"

	echo "."

    ;;
    stop)
    ;;
esac


