#!/bin/sh

#
# Configuration reset script for raperca
#

. /etc/raperca/init-functions

init_dirs

reset() {
    # Restore default config
    for f in "$CONFDIR"/spxconfig.xml "$CONFDIR"/logconfig.xml; do
        cp -f $f.spxsave $f
    done
    # Make sure that the shadow dir is mounted
    mount_shadow_dir
    # Clean all generated files
    clean "$TMPDIR" "$CACHEDIR" "$CONTENTDIR" "$SHADOWCONTENTDIR" "$LOGDIR" \
	"$USRCOMPORTDIR" "$SPOOLDIR" "$VARDIR" "$CAPTUREDIR" "$WEBSTORAGEDIR"
    # Put back the default content
    copy_default_content
    # Zap all locks from the dav lock database 
    clean_davlockdb
    # remove the SSL/TLS certificates added by the web admin interface
    # they are the *.pem files and hash links to them
    find /etc/ssl/certs/ -maxdepth 1 \
	\( -regex ".*/[a-f0-9]+\.[0-9]+" -o -name "*.pem" \) -print0 | \
	xargs -0r rm -f --
    # Remove the passwords / admin / content / log access restriction
    rm -f /etc/apache/conf.d/95-raperca-cont-prot.conf \
	/etc/apache/conf.d/95-raperca-adm-prot.conf \
	/etc/apache/conf.d/95-raperca-log-prot.conf \
	/etc/apache/conf.d/95-raperca-ro.conf \
	/etc/raperca/htpasswd /etc/raperca/spxpasswd.xml
    # Remove some status files
    rm -f /var/lib/raperca/logrotate.status
    # Restore uploader default config
    for f in /etc/raperca/{uploader.ics,uploaderlog.xml,uploaderconf.xml}; do
	cp -f $f.spxsave $f
    done
}

#
# Main
#

case "$1" in
    reset)
        reset
    ;;
    *)
        echo "Usage $0 {reset}" >&2
	exit 1
    ;;
esac
