# common LANDER shell functions

lander_set_DATE() {
    DATE=$(date "+%F %R")
}

lander_check_dagsnap_lander() {
    /bin/ps -C $LANDER_DAGSNAP >/dev/null
    return $?
}

lander_set_LOAD_local() {
    #get the loads
    local _uptime=$(uptime)
    _uptime=${_uptime/*load average:/}
    _uptime=${_uptime//,/}
    read LOAD1 LOAD5 LOAD15 < <(echo $_uptime)
    LOAD=$LOAD1
}

lander_set_JOBS_ALL_local() {
    local _user=$1
    JOBS_ALL=0
    while read state psname; do
	[ "$psname" == "$LANDER_DAGSNAP" ] && continue
	[ "$psname" == "ps" ] && continue
	[ "$psname" == "monitor.sh" ] && continue
	[ "$psname" == "bash" ] && continue
	let '++JOBS_ALL'
	#[ "$state" == "R" ] && let '++load_local_procsR'
    done < <(/bin/ps --no-headers -U $_user -u $_user -o state,comm)
}

lander_set_JOBS_RUNNING_local() {
    local _user=$1 _job=$2 _script=$3
    local _jobname=${_job##*/}
    local _u _shell _s _n _user_check=false
    
    JOBS_RUNNING=0
    [ "$_user" == '-' ] && _user_check=true
    
    if [ -n "$_script" ]; then
        while read _u _shell _s _n ; do
	        [ "$_n" != "${_n%$_script}" ] && \
		        { $_user_check || [ "$_u" == "$_user" ]; } && \
		        let '++JOBS_RUNNING'
    	done < <(/bin/ps -ww --no-headers -C $_jobname -o user,cmd)
    else
        while read _u _s _n ; do
	    { $_user_check || [ "$_u" == "$_user" ]; } && \
		let '++JOBS_RUNNING'
    	done < <(/bin/ps --no-headers -C $_jobname -o user)
    fi
}

lander_set_USERJOBS_RUNNING_local() {
    USERJOBS_RUNNING=$(/bin/ps --no-headers -C $LANDER_USER_WORKER_NAME -o state|wc -l)
}

lander_create_job_local() {
    local _job=$1 _args=$2 _nice=$3
    _nice=${_nice:=0}
    nice -n $_nice -- $_job $_args &
}

lander_set_LOAD_hpcc() {
    #no load abstraction for hpcc
    LOAD=""
}

lander_set_JOBS_ALL_hpcc() {
    local _user=$1
    JOBS_ALL=$(qselect -q @hpc-pbs -u $_user | wc -l)
}

lander_set_JOBS_RUNNING_hpcc() {
    local _user=$1 _job=$2 _script=$3
    local _jobname=${_job##*/}

    if [ -z "$_script" ]; then
	JOBS_RUNNING=$(qselect -u $_user -N $_jobname -s QRWHT | wc -l)
    else
	JOBS_RUNNING=$(
	    for j in $(qselect -u $_user -N $_jobname -s QRWHT); do
		qstat -f $j | sed -n '/submit_args/,$ {s/\t//; H}; $ {x; s/\n//g; p}'
	    done | grep $_script | wc -l
	)
    fi
}

lander_set_USERJOBS_RUNNING_hpcc() {
    USERJOBS_RUNNING=$(qselect -N $LANDER_USER_WORKER_NAME -s QRWHT | wc -l)
}

lander_create_job_hpcc() {
    local _job=$1 _args=$2 _nice=$3 #ignored
    local _jobname=${_job##*/}
    #_jobname=${_jobname:0:15}
    qsub -N $_jobname -z -M $LOGNAME "$_job" -v WORKER_ARGS="$_args"
}

#quotas don't quite work because doesn't count incoming files owned by plander
#so we approximate by adding files in incoming
lander_set_USER_DISK_USAGE_hpcc() {
    local _uname=$1
    USER_DISK_USAGE=""
    local _quota=$({ /usr/usc/bin/showquota -u $_uname -K -f $LANDER_TOPDIR || echo "0"; } | \
	 	   sed -n '$ { s/\([0-9][0-9]*\).[0-9]*K/{\1}/;s/.*{//; s/}.*//; p;}')
#    local _quota=$(/usr/usc/bin/showquota -u $_uname -K -f $LANDER_TOPDIR | sed -n '$ { s/\([0-9][0-9]*\).[0-9]*K/{\1}/;s/.*{//; s/}.*//; p;}')
    if [ "$?" == "0" ]; then
        USER_DISK_USAGE=$((_quota * 1024))
    else
        echo "Error processing showquota output: $_quota" >&2
        USER_DISK_USAGE=0
    fi
    local _nfiles_host=$(/bin/ls -1 $LANDER_TOPDIR/$_uname/incoming_host/* 2>/dev/null | grep -v '\(:\|^\)$' | wc -l)
    local _nfiles_full=$(/bin/ls -1 $LANDER_TOPDIR/$_uname/incoming_full/* 2>/dev/null | grep -v '\(:\|^\)$' | wc -l)
    local _nfiles=$((_nfiles_host + _nfiles_full))
    USER_DISK_USAGE=$(( USER_DISK_USAGE + LANDER_OUTPUT_FILESIZE*_nfiles*1024*1024 ))
}

lander_set_USER_DISK_USAGE_local() {
    local _uname=$1
    local _du="du"
#    local _du="/usr/xpg4/bin/du" #use this one on hpcc
    local _du_output=$($_du -kxs $LANDER_TOPDIR/$_uname/ 2>/dev/null) #trailing slash needed for symlinks
    USER_DISK_USAGE=""
    if [ "$?" == "0" ]; then
        set -- $_du_output
        local _quota=$1
        USER_DISK_USAGE=$((_quota * 1024))
    fi
}

lander_set_JOBS_ALL() {
    lander_set_JOBS_ALL_$LANDER_TYPE $@
}
lander_set_JOBS_RUNNING() {
    lander_set_JOBS_RUNNING_$LANDER_TYPE $@
}
lander_set_USERJOBS_RUNNING() {
    lander_set_USERJOBS_RUNNING_$LANDER_TYPE $@
}
lander_set_USER_DISK_USAGE() {
    lander_set_USER_DISK_USAGE_$LANDER_TYPE $@
}
lander_set_LOAD() {
    lander_set_LOAD_$LANDER_TYPE $@
}
lander_create_job() {
    local _job=$1 _args=$2 _nice=$3
    lander_create_job_$LANDER_TYPE "$_job" "$_args" "$_nice" 
}
lander_user_subscribed() {
    local _user=$1 _queue=$2 _datasource=$3
    /bin/ls $LANDER_TOPDIR_U/$_user/incoming_$_queue/$_datasource/$LANDER_DELIVERYENABLEDFILE >& /dev/null
}
#sets active_<userstype>_<datasource> list of usernames
lander_active_users() {
    local _userstype=$1
    local _user= _quota= _usage= _rest= _f= _x= _datasource=
    local _userlist=""
    local _group=${LANDER_GROUPS[$_userstype]}

    for _datasource in $LANDER_SOURCES; do
        eval active_${_userstype}_${_datasource}=""
    done

    while read _user _quota _usage _rest; do
        [ -n "${_user%%#*}" ] || continue
        if [ -z "$_user" -o -z "$_quota" -o -z "$_usage" ]; then
            echo "Bad quota for user $_user: ($_quser $_quota, $_usage)"
            continue
        fi
        [ $_quota -gt $_usage ] || continue #overquota

        echo " $(groups $_user) " | grep -q " $_group " || continue #not in the group

        #now check if subscribed
        for _f in $LANDER_TOPDIR_U/$_user/incoming_${_userstype}/*/$LANDER_DELIVERYENABLEDFILE; do
            [ -e $_f ] || continue
            _x=${_f%/*}
            _datasource=${_x##*/}
            local _active_${_userstype}_${_datasource}
            eval _active_${_userstype}_${_datasource}+="\ $_user"
            eval  active_${_userstype}_${_datasource}=\$_active_${_userstype}_${_datasource}
        done
    done < <(grep -v ^# $LANDER_QUOTAFILE)
}

lander_raw_cat() {
    eval $LANDER_COMPRESS_RAW_CAT $@
}

lander_usr_compress_stdin() {
    eval $LANDER_COMPRESS_USR_COMPRESS
}
