#!/bin/bash

#
# Copyright 2012 SpinetiX S.A.
#
# media-mount startup hook for raperca
#

# Load general helpers and config
. /etc/media-mount/media-mount-functions
load_sysconfig

# Old versions used direct mounts under sub-dirs of /srv/raperca/; this
# function cleans them out if present; the hook caller holds the fstab lock
clean_raperca_mounts() {
    local regexp="^[[:space:]]*[^#[:space:]][^[:space:]]\+[[:space:]]\+/srv/raperca/"
    grep -q "$regexp" /etc/fstab || return 0
    if ! mktmpfstab; then
	echo "ERROR: cannot create temporary fstab while cleaning fstab of raperca mounts"
	return 1
    fi
    grep -v "$regexp" /etc/fstab > "$TMPFSTAB"
    RET=$?
    if [ $RET -eq 0 ]; then
	savetmpfstab
	RET=$?
    elif [ $RET -eq 1 ]; then
	RET=0 # Nothing to clean in fstab
    fi

    if [ $RET -ne 0 ]; then
	echo "ERROR: failed cleaning fstab of raperca mounts (RET = $RET)"
    fi
    
    return $RET
}

case "$1" in
    init)
	clean_raperca_mounts
	exec /usr/lib/raperca/uploader-action cleanup
	;;
    ready)
	exec /usr/lib/raperca/uploader-action resume
	;;
    *)
	echo "$0: unknown arguments:" "$@" >&2
	;;
esac

exit 1
