#!/bin/ksh
#
# Script for investigating postponed post-installation jobs submitted
# using postrun
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2004-2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

export PATH=/usr/bin
LC_ALL=C
export LC_ALL
MYDIR=$(cd $(dirname $0); pwd)

postrun_root_found=no
if [ "$PKG_INSTALL_ROOT" != "" ]; then
    pkginfo -q -R $PKG_INSTALL_ROOT SUNWpostrun-root && postrun_root_found=yes
else
    pkginfo -q SUNWpostrun-root && postrun_root_found=yes
fi

if [ $postrun_root_found = no ]; then
    echo "ERROR: postrun-query cannot find SUNWpostrun-root"
    exit 1
fi

if [ "$PKG_INSTALL_ROOT" != "" ]; then
    POSTRUN_ROOT_BASEDIR=`pkginfo -R $PKG_INSTALL_ROOT -l SUNWpostrun-root \
	| grep BASEDIR: |  sed 's/BASEDIR:[ 	]*//' | sed 's/ *//'`
else
    POSTRUN_ROOT_BASEDIR=`pkginfo -l SUNWpostrun-root \
	| grep BASEDIR: |  sed 's/BASEDIR:[ 	]*//' | sed 's/ *//'`
fi

SPOOLDIR="$PKG_INSTALL_ROOT$POSTRUN_ROOT_BASEDIR/var/spool/postrun"

usage() {
    echo 'Usage: postrun-query [options]'
    echo
    echo 'Options:'
    echo '    -c <class>, --class <class>'
    echo '        Only consider jobs the belong to class <class>'
    echo
    echo '    -e, --exists'
    echo '        return 0 if spooled jobs exist 1 otherwise'
    echo
    echo '    -n, --count'
    echo '        print the number of spooled jobs only.  The default'
    echo '        behaviour is to list all jobs'
    echo
    echo '    -j <job>, --job <job>'
    echo '        display job number <job>'
    echo
    echo '    -h, -?, --help'
    echo '        Display this help'
    exit 1
}


postrun_query_count=no
postrun_query_class=
postrun_query_check_exists=no
postrun_query_job=

# process the command line
while [ $# -gt 0 ]; do
    case "$1" in
	-h|-\?|--help)
            usage
            ;;
        -e|--exists)
	    postrun_query_check_exists=yes
	    ;;
        -n|--count)
	    postrun_query_count=yes
	    ;;
	-j|--job)
	    opt="$1"
	    if [ $# == 0 ]; then
		    echo "postrun-query: error: argument expected after $opt"
		    exit 1
	    fi
	    shift
	    postrun_query_job="$1"
	    ;;
	-c|--class)
	    opt="$1"
	    if [ $# == 0 ]; then
		    echo "postrun-query: error: argument expected after $opt"
		    exit 1
	    fi
	    shift
	    postrun_query_class="$1\$"
	    ;;
	--)
	    break
	    ;;
	*)
	    echo "postrun: error: invalid argument: $1"
	    exit 1
	    ;;
    esac
    shift
done

# exit 0 if jobs exist
# exit 1 if no jobs exist
if [ $postrun_query_check_exists = yes ]; then
    grep "^class: $postrun_query_class" $SPOOLDIR/*.ctrl >/dev/null 2>&1 \
	    && exit 0 || exit 1
fi

# print the # of jobs
if [ $postrun_query_count = yes ]; then
    grep -l "^class: $postrun_query_class" $SPOOLDIR/*.ctrl 2>/dev/null \
	| wc -l
    exit 0
fi

# print info about a job
if [ "x$postrun_query_job" != x ]; then
    test -f $SPOOLDIR/$postrun_query_job.ctrl && \
	job_details="`cat $SPOOLDIR/$postrun_query_job.ctrl`" && \
	job_commands="`cat $SPOOLDIR/$postrun_query_job.cmd`" || {
	echo "postrun-query: job $postrun_query_job not found"
	exit 1
    }
    echo Job $postrun_query_job
    echo "Submitted on`echo "$job_details" | grep '^submit_time:' | cut -f2- -d:`"
    echo "Belongs to package(s):`echo "$job_details" | grep pkginst: | cut -f2 -d:`"
    classes=`echo "$job_details" | grep '^class:' | cut -f2 -d:`
    classes=`echo $classes | sed -e 's/ /,/g'`
    echo "Class(es): $classes"
    echo "---commands follow---"
    echo "$job_commands"
    echo "---end of commands---"
    exit 0
fi

# list the jobs
ctrls=`grep -l "^class: $postrun_query_class" $SPOOLDIR/*.ctrl 2>/dev/null`
test "x$ctrls" = x \
    && echo "No spooled jobs found." \
    || echo "Job #	Class(es)	Package(s)"
for ctrl in $ctrls; do
    nr=`basename $ctrl .ctrl`
    classes=`grep '^class:' $ctrl | cut -f2 -d:`
    classes=`echo $classes | sed -e 's/ /,/g'`
    pkginst=`grep '^pkginst:' $ctrl | cut -f2 -d:`
    echo "$nr	$classes	$pkginst"
done
