#!/bin/bash

# Start, stop data delivery
# done by creating/removing lockfiles

# $Id$

usage () {
    cat >&2 <<EOF
$0: manipulate data delivery for the user
Usage:
	$0 <cmd> <$allqueues> [landername[ landername]]
where <cmd> is one of the following
	start	- start data delivery (may take a while)
	stop	- stop data delivery
	status	- query status of data delivery
The second argument controls whether raw,only host,
        full, flow or a hooked queue data is delivered; 
        it defaults to host.
The third (optional) argument controls which lander data
        the user is subscribing to; it defaults to all
EOF
}

LANDERCONF=~plander/lander.conf

[ -r $LANDERCONF ] || {
    echo "User $LOGNAME does not have read permissions for /home/plander/lander.conf"
    echo "(must be a member of csci-ar group, but probably isn't)"
    echo "(current groups: $((id -nG)))"
    exit 1
} >&2

. $LANDERCONF

datatype=all
landername=all

#set $overquota_check_ to "yes" if user $1 is overquota or "" if not
user_under_quota() {
    local _user=$1
    local _user _quota _usage _rest
    read _user _quota _usage _rest < <(grep ^$_user $LANDER_QUOTAFILE)
    if [ -z "$_quota" -o -z "_$usage" ]; then
        echo "Bad quota for user $user"
        return 1
    else
        [ $_quota -gt $_usage ] && return 0
    fi
    return 1
}

user_check() {
    local _user=$1
    local _type=$2
    local _u _rest

    user_check_err=""

    eval local _group=\$LANDER_GROUP_$(echo $_userstype | tr '[:lower:]' '[:upper:]')
    if ! groups $_user | grep -q " $_group"; then
	echo "ERROR: user $_user is not authorized to receive $_type data"
	return 1
    fi
    if ! user_under_quota $_user; then
	echo "WARNING: user $_user is currently over quota (cleanup before data is delivered)"
    fi
    return 0
}

delivery_start() {
    local _user=$1
    local _type=$2
    local _landers=$3
    local _setup="$LANDER_SCRIPTDIR/lander_setup.sh"

    if [ -x "$_setup" ]; then
	( 
	    export LOGNAME="$_user"; 
	    $_setup -q -Q$_type || {
		echo "ERROR: user $_user must rerun 'lander_setup.sh -Q $_type -y'"
		exit 1
	    }
	) || exit 1
    else
	echo "WARNING: can't run $_setup"
    fi
    if user_check $_user $_type; then 
	for _lander in $_landers; do
	    local _dir=${LANDER_TOPDIR_U}/${_user}/incoming_${_type}/${_lander}
	    if [ -d $_dir ]; then
		if [ -e $_dir/$LANDER_DELIVERYENABLEDFILE ]; then
		    echo "WARNING: $_type data delivery from $_lander already enabled"
		else
		    local _err=`touch $_dir/$LANDER_DELIVERYENABLEDFILE 2>&1`
		    if [ -e $_dir/$LANDER_DELIVERYENABLEDFILE ]; then
			echo "ENABLED $_type data delivery from $_lander"
		    else
			echo "ERROR: FAILED to enabled delivery: $_err"
		    fi
		fi
	    else
		echo "ERROR: directory $_dir does not exist (perhaps you forgot to run user_setup.sh?)"
	    fi
	done
    fi
}

delivery_stop() {
    local _user=$1
    local _type=$2
    local _landers=$3
    local _lander
    if user_check $_user $_type; then 
	for _lander in $_landers; do
	    local _dir=${LANDER_TOPDIR_U}/${_user}/incoming_${_type}/${_lander}
	    if [ -d $dir ]; then
		if [ -e $_dir/$LANDER_DELIVERYENABLEDFILE ]; then
		    local _err=`/bin/rm -f $_dir/$LANDER_DELIVERYENABLEDFILE 2>&1`
		    if [ -e $_dir/$LANDER_DELIVERYENABLEDFILE ]; then
			echo "ERROR: FAILED to disable $_type data delivery from $_lander: $_err"
		    else
			echo "DISABLED $_type data delivery from $_lander"
		    fi
		else
		    echo "WARNING: $_type data delivery from $_lander already disabled"
		fi
	    else
		echo "ERROR: directory $dir does not exist (perhaps you forgot to run user_setup.sh?)"
	    fi
	done
    fi
}

delivery_status() {
    local _user=$1
    local _type=$2
    local _landers=$3
    local _lander
    if user_check $_user $_type; then 
	for _lander in $_landers; do
	    local _dir=${LANDER_TOPDIR_U}/${_user}/incoming_${_type}/${_lander}
	    if [ -d $_dir ]; then
		if [ -e $_dir/$LANDER_DELIVERYENABLEDFILE ]; then
		    echo "STATUS: $_type data delivery from $_lander is ENABLED"
		else
		    echo "STATUS: $_type data delivery from $_lander is DISABLED"
		fi
	    else
		echo "ERROR: directory $_dir does not exist (perhaps you forgot to run user_setup.sh?)"
	    fi
	done
	
    fi
}

#MAIN
hookedqueues=
for hook in $LANDER_RAW_HOOKS; do
    [ -n "$hookedqueues" ] && hookedqueues+="|"
    hookedqueues+=$hook
done
allqueues=$hookedqueues
[ -n "$allqueues" ] && allqueues+="|"
allqueues+="raw|host|full|flow"

#check for options
username=$LOGNAME
while getopts "hu:" opt; do
    case $opt in
	( h ) usage; exit 0; ;;
	( u ) username=$OPTARG; ;;
    esac
done

shift $((OPTIND-1))

#main: check first argument
action=$1
case "$action" in
    ( start|stop|status ) 
       action=$1 	;;
    ( * ) 
    usage; exit 1 
  	;;
esac

#check next argument
shift
datatype=$1
eval "case '$datatype' in
    ( $allqueues ) : ;;
    ( * ) usage; exit 1 ;;
esac"

# check last args
shift
landers=$@
if [ -z "$landers" ] || [ "$landers" == "all" ]; then
    landers="$LANDER_SOURCES"
else
    #see if they make sense
    LANDERS="$LANDER_SOURCES "
    for l in $landers; do
	if [ "${LANDERS/$l /...}" == "$LANDERS" ]; then
	    #substitution wasn't made, this is an invalid lander name
	    echo "ERROR: Unknown lander $l (must be one of: \"$LANDER_SOURCES\")"
	    exit 1
	fi
    done
fi

delivery_$action $username $datatype "$landers"

