#!/bin/bash

#make sure all directories exist and have right permissions

# $Id$

LANDERCONF=~plander/lander.conf

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

usage () {
    cat >&2 <<EOF
$0: setup LANDER directory structure
Usage:
	$0 <lander> [-y|-n|-f|-r|-Q <data_type>|-q]

If <lander> is given, verifies (and possibly fixes) 
directory structure for <lander> data collection.  
<lander> can be a '*' for all currently defined LANDERS
in the config file (note that '*' must be quoted to avoid
shell expansion, e.g.
    $0 \*

If no argument given, sets up directory structure for the 
calling user.

where options are
        -y      - asnwer "yes" to all prompts
        -n      - asnwer "no" to all prompts
        -r      - enable raw queue
        -f	- enable flow queue
        -q      - quietly check status of directory setup
        -Q $allqueues
                - check/setup a particular queue

EOF
}

error() {
	echo "$1" >&2
	exit 1
}

quiet=
queue=
setup_dir () {
    local grp=$1
    local mod=$2
    shift 2
    local DIRS=$@ 
    local dir rights group

    for dir in $DIRS; do
	dir="$dir/"
	if [ ! -d $dir ]; then
	    [ -n "$quiet" ] && exit 1
	    echo -n "Dir $dir doesn't exist, fix? ([y]/n/q) "
	    if [ -z "$ALL" ]; then
		read yn
	    else
		yn=$ALL
		echo "$yn"
	    fi
	    case "$yn" in
		( "" | [yY]* )	mkdir $dir && chgrp $grp $dir && chmod -c $mod $dir ;;
		( [qQ]*  )	exit 0 ;;
		( * ) 		exit 1 ;;
	    esac
	fi
	read rights group < <(stat -c "%a %G" $dir)
	[ "$group" == "$grp" -a "${rights: -3}" == "${mod: -3}" ] && continue #nothing to do with this dir

	if [ "$group" != "$grp" ]; then
	    [ -n "$quiet" ] && exit 1
	    echo -n "Dir $dir isn't group owned by $grp, fix ([y]/n/q) "
	    if [ -z "$ALL" ]; then
		read yn
	    else
		yn=$ALL
		echo "$yn"
	    fi
	    case "$yn" in
		( "" | [yY]* )	chgrp $grp $dir || { 
		    echo "ERROR: Cannot use group $grp (not a member?)"
		    exit 1
		} ;;
		( [qQ*] )	exit 0 ;;
		( * )		exit 1 ;;
	    esac
	fi
	#quietly change permissions
	(( rights != mod )) && chmod -c $mod $dir || {
	    echo "ERROR: Cannot change permissions of $dir"
	    exit 1
	}
    done
}

setup_raw_tmp () {
    local _lander=$1
    setup_dir $LANDER_GROUP_RAW 0700 $LANDER_RAWDIR/$_lander $LANDER_TMPDIR/$_lander
}

setup_user_queue () {
    local group=$1 type=$2
    MYDIR=${LANDER_USERDIR} #MYDIR is caller's cluster directory
    setup_dir $group 0750 $MYDIR/incoming_${type}
    setup_dir $group 0750 $MYDIR/inprocess_${type}
    for lander in $LANDER_SOURCES; do 
	setup_dir $group 0770 $MYDIR/incoming_${type}/$lander
	setup_dir $group 0770 $MYDIR/inprocess_${type}/$lander
    done
}

setup_user_main () {
    MYDIR=${LANDER_USERDIR} #MYDIR is caller's cluster directory
    setup_dir $LANDER_GROUP_MAIN 0750 $MYDIR
}

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

while getopts "ynQ:qrfs" opt; do
    case $opt in
	( y ) ALL="y"  ;;
	( n ) ALL="n"  ;;
	( r ) raw="r"  ;;
	( f ) flow="f" ;;
	( Q ) queue=$OPTARG ;;
	( q ) quiet="yes" ;;
	( * ) usage; exit 1 ;;
    esac
done
shift $((OPTIND-1))

landers=$@
if [ -n "$landers" ]; then
    #check/update system setup
    [ "$landers" == "*" ] && landers=$LANDER_SOURCES
    for l in $landers; do
	setup_raw_tmp $l
    done
else
    setup_user_main
    if [ -n "$queue" ]; then
	eval "case $queue in
	    ( $allqueues )
	    group=\$LANDER_GROUP_$(echo $queue | tr '[:lower:]' '[:upper:]')
	    [ -z '\$group' ] && error 'No group set for hooked queue \$queue'
	    setup_user_queue \$group \$queue
	    ;;
	    
	    ( * ) error 'Unknown queue: $queue' ;;
	esac"
    else 
	setup_user_queue $LANDER_GROUP_HOST host
	setup_user_queue $LANDER_GROUP_FULL full
	[ -n "$raw"  ] && setup_user_queue $LANDER_GROUP_RAW  raw
	[ -n "$flow" ] && setup_user_queue $LANDER_GROUP_FLOW flow
    fi
fi
