#!/bin/sh
#
# ------------------------------------------------------------------
#  show_family.sh
#
#  October 2010, Michael C. Scott
#
#  Copyright (c) 2010-2012 by Cisco Systems, Inc.
#  All rights reserved.
# ------------------------------------------------------------------


# Prints a header before iterating over the specified nodes (printing a node
# header if it iterates over more than one node). Within each node it iterates
# over the processes on the node. For each process the process name,
# session id, parent group and parent PID are retrieved. Appropriate fields are
# converted into JIDs before printing the results.


# SH_PROC_FAMILY_FORMAT_STRING
#
# The format string used when printing information.
SH_PROC_FAMILY_FORMAT_STRING="%-5.5s %-15.15s %-8.8s %-8.8s %-8.8s %-8.8s %-8.8s\n"


# Load the show_proc / pidin library.
source `dirname $0`/sh_proc_lib


function main
{
    local node_list=""
    local process_list=""    

    printf "$SH_PROC_FAMILY_FORMAT_STRING" \
           "pid" "name" "session" "pgrp" "ppid" "sibling" "child"

    # Generate a list of nodes.
    sh_proc_node_iterator
    node_list=("${sh_proc_node_iterator_out[@]}")

    # Iterate over each node.
    for node in ${node_list[@]}; do
        # If there are multiple nodes we print a header for each node.
        if [ ${#node_list[@]} -gt 1 ]; then
            sh_proc_print_node_header $node
        fi
        # Set the path for the /proc filesystem of the current node.
        sh_proc_set_proc_path $node    

        # Genereate a list of processes.    
        sh_proc_process_iterator $node
        process_list=($sh_proc_process_iterator_out)

        # Generate a list of JIDs from the process list.
        #sh_proc_pid_list_to_jid_list_in=("${process_list[@]}")
        #sh_proc_pid_list_to_jid_list
        #jid_list=($sh_proc_pid_list_to_jid_list_out)

        # Iterate over each process.
        local index=0
        while [ $index -lt ${#process_list[@]} ]; do
            local process=${process_list[$index]}

            # Retrieve the required information.
            sh_proc_get_item  $process "ppid"
            ppid_out=$sh_proc_get_item_out
 
            sh_proc_get_item $process "pgroup"
            pgroup_out=$sh_proc_get_item_out

            sh_proc_get_item $process "basename15"
            name_out=$sh_proc_get_item_out

            sh_proc_get_item $process "session"
            session_out=$sh_proc_get_item_out    

            #Convert ppid, pgroup and session to JIDs
        #    local jid_conversion=(`sh_proc_pid_jid_conversion p2j $session_out $pgroup_out $ppid_out`)
        #    session_out=${jid_conversion[0]}
        #    pgroup_out=${jid_conversion[1]}
        #    ppid_out=${jid_conversion[2]}

            # If the information was succesfully retrieved print it. If the
            # PID could not be converted to a JID print an apporpriate
            # error message.
        #    if [ $sh_proc_get_item_error -eq 0 ]; then
        #        if [ ${jid_list[$index]} == "-1" ]; then
        #            printf "Failed to get jobid from pid\n"
        #        else
                    printf "$SH_PROC_FAMILY_FORMAT_STRING" \
                           "$process" "$name_out" "$session_out" \
                           "$pgroup_out" "$ppid_out" "0" "0"
        #        fi
        #    else
        #        sh_proc_get_item_error=0
        #    fi

            let index+=1
        done       
        printf "\n"
    done
}


sh_proc_command_parser "$@"
main
