#!/bin/bash
#
# Initialization functions for raperca
#

# Initializes the location variables, if the argument is non-empty
# it overrides the default raperca service directory
init_dirs() {
    SRVDIR=/srv/raperca
    SHADOWSRVDIR=/srv/.shadow-raperca
    if [ -n "$1" ]; then
	SRVDIR="$1"
	SHADOWSRVDIR="$(dirname "$SRVDIR")"/.shadow-raperca
    fi
    STARTDIR="$SRVDIR"
    TMPDIR="$SRVDIR"/tmp
    CACHEDIR="$SRVDIR"/cache
    CAPTUREDIR="$SRVDIR"/capture
    WEBSTORAGEDIR="$SRVDIR"/webstorage
    LOGDIR="$SRVDIR"/log
    CONTENTDIR="$SRVDIR"/content
    SHADOWCONTENTDIR="$SHADOWSRVDIR"/content
    TMPCONTENTDIR="$CONTENTDIR"/.tmp
    SHADOWTMPCONTENTDIR="$SHADOWCONTENTDIR"/.tmp
    DEFCONTENTDIR=/usr/share/resources/default/content/default
    USRCOMPORTDIR="$SRVDIR"/comportfsm
    ADMINDIR="$SRVDIR"/admin
    FONTDIR="$CONTENTDIR"/fonts
    CONFDIR=/etc/raperca
    DAVLOCKDBDIR=/var/cache/raperca/dav
    SPOOLDIR=/var/spool/raperca
    VARDIR=/var/lib/raperca
}

# Mounts the shadow dir if not already mounted
mount_shadow_dir() {
    grep -q "^[^[:space:]]\+[[:space:]]\+$SHADOWSRVDIR[[:space:]]\+" \
	/proc/mounts || mount --bind "$SRVDIR" "$SHADOWSRVDIR"
}

# Creates the specified content dir, if not already present
# and sets the permissions correctly
make_content_dir()
{
    [ -n "$1" ] || return
    [ -d "$(dirname "$1")" ] || return
    mkdir -p "$1" && chown root:www "$1" && chmod 0775 "$1"
}

# Copies the default content to the list dirs provided as arguments;
# if none provided, or first arg is empty, it defaults to
# "$SHADOWCONTENTDIR" and "$CONTENTDIR".
# The copy is done through the first dir, so if two dirs are effectively
# the same the further copies are a no-op.
copy_default_content() {
    if [ -z "$1" ]; then
	copy_default_content "$SHADOWCONTENTDIR" "$CONTENTDIR"
    else
	local SDIR
	SDIR="$1"
	make_content_dir "$SDIR" && \
	    find "$DEFCONTENTDIR" -mindepth 1 -maxdepth 1 -print0 | \
	    xargs -0r cp -f -r --target-directory="$SDIR" && \
	    find "$SDIR" -mindepth 1 -print0 | xargs -0r chown www:www
	[ -e "$SDIR"/index.svg ] && touch "$SDIR"/index.svg
	while shift; do
	    make_content_dir "$1" && \
	    find "$SDIR" -mindepth 1 -maxdepth 1 -print0 | \
		xargs -0r cp -f -r -p --target-directory="$1" 2>/dev/null
	    [ -e "$1"/index.svg ] && touch "$1"/index.svg
	done
    fi
}

# Removes the contents of all the directories passed as arguments
clean() {
    # recursively remove all contents of the passed-in dirs
    # this way hidden files are also catched.
    find  "$@" -mindepth 1 -maxdepth 1 -print0 | \
	xargs -0r rm -rf -- 2>/dev/null
}

# Removes the contents of all the directories passed as arguments
# but leaves .htaccess in the top dir in place
clean_nohtaccess() {
    # recursively remove all contents of the passed-in dirs
    # this way hidden files are also catched.
    find  "$@" -mindepth 1 -maxdepth 1 \! -name .htaccess -print0 | xargs -0r rm -rf --
}

# Removes all dav_fs tmp file from all the directories passed
# as arguments.
clean_dav_tmp() {
    find "$@" -type f -name ".davtmp.*" -print0 | xargs -0r rm -f --
}

# Cleans the dav lock database
clean_davlockdb() {
    find "$DAVLOCKDBDIR" -type f -print0 | xargs -0r rm -f --
}
