#!/bin/ksh

## ###################################################
## AKUTIL: Written by Adam Kirkpatrick October, 2011 (EMC)
## This script is used to query Networker BU & clone status 
## ###################################################
Version="Version-19.3.0"

menu_rtn()
{
	while true
	do
		clear
		view=0
		if [ "$(uname -a | grep -c "Linux")" -ne "0" ]
		then
			termsize=`stty --all | grep "rows" | awk '{print $5,$7}' | sed -n "s/;//"gp`
			ECHO="echo -e"
		elif [ "$(uname -a | grep -c "SunOS")" -ne "0" ]
		then
			termsize=`stty -a | grep "rows" | awk '{print $3,$6}' | sed -n "s/;//"gp`
			ECHO="echo"
		else
			$ECHO "UNKNOWN OS Type"; exit 1
		fi
		rows=`$ECHO "$termsize" | awk '{print $1}'`; cols=`$ECHO "$termsize" | awk '{print $2}'`
		rows=`expr $rows / 2`; rowpos=`expr $rows - 8`
		cols=`expr $cols / 2`; colpos=`expr $cols - 20`

		tput cup `expr $rowpos + 1` $colpos; $ECHO  "======================================="
		tput cup `expr $rowpos + 2` $colpos; $ECHO  "|$BLUE_BG$CYAN_FG        Networker-Util Menu          $DEF|"
		tput cup `expr $rowpos + 3` $colpos; $ECHO  "|            $GREEN_FG$Version$DEF"
		tput cup `expr $rowpos + 4` $colpos; $ECHO  "|=====================================|"
		tput cup `expr $rowpos + 5` $colpos; $ECHO  "|      1. b) Formatted Backup Report  |"
		tput cup `expr $rowpos + 6` $colpos; $ECHO  "|      2. c) Formatted Clone Report   |"
		tput cup `expr $rowpos + 7` $colpos; $ECHO  "|      3. B) Backup Report            |"
		tput cup `expr $rowpos + 8` $colpos; $ECHO  "|      4. C) Clone Report             |"
		tput cup `expr $rowpos + 9` $colpos; $ECHO  "|      5. A) Activity Monitor         |"
		tput cup `expr $rowpos + 10` $colpos; $ECHO  "|      6. M) Media Report             |"
		tput cup `expr $rowpos + 11` $colpos; $ECHO  "|      7. I) In-Progress Report       |"
		tput cup `expr $rowpos + 12` $colpos; $ECHO  "|      8. U) User Defined Report      |"
		tput cup `expr $rowpos + 13` $colpos; $ECHO  "|      9. s) BootStrap Report         |"
		tput cup `expr $rowpos + 14` $colpos; $ECHO  "|     10. H) Help                     |"
		tput cup `expr $rowpos + 15` $colpos; $ECHO  "|                                     |"
		tput cup `expr $rowpos + 16` $colpos; $ECHO "======================================="
		tput cup `expr $rowpos + 17` $colpos; $ECHO  "$CYAN_FG Select By # or Alpha Q=Quit:$DEF > \c"; read input1
		if [ "$input1" = "" ];then continue
		elif [ "$input1" = "q" -o "$input1" = "Q" ]
		then
			rm $TMP_PATH/nwutil*.tmp >/dev/null 2>&1
			exit 0
		elif [ "$input1" = "1" -o "$input1" = "b" ]
		then
			report1_rtn; if [ "$?" -eq "1" ];then continue; fi
		elif [ "$input1" = "2" -o "$input1" = "c" ]
		then
			report2_rtn; if [ "$?" -eq "1" ];then continue; fi
		elif [ "$input1" = "3" -o "$input1" = "B" ]
		then
			report3_rtn; if [ "$?" -eq "1" ];then continue; fi
		elif [ "$input1" = "4" -o  "$input1" = "C" ]
		then
			report4_rtn; if [ "$?" -eq "1" ];then continue; fi
		elif [ "$input1" = "5" -o  "$input1" = "A" -o "$input1" = "a" ]
		then
		        $ECHO "\n$RED_BG$WHITE_FG NOTE: $DEF$CYAN_FG You are about to launch the Networker monitor where the display is refreshed every few seconds. "
			$ECHO "\n$BLUE_BG$WHITE_FG Basic interactive functions are as follows; $DEF$GREEN_FG\n"
			$ECHO "        $GREEN_FG Q/q$DEF = Quit"
			$ECHO "        $GREEN_FG D/d$DEF = Hide or Show the DEVICE display area"
			$ECHO "        $GREEN_FG G/g$DEF = Hide or Show the GROUP display area"
			$ECHO "        $GREEN_FG L/l$DEF = Hide or Show the LOG display area"
			$ECHO "        $GREEN_FG P/p$DEF = Hide or Show the PENDING display area"
			$ECHO "        $GREEN_FG S/s$DEF = Hide or Show the SESSIONS display area"
			$ECHO "        $GREEN_FG Tab-Key$DEF = Highlight a given display area"
			$ECHO "        $GREEN_FG Up/Down Arrows$DEF = Scroll within a highlighted display area"
                        $ECHO "$DEF\n <Press Enter to Continue>: \c"; read
                        nsrwatch -s $Serverx
		elif [ "$input1" = "6" -o  "$input1" = "M" -o "$input1" = "m" ]
		then
			report6_rtn; if [ "$?" -eq "1" ];then continue; fi
		elif [ "$input1" = "7" -o  "$input1" = "I" -o "$input1" = "i" ]
		then
			menu_flag=7; report1_rtn; if [ "$?" -eq "1" ];then continue; fi
		elif [ "$input1" = "8" -o "$input1" = "U" -o "$input1" = "u" ]
		then
			menu_flag=8; report8_rtn; if [ "$?" -eq "1" ];then continue; fi
		elif [ "$input1" = "9" -o "$input1" = "S" -o "$input1" = "s" ]
		then
			echo "\n======================================================================================================="
			echo "$BLUE_BG$WHITE_FG                   Networker BootStrap Report Details                                                  "
			echo "         date     time    level    ssid      file   record   volume                                    $DEF"
			echo "======================================================================================================="
			mminfo -s $Serverx -B | sed -n "2,9999"p
			echo "\n======================================================================================================="
                        $ECHO "$DEF\n <Press Enter to Continue>: \c"; read
		elif [ "$input1" = "10" -o "$input1" = "H" -o "$input1" = "h" ]
		then
			clear
			$ECHO "$BLUE_BG$WHITE_FG$Version$DEF\n"
        		$ECHO "For Technical Support contact the author@:$CYAN_FG adam.kirkpatrick@emc.com$DEF\n"
        		$ECHO "$BOLD ATO Usage Syntax: $DEF\n"
        		$ECHO "$GREEN_FG   $CYAN_BG$GRAY_FG nwutil  [Networker-server-name]$DEF"
			$ECHO "\n This tool is intended to simplify the CLI usage for Networker by providing some common reports for verifying backup and clone"
			$ECHO " status while masking much of the syntax required. It's fully interactive and each report function saves its ouput to a file"
			$ECHO " with the name displayed for reference."
			$ECHO "\n The script may be installed on the Networker server or client and is reccommended to be placed in /bin. All report and temporary"
			$ECHO " files are maintained in /nsr/tmp and there are no user defined variables."
			$ECHO "\n$CYAN_BG$GRAY_FG Report Functions $DEF"
			$ECHO "\nTo select a report you may enter either the alpha character which for reports 1-4 is case senseitive or you can use the"
			$ECHO "numneric number associated with each. The first time used a discovery of the Networker configuration is performed and saved"
			$ECHO "making subsequent uses quicker. A refresh option is available to force a rediscover in situations where the configuration has changed."
			$ECHO "\n$GREEN_FG Backup Summary Report:$DEF Provides a backup summary report with a color coded header line containing various success and failure metrics."
			$ECHO "                        The color will be red when failures are detected, green when no errors and blue when no backups are available." 
			$ECHO "                        Prompts are made allowing you to select whether to view the various report categories available." 
			$ECHO "\n$GREEN_FG Clone Summary Report:$DEF  Provides a clone summary report with a color coded header line containing various success and failure metrics."
			$ECHO "                        The color will be red when failures are detected, green when no errors and blue when no backups are available." 
			$ECHO "                        Prompts are made allowing you to choose whether to view the various report categories available." 
			$ECHO "\n$GREEN_FG Backup Report:$DEF         Provides a backup report only showing success and failure lines and identifies clients with no backups."
			$ECHO "\n$GREEN_FG Clone Report:$DEF          Provides a clone report only showing success and failure lines and tifies clients with no backups."
			$ECHO "\n$GREEN_FG Monitor View:$DEF          Provides a method to view a running system similar to what's available in the NMC GUI monitor window."
			$ECHO "\n$GREEN_FG Media Report:$DEF          Provides a media report showing barcode and various other metrics related to media managment.$DEF" 
			$ECHO ""
			$ECHO "By touching file name$CYAN_FG $TMP_PATH/customize/nwutil.txt$DEF the various Networker command syntax being used will be displayed"
			$ECHO "\n<Press Enter to Continue>"; read
		else
			tput cup `expr $rowpos + 17` `expr $colpos + 32`; $ECHO -e "$RED_FG Input Invalid$DEF"; sleep 1
		fi
 	done
}

display_report_rtn()
{
# ===> Arg1 = report file name Arg2 = nodisplay
	mv $1 $1.tmp >/dev/null 2>&1
#  	clear
	$ECHO
	$ECHO "$header1" > $1
	title="$($ECHO "   $(head -1 $1.tmp)")"
	
	Title_="$($ECHO "$BLUE_BG$WHITE_FG$title                                                                             " | cut -b1-176)"
	$ECHO "$Title_$DEF" >> $1
	$ECHO "$header1" >> $1
	sed -n "2,99999"p $1.tmp | grep -n "^" | sed -n "s/:/) /"p >> $1
	if [ "$2" = "" ];then more $1; fi
	rm $1.tmp >/dev/null 2>&1
}

multi_prep_rtn()
{
# =====> prepare proper syntax for multiple group or clients to mminfo
	if [ "$($ECHO "$Grpx" | grep -c ",")" -gt "0" ]
	then
		Group_q_="$($ECHO "$Grpx" | sed -n "s/,/,group=/"gp)"
		Group_q="$($ECHO "$Group_q_" | sed -n "s/^ /group=/"p)"
	else
		Group_q="group=$Grpx"
	fi
	if [ "$($ECHO "$Clientx" | grep -c ",")" -gt "0" ]
	then
		Client_q_="$($ECHO "$Clientx" | sed -n "s/,/,client=/"gp)"
		Client_q="$($ECHO "$Client_q_" | sed -n "s/^ /client=/"p)"
	else
		Client_q="client=$Clientx"
	fi
	if [ "$Grpx" = "ALL" -a "$Clientx" = "ALL" ]
	then
		Query_opt=""; Queryx=""
	elif [ "$Grpx" != "ALL" -a "$Clientx" = "ALL" ]
	then
		Query_opt=" -q "; Queryx="$Group_q"
	elif [ "$Grpx" = "ALL" -a "$Clientx" != "ALL" ]
	then
		Query_opt=" -q "; Queryx="$Client_q"
	elif [ "$Grpx" != "ALL" -a "$Clientx" != "ALL" ]
	then
		Query_opt=" -q "; Queryx="$Group_q,$Client_q"
	fi
} 

report1_rtn()
{
# ====> Called by menu opt-1 reports backup status or menu option-7 for activity report only

	client_w=27; level_w=6; sscreate_w=17; sscomp_w=17; ssbrowse_w=12; ssretent_w=12; totalsize_w=14;nfiles_w=8; group_w=15; name_w=25

	select_rtn group
	if [ "$?" -eq "1" ];then return; fi
	select_rtn client "$Grpx"
	if [ "$?" -eq "1" ];then return; fi
	reportx="report1"
	clear
	$ECHO "$CYAN_FG Rendering mminfo report using$WHITE_FG Sort-Order=$Sortx Group=$Grpx Client=$Clientx$CYAN_FG, please wait ..."
	multi_prep_rtn
	if [ -f $TMP_PATH/customize/nwutil.txt ]
	then
#		echo "$WHITE_FG DEBUG-CMD: mminfo -s $Serverx -o$Sortx -av -r \"client($client_w),ssid,ssflags,level($level_w),sscreate($sscreate_w),sscomp($sscomp_w),ssbrowse($ssbrowse_w),ssretent($ssretent_w),sumsize($totalsize_w),nfiles($nfiles_w),group($group_w),name($name_w)\"$Query_opt\"$Queryx\" -t \"$Datex\"$DEF"
		echo "$WHITE_FG DEBUG-CMD: mminfo -s $Serverx -o$Sortx -av -r \"client($client_w),ssid,ssflags,level($level_w),sscreate($sscreate_w),sscomp($sscomp_w),ssbrowse($ssbrowse_w),ssretent($ssretent_w),totalsize,nfiles($nfiles_w),group($group_w),name($name_w)\"$Query_opt\"$Queryx\" -t \"$Datex\"$DEF"
	fi
 	mminfo -s $Serverx -o$Sortx -av -r "client($client_w),ssid,ssflags,level($level_w),sscreate($sscreate_w),sscomp($sscomp_w),ssbrowse($ssbrowse_w),ssretent($ssretent_w),totalsize($totalsize_w),nfiles($nfiles_w),group($group_w),name($name_w)"$Query_opt"$Queryx" -t "$Datex" > $TMP_PATH/nwutil-$reportx.rpt 
#  	mminfo -s $Serverx -o$Sortx -av -r "client($client_w),ssid,ssflags,level($level_w),sscreate($sscreate_w),sscomp($sscomp_w),ssbrowse($ssbrowse_w),ssretent($ssretent_w),sumsize,nfiles($nfiles_w),group($group_w),name($name_w)"$Query_opt"$Queryx" -t "$Datex" > $TMP_PATH/nwutil-$reportx.rpt 
set -

	touch $TMP_PATH/nwutil-$reportx-drinfo.rpt
	egrep "index:|bootstrap" $TMP_PATH/nwutil-$reportx.rpt > $TMP_PATH/nwutil-$reportx-drinfo.rpt
	drinfocnt="$(wc -l $TMP_PATH/nwutil-$reportx-drinfo.rpt | awk '{print $1}')"
	if [ "$drinfocnt" -gt "0" ]
	then
# ============> remove all index and bootstrap BU's from main report
		egrep -v "index:|bootstrap" $TMP_PATH/nwutil-$reportx.rpt > $TMP_PATH/nwutil-$reportx.rpt.tmp
		mv $TMP_PATH/nwutil-$reportx.rpt.tmp $TMP_PATH/nwutil-$reportx.rpt >/dev/null 2>&1
	fi

	display_report_rtn $TMP_PATH/nwutil-$reportx.rpt nodisplay
	clicnt="$(wc -l $TMP_PATH/nwutil-client.dat | awk '{print $1}')"

# ====> Identify clients in the list which have no BU's in time frame selected
	clinobucnt=0
	rm $TMP_PATH/nwutil-$reportx-nobu.rpt >/dev/null 2>&1
	for x in $(cat $TMP_PATH/nwutil-client.dat | awk -F+ '{print $2}')
	do
		if [ "$(grep -c "$x" $TMP_PATH/nwutil-$reportx.rpt)" -ne "0" ]
		then
			continue
		elif [ "$Clientx" = "ALL" -o "$Clientx" = "$x" ]
		then
			clinobucnt="$(expr $clinobucnt + 1)"
			$ECHO "Client-Has-No-Backup: $x" >> $TMP_PATH/nwutil-$reportx-nobu.rpt
		fi		
	done

	rm $TMP_PATH/nwutil-$reportx-failed.rpt >/dev/null 2>&1
	rm $TMP_PATH/nwutil-$reportx-active.rpt >/dev/null 2>&1
	rm $TMP_PATH/nwutil-$reportx-success.rpt >/dev/null 2>&1
	touch $TMP_PATH/nwutil-$reportx-failed.rpt >/dev/null
	touch $TMP_PATH/nwutil-$reportx-active.rpt >/dev/null
	touch $TMP_PATH/nwutil-$reportx-success.rpt >/dev/null
	lastline="$(wc -l $TMP_PATH/nwutil-$reportx.rpt | awk '{print $1}')"
   	failchk="$(sed -n "4,99999"p $TMP_PATH/nwutil-$reportx.rpt | awk '{print $1"+"$2"+"$4}')"

	spcnt=0
 	for x in $($ECHO "$failchk")
 	do
		if [ "$spcnt" -gt "7" ]; then spcnt=0; fi	
		if [ "$(uname -a | grep -c "Linux")" -ne "0" ]
		then
			$ECHO -n "Formatting Report line [ $linenum of $lastline ], Please Wait...[${spin[$spcnt]}] \c"; $ECHO -n "\r"; spcnt=`expr $spcnt + 1`
		elif [ "$(uname -a | grep -c "SunOS")" -ne "0" ]
		then
  			$ECHO "Formatting Report line [ $linenum of $lastline ], Please Wait...[${spin[$spcnt]}]                                               \r\c"; spcnt=`expr $spcnt + 1`
		fi

# ============> Establish various reports based on success, failure and active status
		flags="$($ECHO "$x" | awk -F+ '{print $NF}')"
		if [ "$($ECHO "$flags" | egrep -c "i|a")" -eq "1" -a "$($ECHO "$flags" | egrep -c "F")" -ge "1" -a "$($ECHO "$flags" | egrep -c "r|I")" -eq "1" ]
		then
			linenum="$($ECHO "$x" | awk -F+ '{print $1}' | sed -n "s/)//"p)"
			linenum="$(expr $linenum + 3)"
			sed -n "$linenum,$linenum"p $TMP_PATH/nwutil-$reportx.rpt >> $TMP_PATH/nwutil-$reportx-failed.rpt
		fi
		if [ "$($ECHO "$flags" | egrep -c "i|I")" -eq "1" -a "$($ECHO "$flags" | egrep -c "a|F|r")" -eq "0" ]
		then
			linenum="$($ECHO "$x" | awk -F+ '{print $1}' | sed -n "s/)//"p)"
			linenum="$(expr $linenum + 3)"
			sed -n "$linenum,$linenum"p $TMP_PATH/nwutil-$reportx.rpt >> $TMP_PATH/nwutil-$reportx-active.rpt
		fi
		if [ "$($ECHO "$flags" | egrep -c "i|I")" -eq "0" -a "$($ECHO "$flags" | egrep -c "v")" -eq "1" -a "$($ECHO "$flags" | egrep -c "F")" -eq "1" ]
		then
			linenum="$($ECHO "$x" | awk -F+ '{print $1}' | sed -n "s/)//"p)"
			linenum="$(expr $linenum + 3)"
			sed -n "$linenum,$linenum"p $TMP_PATH/nwutil-$reportx.rpt >> $TMP_PATH/nwutil-$reportx-success.rpt
		fi
 	done

  	failcnt="$(wc -l $TMP_PATH/nwutil-$reportx-failed.rpt | awk '{print $1}')"
  	activecnt="$(wc -l $TMP_PATH/nwutil-$reportx-active.rpt | awk '{print $1}')"
  	successcnt="$(wc -l $TMP_PATH/nwutil-$reportx-success.rpt | awk '{print $1}')"
	
	scnt="$(sed -n "4,9999"p $TMP_PATH/nwutil-$reportx.rpt | awk '{print $3}' | sort -u | wc -l | awk '{print $1}')"
	if [ "$($ECHO "$Clientx"| grep -c "ALL")" -ne  "0" -a "$(grep -c "$Clientx" $TMP_PATH/nwutil-$reportx.rpt)" -eq "0" ]
	then
		clibucnt=0
	fi
	sscnt="$(expr $(wc -l $TMP_PATH/nwutil-$reportx.rpt | awk '{print $1}') - 3)"
	clibucnt="$(sed -n "4,9999"p $TMP_PATH/nwutil-$reportx.rpt | awk '{print $1}' | sort -u | wc -l | awk '{print $1}')"
	
	if [ "$menu_flag" -eq "7" ]
	then
		$ECHO "$header1"; $ECHO "$Title_ $DEF"; $ECHO "$header1"
  		awk -F\) '{print $2}' $TMP_PATH/nwutil-$reportx-active.rpt | grep -n "^" | sed -n "s/:/) /"p > $TMP_PATH/nwutil-$reportx-active.rpt.tmp
		color_flag=1; display_color $TMP_PATH/nwutil-$reportx-active.rpt.tmp
		rm $TMP_PATH/nwutil-$reportx-active.rpt.tmp >/dev/null 2>&1
		menu_flag=0; return
	fi

# ====> Prepare summary header line
# 	clear
	$ECHO "\n$header1"
	if [ "$failcnt" -gt "0" ]
	then
		linex="$($ECHO "$RED_BG$WHITE_FG BACKUP-STATUS-SUMMARY:$BLUE_BG Client-Cnt=$clicnt Success-Cnt=$successcnt Active-Cnt=$activecnt Failed-Cnt=$failcnt Client-No-Backup-Cnt=$clinobucnt Save-Set-Cnt=$sscnt Bootstrap|Index=$drinfocnt                                                                    " | cut -b1-182)"
	elif [ "$sscnt" -eq "0" ]
	then
		linex="$($ECHO "$BLUE_BG$YELLOW_FG BACKUP-STATUS-SUMMARY:$BLUE_BG$WHITE_FG Client-Cnt=$clicnt Success-Cnt=$successcnt Active-Cnt=$activecnt Failed-Cnt=$failcnt Client-No-Backup-Cnt=$clinobucnt Save-Set-Cnt=$sscnt  Bootstrap|Index=$drinfocnt                                                        " | cut -b1-182)"
	else
		linex="$($ECHO "$GREEN_BG$WHITE_FG BACKUP-STATUS-SUMMARY:$BLUE_BG Client-Cnt=$clicnt Success-Cnt=$successcnt Active-Cnt=$activecnt Failed-Cnt=$failcnt Client-No-Backup-Cnt=$clinobucnt Save-Set-Cnt=$sscnt Bootstrap|Index=$drinfocnt                                                                  " | cut -b1-182)"
	fi

# ====> Prompt user as to whether to display the various reports
	$ECHO "$linex$DEF"
	$ECHO "$header1"
 	if [ "$activecnt" -gt "0" ]
 	then
		$ECHO "\n$CYAN_FG Would you like to review the client names with$GREEN_FG ACTIVE [$activecnt]$CYAN_FG backups? Y/N Def=No Q=Quit >$DEF \c"; read inputx
 		if [ "$inputx" = "Y" -o "$inputx" = "y" ]
		then
			$ECHO "$header1"; $ECHO "$Title_ $DEF"; $ECHO "$header1"
  			awk -F\) '{print $2}' $TMP_PATH/nwutil-$reportx-active.rpt | grep -n "^" | sed -n "s/:/) /"p > $TMP_PATH/nwutil-$reportx-active.rpt.tmp
			color_flag=1; display_color $TMP_PATH/nwutil-$reportx-active.rpt.tmp
			rm $TMP_PATH/nwutil-$reportx-active.rpt.tmp >/dev/null 2>&1
		elif [ "$inputx" = "q" -o "$inputx" = "Q" ];then return; fi
	fi
 	if [ "$failcnt" -gt "0" ]
 	then
 		$ECHO "\n$CYAN_FG Would you like to review the client names with$RED_FG FAILED [$failcnt]$CYAN_FG backups? Y/N Def=No Q=Quit >$DEF \c"; read inputx
 		if [ "$inputx" = "Y" -o "$inputx" = "y" ]
		then
			$ECHO "$header1"; $ECHO "$Title_ $DEF"; $ECHO "$header1"
  			awk -F\) '{print $2}' $TMP_PATH/nwutil-$reportx-failed.rpt | grep -n "^" | sed -n "s/:/) /"p > $TMP_PATH/nwutil-$reportx-failed.rpt.tmp
			color_flag=0; display_color $TMP_PATH/nwutil-$reportx-failed.rpt.tmp 
			rm $TMP_PATH/nwutil-$reportx-failed.rpt.tmp >/dev/null 2>&1
		elif [ "$inputx" = "q" -o "$inputx" = "Q" ];then return; fi
 	fi
	if [ "$clinobucnt" -gt "0" ]
	then
		$ECHO "\n$CYAN_FG Would you like to review the client names with$YELLOW_FG NO [$clinobucnt]$CYAN_FG backups? Y/N Def=No Q=Quit >$DEF \c"; read inputx
		if [ "$inputx" = "Y" -o "$inputx" = "y" ]
		then
			grep -n "^" $TMP_PATH/nwutil-$reportx-nobu.rpt | sed -n "s/:/) /"p > $TMP_PATH/nwutil-$reportx-nobu.rpt.tmp
			color_flag=2; display_color $TMP_PATH/nwutil-$reportx-nobu.rpt.tmp
			rm $TMP_PATH/nwutil-$reportx-nobu.rpt.tmp >/dev/null 2>&1
		elif [ "$inputx" = "q" -o "$inputx" = "Q" ];then return; fi
	fi
	if [ "$drinfocnt" -gt "0" ]
	then
		$ECHO "\n$CYAN_FG Would you like to review the $YELLOW_FG BOOTSTRAP & INDEX [$drinfocnt]$CYAN_FG backups? Y/N Def=No Q=Quit >$DEF \c"; read inputx
		if [ "$inputx" = "Y" -o "$inputx" = "y" ]
		then
			$ECHO "$header1"; $ECHO "$Title_ $DEF"; $ECHO "$header1"
			grep -n "^" $TMP_PATH/nwutil-$reportx-drinfo.rpt | sed -n "s/:/) /"p > $TMP_PATH/nwutil-$reportx-drinfo.rpt.tmp
			color_flag=2; display_color $TMP_PATH/nwutil-$reportx-drinfo.rpt.tmp
			rm $TMP_PATH/nwutil-$reportx-drinfo.rpt.tmp >/dev/null 2>&1
		elif [ "$inputx" = "q" -o "$inputx" = "Q" ];then return; fi
	fi
	if [ "$successcnt" -gt "0" ]
	then
		$ECHO "\n$CYAN_FG Would you like to review the client names with$GREEN_FG SUCCESSFUL [$successcnt]$CYAN_FG backups? Y/N Def=No Q=Quit >$DEF \c"; read inputx
		if [ "$inputx" = "Y" -o "$inputx" = "y" ]
		then
			$ECHO "$header1"; $ECHO "$Title_ $DEF"; $ECHO "$header1"
			mv $TMP_PATH/nwutil-$reportx-success.rpt $TMP_PATH/nwutil-$reportx-success.rpt.tmp >/dev/null 2>&1
			color_flag=1; display_color $TMP_PATH/nwutil-$reportx-success.rpt.tmp
			rm $TMP_PATH/nwutil-$reportx-success.rpt.tmp >/dev/null 2>&1
			elif [ "$inputx" = "q" -o "$inputx" = "Q" ];then return; fi
	fi
	clone_flag=0; color_flag=0
	$ECHO "\n$CYAN_FG Would you like to review the$WHITE_FG COMPLETE$CYAN_FG report? Y/N Def=No >$DEF \c"; read inputx
	if [ "$inputx" = "Y" -o "$inputx" = "y" ]
	then
		more $TMP_PATH/nwutil-$reportx.rpt
		$ECHO "$DEF$header1"
		$ECHO "$BLUE_BG$WHITE_FG SS-Flag-Definitions: C=Continued E=Eligible-Recycle F=Finished i=Incomplete I=In-Progress k=Chk-Point-Restart K=Cover N=NDMP P=Snapshot r=Purged R=Raw v=Valid  $DEF"
		$ECHO "$DEF$header1"
		$ECHO "\n$DEF Report File: $TMP_PATH/nwutil-$reportx.rpt <Press Enter To Continue> \c"; read
	fi
}

report3_rtn()
{
# ====> Called by menu opt-3 reports backup status
	select_rtn group
	if [ "$?" -eq "1" ];then return; fi
	select_rtn client "$Grpx"
	if [ "$?" -eq "1" ];then return; fi
	reportx="report3"
	multi_prep_rtn
	clear
	$ECHO "Rendering mminfo report using Sort-Order=$Sortx Group=$Grpx Client=$Clientx, please wait ..."
	if [ -f $TMP_PATH/customize/nwutil.txt ]
	then
		echo "$WHITE_FG DEBUG-CMD: mminfo -s $Serverx -o$Sortx -av -r \"client(30),ssid,ssflags,sscomp(17),level(6),totalsize(14),nfiles(8),ssbrowse(12),ssretent(12),group(25),name(40)\"$Query_opt\"$Queryx\" -t \"$Datex\"$DEF"
#		echo "$WHITE_FG DEBUG-CMD: mminfo -s $Serverx -o$Sortx -av -r \"client(30),ssid,ssflags,sscomp(17),level(6),totalsize(14),nfiles(8),ssbrowse(12),ssretent(12),group(25),name(40)\"$Query_opt\"$Queryx\" -t \"$Datex\"$DEF"
	fi
#	mminfo -s $Serverx -o$Sortx -av -r "client(30),ssid,ssflags,sscomp(17),level(6),sumsize,nfiles(8),ssbrowse(12),ssretent(12),group(25),name(40)"$Query_opt"$Queryx" -t "$Datex" > $TMP_PATH/nwutil-$reportx.rpt
	mminfo -s $Serverx -o$Sortx -av -r "client(30),ssid,ssflags,sscomp(17),level(6),totalsize(14),nfiles(8),ssbrowse(12),ssretent(12),group(25),name(40)"$Query_opt"$Queryx" -t "$Datex" > $TMP_PATH/nwutil-$reportx.rpt
	display_report_rtn $TMP_PATH/nwutil-$reportx.rpt
	$ECHO "$DEF$header1"
	$ECHO "$BLUE_BG$WHITE_FG SS-Flag-Definitions: C=Continued E=Eligible-Recycle F=Finished i=Incomplete I=In-Progress k=Chk-Point-Restart K=Cover N=NDMP P=Snapshot r=Purged R=Raw v=Valid  $DEF"
	$ECHO "$DEF$header1"
	$ECHO "\n$DEF Report File: $TMP_PATH/nwutil-$reportx.rpt <Press Enter To Continue> \c"; read
}

report2_rtn()
{
# ====> Called by menu opt-2 reports clone status
	client_w=27; family_w=7; validcopies_w=0; ssid_w=0; sscomp_w=20; clonetime_w=24; clretent_w=12;clflags_w=6; group_w=25; name_w=40
	select_rtn group
	if [ "$?" -eq "1" ];then return; fi
	select_rtn client "$Grpx"
	if [ "$?" -eq "1" ];then return; fi
	reportx="report2"
	multi_prep_rtn
	clear
	$ECHO "Rendering mminfo report using Sort-Order=$Sortx Group=$Grpx Client=$Clientx, please wait ..."
	if [ -f $TMP_PATH/customize/nwutil.txt ]
	then
		echo "$WHITE_FG DEBUG-CMD: mminfo -s $Serverx -o$Sortx -av -r \"client($client_w),family($family_w),validcopies,clflags($clflags_w),ssid,sscomp($sscomp_w),clonetime($clonetime_w),clretent($clretent_w),group,name($name_w)\"$Query_opt\"$Queryx\" -t \"$Datex\"$DEF"
	fi
	mminfo -s $Serverx -o$Sortx -av -r "client($client_w),family($family_w),validcopies,clflags($clflags_w),ssid,sscomp($sscomp_w),clonetime($clonetime_w),clretent($clretent_w),group,name($name_w)"$Query_opt"$Queryx" -t "$Datex" > $TMP_PATH/nwutil-$reportx.rpt 

	display_report_rtn $TMP_PATH/nwutil-$reportx.rpt nodisplay
	clicnt="$(wc -l $TMP_PATH/nwutil-client.dat | awk '{print $1}')"
	clinobucnt=0
	for x in $(cat $TMP_PATH/nwutil-client.dat | awk -F+ '{print $2}')
	do
		if [ "$(grep -c "$x" $TMP_PATH/nwutil-$reportx.rpt)" -ne "0" ]
		then
			continue
		else
			clinobucnt="$(expr $clinobucnt + 1)"
			$ECHO "Client-Has-No-Backup: $x" >> $TMP_PATH/nwutil-$reportx-nobu.rpt
		fi		
	done

	rm $TMP_PATH/nwutil-$reportx-failed.rpt >/dev/null 2>&1
	rm $TMP_PATH/nwutil-$reportx-noclone.rpt >/dev/null 2>&1
	rm $TMP_PATH/nwutil-$reportx-success.rpt >/dev/null 2>&1
	rm $TMP_PATH/nwutil-$reportx-zerocopy.rpt >/dev/null 2>&1
	touch $TMP_PATH/nwutil-$reportx-failed.rpt >/dev/null
	touch $TMP_PATH/nwutil-$reportx-noclone.rpt >/dev/null
	touch $TMP_PATH/nwutil-$reportx-success.rpt >/dev/null
	touch $TMP_PATH/nwutil-$reportx-zerocopy.rpt >/dev/null
	lastline="$(wc -l $TMP_PATH/nwutil-$reportx.rpt | awk '{print $1}')" 
    	clonechk="$(sed -n "4,99999"p $TMP_PATH/nwutil-$reportx.rpt |awk '{print $1"+"$2"+"$3"+"$4"+"$5"+"$6}')"

	spcnt=0
  	for x in $($ECHO "$clonechk")
  	do
		if [ "$spcnt" -gt "7" ]; then spcnt=0; fi	
		if [ "$(uname -a | grep -c "Linux")" -ne "0" ]
		then
			$ECHO -n "Formatting Report line [ $linenum of $lastline ], Please Wait...[${spin[$spcnt]}] \c"; $ECHO -n "\r"; spcnt=`expr $spcnt + 1`
		elif [ "$(uname -a | grep -c "SunOS")" -ne "0" ]
		then
  			$ECHO "Formatting Report line [ $linenum of $lastline ], Please Wait...[${spin[$spcnt]}]                                       \r\c"; spcnt=`expr $spcnt + 1`
		fi
 		clone_cnt="$($ECHO "$x" | awk -F+ '{print $4}')"
 		clone_flags="$($ECHO "$x" | awk -F+ '{print $5}')"
 		media_type="$($ECHO "$x" | awk -F+ '{print $3}')"

 		if [ "$clone_cnt" -eq "0" ] 
 		then
 			linenum="$($ECHO "$x" | awk -F+ '{print $1}' | sed -n "s/)//"p)"
 			linenum="$(expr $linenum + 3)"
 			sed -n "$linenum,$linenum"p $TMP_PATH/nwutil-$reportx.rpt >> $TMP_PATH/nwutil-$reportx-zerocopy.rpt
		fi
 		if [ "$media_type" = "disk" -a "$clone_cnt" -ne "0" -a "$($ECHO "$clone_flags" | egrep -c "a|s|i")" -ne "0" ]
 		then
 			linenum="$($ECHO "$x" | awk -F+ '{print $1}' | sed -n "s/)//"p)"
 			linenum="$(expr $linenum + 3)"
 			sed -n "$linenum,$linenum"p $TMP_PATH/nwutil-$reportx.rpt >> $TMP_PATH/nwutil-$reportx-failed.rpt
		elif [ "$media_type" = "disk" -a "$clone_cnt" -gt "2"  -a "$($ECHO "$clone_flags" | egrep -c "a|s|i")" -eq "0" ]
		then
 			linenum="$($ECHO "$x" | awk -F+ '{print $1}' | sed -n "s/)//"p)"
 			linenum="$(expr $linenum + 3)"
 			sed -n "$linenum,$linenum"p $TMP_PATH/nwutil-$reportx.rpt >> $TMP_PATH/nwutil-$reportx-success.rpt
 		elif [ "$media_type" = "disk" -a "$clone_cnt" -eq "2" ]
 		then
 			linenum="$($ECHO "$x" | awk -F+ '{print $1}' | sed -n "s/)//"p)"
 			linenum="$(expr $linenum + 3)"
 			sed -n "$linenum,$linenum"p $TMP_PATH/nwutil-$reportx.rpt >> $TMP_PATH/nwutil-$reportx-noclone.rpt
		fi

 		if [ "$media_type" = "tape" -a "$clone_cnt" -ne "0" -a "$($ECHO "$clone_flags" | egrep -c "a|s|i")" -ne "0" ]
                then
 			linenum="$($ECHO "$x" | awk -F+ '{print $1}' | sed -n "s/)//"p)"
 			linenum="$(expr $linenum + 3)"
 			sed -n "$linenum,$linenum"p $TMP_PATH/nwutil-$reportx.rpt >> $TMP_PATH/nwutil-$reportx-failed.rpt
		elif [ "$media_type" = "tape" -a "$clone_cnt" -gt "1" -a "$($ECHO "$clone_flags" | egrep -c "a|s|i")" -eq "0" ]
		then
 			linenum="$($ECHO "$x" | awk -F+ '{print $1}' | sed -n "s/)//"p)"
 			linenum="$(expr $linenum + 3)"
 			sed -n "$linenum,$linenum"p $TMP_PATH/nwutil-$reportx.rpt >> $TMP_PATH/nwutil-$reportx-success.rpt
 		elif [ "$media_type" = "tape" -a "$clone_cnt" -eq "1" ]
                then
 			linenum="$($ECHO "$x" | awk -F+ '{print $1}' | sed -n "s/)//"p)"
 			linenum="$(expr $linenum + 3)"
 			sed -n "$linenum,$linenum"p $TMP_PATH/nwutil-$reportx.rpt >> $TMP_PATH/nwutil-$reportx-noclone.rpt
 		fi
  	done
  	failcnt="$(wc -l $TMP_PATH/nwutil-$reportx-failed.rpt | awk '{print $1}')"
  	noclonecnt="$(wc -l $TMP_PATH/nwutil-$reportx-noclone.rpt | awk '{print $1}')"
  	zerocnt="$(wc -l $TMP_PATH/nwutil-$reportx-zerocopy.rpt | awk '{print $1}')"
 	sscnt="$(sed -n "4,9999"p $TMP_PATH/nwutil-$reportx.rpt | awk '{print $3}' | sort -u | wc -l | awk '{print $1}')"
	clonecnt="$(wc -l $TMP_PATH/nwutil-$reportx-success.rpt | awk '{print $1}')"
	sscnt="$(expr $(wc -l $TMP_PATH/nwutil-$reportx.rpt | awk '{print $1}') - 3)"
	clear	
	$ECHO "\n$header1"
	if [ "$failcnt" -gt "0" ]
	then
		clear
		linex="$($ECHO "$RED_BG$WHITE_FG CLONE-STATUS-SUMMARY:$BLUE_BG Client-Cnt=$clicnt Clone-Success-Cnt=$clonecnt Clone-Failed-Cnt=$failcnt No-Clone=$noclonecnt Zero-Copy=$zerocnt Save-Set-Cnt=$sscnt                                                                     " | cut -b1-182)"
	elif [ "$noclonecnt" -gt "0" -o "$clonecnt" -gt "0" ]
	then
		clear
		linex="$($ECHO "$GREEN_BG$WHITE_FG CLONE-STATUS-SUMMARY:$BLUE_BG Client-Cnt=$clicnt Clone-Success-Cnt=$clonecnt Clone-Failed-Cnt=$failcnt No-Clone=$noclonecnt Zero-Copy=$zerocnt Save-Set-Cnt=$sscnt                                                                     " | cut -b1-182)"
	fi
	$ECHO "$linex$DEF"
	$ECHO "$header1"
 	if [ "$noclonecnt" -gt "0" ]
 	then
 		$ECHO "\n$CYAN_FG Would you like to review the client/saveset names with$YELLOW_FG NO [$noclonecnt]$CYAN_FG clones? Y/N Def=No Q=Quit >$DEF \c"; read inputx
 		if [ "$inputx" = "Y" -o "$inputx" = "y" ]
		then
			clone_flag=1
			$ECHO "$header1"; $ECHO "$Title_ $DEF"; $ECHO "$header1"
			color_flag=2; display_color $TMP_PATH/nwutil-$reportx-noclone.rpt
		elif [ "$inputx" = "q" -o "$inputx" = "Q" ];then return; fi	
 	fi
 	if [ "$zerocnt" -gt "0" ]
 	then
 		$ECHO "\n$CYAN_FG Would you like to review the client/saveset names with$YELLOW_FG NO-VALID[$zerocnt]$CYAN_FG copies? Y/N Def=No Q=Quit >$DEF \c"; read inputx
 		if [ "$inputx" = "Y" -o "$inputx" = "y" ]
		then
			clone_flag=1
			$ECHO "$header1"; $ECHO "$Title_ $DEF"; $ECHO "$header1"
			color_flag=2; display_color $TMP_PATH/nwutil-$reportx-zerocopy.rpt
		elif [ "$inputx" = "q" -o "$inputx" = "Q" ];then return; fi	
 	fi
 	if [ "$failcnt" -gt "0" ]
 	then
 		$ECHO "\n$CYAN_FG Would you like to review the client/saveset names with$RED_FG FAILED [$failcnt] $CYAN_FG clones? Y/N Def=No Q=Quit >$DEF \c"; read inputx
 		if [ "$inputx" = "Y" -o "$inputx" = "y" ]
		then
			clone_flag=1
			$ECHO "$header1"; $ECHO "$Title_ $DEF"; $ECHO "$header1"
			color_flag=0; display_color $TMP_PATH/nwutil-$reportx-failed.rpt
		elif [ "$inputx" = "q" -o "$inputx" = "Q" ];then return; fi	
 	fi
 	if [ "$clonecnt" -gt "0" ]
 	then
 		$ECHO "\n$CYAN_FG Would you like to review the client/saveset names with$GREEN_FG SUCCESSFUL [$clonecnt] $CYAN_FG clones? Y/N Def=No Q=Quit >$DEF \c"; read inputx
 		if [ "$inputx" = "Y" -o "$inputx" = "y" ]
		then
			clone_flag=1
			$ECHO "$header1"; $ECHO "$Title_ $DEF"; $ECHO "$header1"
			color_flag=1; display_color $TMP_PATH/nwutil-$reportx-success.rpt
		elif [ "$inputx" = "q" -o "$inputx" = "Q" ];then return; fi	
 	fi
	$ECHO "\n$CYAN_FG Would you like to review the$WHITE_FG COMPLETE$CYAN_FG report? Y/N Def=No >$DEF \c"; read inputx
	if [ "$inputx" = "Y" -o "$inputx" = "y" ]
	then
		more $TMP_PATH/nwutil-$reportx.rpt
		$ECHO "$DEF$header1"
		$ECHO "$BLUE_BG$WHITE_FG Clone-Flag-Definitions: a=Aborted E=Eligble-Recycle i=Incomplete o=Opaque-NDMP s=Suspect-Read-Error                                                              $DEF"
		$ECHO "$DEF$header1"
		$ECHO "\n$DEF Report File: $TMP_PATH/nwutil-$reportx.rpt <Press Enter To Continue> \c"; read
	fi
	clone_flag=0
}

display_color()
{
# ====> Display an error report in red similar to more, more cmd reverts to default color after first screen
#       $1 = file name	
	lline="$(wc -l $1 | awk '{print $1}')"; fline=1; rownum="$($ECHO "$termsize" | awk '{print $1}')"; lrow=$rownum
	rownum="$(expr $rownum - 2)"
	while true
	do
		if [ "$color_flag" -eq "1" ];then $ECHO "$GREEN_FG\c"
		elif [ "$color_flag" -eq "2"  ];then $ECHO "$YELLOW_FG\c"
		else $ECHO "$RED_FG\c"; fi
		awk -F\) '{print $2}' $1 | grep -n "^" | sed -n "s/:/) /"p | sed -n "$fline,$lrow"p

		if [ "$clone_flag" -eq "1" ]
		then
			$ECHO "$DEF$header1"
			$ECHO "$BLUE_BG$WHITE_FG Clone-Flag-Definitions: a=Aborted E=Eligble-Recycle i=Incomplete o=Opaque-NDMP s=Suspect-Read-Error                                                              $DEF"
		elif [ "$clone_flag" -eq "0" ]
		then
			$ECHO "$DEF$header1"
			$ECHO "$BLUE_BG$WHITE_FG SS-Flag-Definitions: C=Continued E=Eligible-Recycle F=Finished i=Incomplete I=In-Progress k=Chk-Point-Restart K=Cover N=NDMP P=Snapshot r=Purged R=Raw v=Valid  $DEF"
		fi
		$ECHO "$DEF$header1"
		$ECHO "\n$DEF Report File: $1 <Press Enter to Continue or Q=Quit> \c"; read input
		if [ "$input" = "Q" -o "$input" = "q" ];then return 1; fi
		fline="$(expr $lrow + 1)"
		lrow="$(expr $lrow + $rownum)"
		if [ "$fline" -gt "$lline" ];then break; fi
	done
}

report4_rtn()
{
# ====> Called by menu opt-4 reports clone status
	client_w=27; family_w=7; sscomp_w=20; clonetime_w=24;  clretent_w=12; group_w=25; clflags_w=6; name_w=40
	select_rtn group
	if [ "$?" -eq "1" ];then return; fi
	select_rtn client "$Grpx"
	if [ "$?" -eq "1" ];then return; fi
	reportx="report4"
	multi_prep_rtn
	if [ -f $TMP_PATH/customize/nwutil.txt ]
	then
		echo "$WHITE_FG DEBUG-CMD: mminfo -s $Serverx -o$Sortx -av -r \"client($client_w),family($family_w),validcopies,ssid,sscomp($sscomp_w),clonetime($clonetime_w),clretent($clretent_w),clflags($clflags_w),group,name($name_w)\"$Query_opt\"$Queryx\" -t \"$Datex\"$DEF"
	fi
	mminfo -s $Serverx -o$Sortx -av -r "client($client_w),family($family_w),validcopies,ssid,sscomp($sscomp_w),clonetime($clonetime_w),clretent($clretent_w),clflags($clflags_w),group,name($name_w)"$Query_opt"$Queryx" -t "$Datex" | grep -v .RO > $TMP_PATH/nwutil-$reportx.rpt
	clear
	$ECHO "Rendering mminfo report using Sort-Order=$Sortx Group=$Grpx Client=$Clientx, please wait ..."
	display_report_rtn $TMP_PATH/nwutil-$reportx.rpt
	$ECHO "$DEF$header1"
	$ECHO "$BLUE_BG$WHITE_FG Clone-Flag-Definitions: a=Aborted E=Eligble-Recycle i=Incomplete o=Opaque-NDMP s=Suspect-Read-Error                                                              $DEF"
	$ECHO "$DEF$header1"
	$ECHO "\n$DEF Report File: $TMP_PATH/nwutil-$reportx.rpt <Press Enter To Continue> \c"; read
}

report6_rtn()
{
# ====> Called by menu opt-6 media report
	client_w=27; volflags_w=2; volume_w=28; volaccess_w=10; volretent_w=10; family_w=7; barcode_w=8; group_w=25; pool_w=20; name_w=0
	select_rtn group
	if [ "$?" -eq "1" ];then return; fi
	select_rtn client "$Grpx"
	if [ "$?" -eq "1" ];then return; fi
	reportx="report6"
	multi_prep_rtn
	clear
	$ECHO "Rendering mminfo report using Sort-Order=$Sortx Group=$Grpx Client=$Clientx, please wait ..."
	if [ -f $TMP_PATH/customize/nwutil.txt ]
	then
		echo "$WHITE_FG DEBUG-CMD: mminfo -s $Serverx -o$Sortx -av -r \"client($client_w),volume($volume_w),volaccess($volaccess_w),volretent($volretent_w),family($family_w),barcode($barcode_w),volflags($volflags_w),group($group_w),pool($pool_w),name\"$Query_opt\"$Queryx\" -t \"$Datex\"$DEF"
	fi
	mminfo -s $Serverx -o$Sortx -av -r "client($client_w),volume($volume_w),volaccess($volaccess_w),volretent($volretent_w),family($family_w),barcode($barcode_w),volflags($volflags_w),group($group_w),pool($pool_w),name"$Query_opt"$Queryx" -t "$Datex" | grep -v ".RO " > $TMP_PATH/nwutil-$reportx.rpt
	$ECHO "\n$CYAN_FG Would you like to review the unique tape barcodes involved? Y/N Def=No >$DEF \c"; read inputx
	if [ "$inputx" = "Y" -o "$inputx" = "y" ]
	then
		$ECHO "$DEF$header1\n"
		grep " tape " $TMP_PATH/nwutil-$reportx.rpt | awk '{print $6}' | sort -u | grep -n "^" | sed -n "s/:/) /"gp | more
		$ECHO "\n$DEF Report File: $TMP_PATH/nwutil-$reportx.rpt <Press Enter To Continue> \c"; read
	fi
	$ECHO "\n$CYAN_FG Would you like to review the complete media report? Y/N Def=No >$DEF \c"; read inputx
	if [ "$inputx" = "Y" -o "$inputx" = "y" ]
	then
		display_report_rtn $TMP_PATH/nwutil-$reportx.rpt
		$ECHO "$DEF$header1"
		$ECHO "\n$DEF Report File: $TMP_PATH/nwutil-$reportx.rpt <Press Enter To Continue> \c"; read
	fi
}

report8_rtn()
{
# ====> Called by menu opt-8 user defined report
	$ECHO "\nThe following fields are available to include in a custom report. To include a field enter its number and optionally"
	$ECHO "include the desired number of columns to allocate to it by using(#). An example entry would be 1,2(30),3,4,5(12) etc."
	$ECHO "\nThe number shown in () after a field entry is the max columns required. Applying a column count is optional but provides"
	$ECHO "a more formatted look."

	$ECHO "$RED_BG$WHITE_FG This report is under construction $DEF"

	$ECHO "\n$CYAN_FG Client & Backup Related:\n$GREEN_FG"
	$ECHO "1. Client-Name           2. Browse-Time(9)       3. Save-Set-Ret(9)      4. Save-Set-Compl(9)    5. Group-Name(12)"
	$ECHO "6. Save-Set-Name(12)     7. Save-Set-Insertion   8. Total-Size(11)       9. File-Count(5)        10. Backup-Level(5)"
	$ECHO "11. SSet-Flags(7)        12. Save-Set-ID(10)     13.                     14.                     15.                "

	$ECHO "\n$CYAN_FG Media & Clone Related:\n$GREEN_FG"
	$ECHO "16. Barcode(15)          17. Volume Name(15)     18. Family-Media(4)     19. Volume-Flags(5)     20. Volume-State(7)"
	$ECHO "21. Clone-Flags(5)       22. Volume-Retention(9) 23. Volume-Access(9)    24. Valid-Copies        25. Pool-Name"

	if [ ! -s $TMP_PATH/nwutil-saveprofiles.dat ]
	then
		$ECHO "\n$CYAN_FG Enter desired fields by number in a comma separated string. or Q=Quit > $DEF\c"; read urptx_
	elif [ -s $TMP_PATH/nwutil-saveprofiles.dat ]
	then
		$ECHO "\n$CYAN_FG Enter desired fields by number in a comma separated string. S=Select-Profile or Q=Quit > $DEF\c"; read urptx_
		if [ "$urptx_" = "S" -o "$urptx_" = "s" ]
		then
    			echo 
			grep "\[U-" $TMP_PATH/nwutil-saveprofiles.dat |grep -n "^" | sed -n "s/:/) /"p | tee -a $TMP_PATH/nwutil-saveprofiles.dat.tmp
			$ECHO "$CYAN_FG\nSelect desired profile by number: or E=Edit Q=Quit :$DEF > \c"; read profilex
			if [ "$profilex" = "q" -o "$profilex" = "Q" -o "$profilex" = "" ];then return; fi
			if [ "$profilex" = "e" -o "$profilex" = "E" ]
			then
				vi $TMP_PATH/nwutil-saveprofiles.dat
				return
			fi
			Profilex="$(sed -n "$profilex,$profilex"p $TMP_PATH/nwutil-saveprofiles.dat.tmp)"
			string_rpt="$(echo "$Profilex" | awk -F\| '{print $6}')"
			Sortx="$(echo "$Profilex" | awk -F\| '{print $2}')"
			Datex="$(echo "$Profilex" | awk -F\| '{print $3}')"
			Grpx="$(echo "$Profilex" | awk -F\| '{print $4}')"
			Clientx="$(echo "$Profilex" | awk -F\| '{print $5}')"
			String_rpt="$string_rpt"; urptx_="$string_rpt"
			rm $TMP_PATH/nwutil-saveprofiles.dat.tmp >/dev/null 2>&1
		fi
	else
		Profilex=""
	fi

 	urptx="$(echo "$urptx_" | sed -n "s/,/ /"gp)"; string_rpt_=""

	for x in $(echo "$urptx")
	do
 		if [ "$(echo "$x"| grep -c "(")" -ne "0" ]
 		then
 			fldx="$(echo "$x" | awk -F\( '{print $1}')"
			colx="($(echo "$x" | awk -F\( '{print $2}' | sed -n "s/)//"gp))"
 		else
 			fldx="$x"; colx=""
 		fi
 		if [ "$fldx" = "1" ];then fldx="client"
  		elif [ "$fldx" = "2" ];then fldx="ssbrowse"
  		elif [ "$fldx" = "3" ];then fldx="ssretent"
 	  	elif [ "$fldx" = "4" ];then fldx="sscomp"
 	  	elif [ "$fldx" = "5" ];then fldx="group"
 	  	elif [ "$fldx" = "6" ];then fldx="name"
 	  	elif [ "$fldx" = "7" ];then fldx="ssinsert"
 	  	elif [ "$fldx" = "8" ];then fldx="totalsize"
 	  	elif [ "$fldx" = "9" ];then fldx="nfiles"
 	  	elif [ "$fldx" = "10" ];then fldx="level"
 	  	elif [ "$fldx" = "11" ];then fldx="ssflags"
 	  	elif [ "$fldx" = "12" ];then fldx="ssid"
 	  	elif [ "$fldx" = "13" ];then fldx=""
 	  	elif [ "$fldx" = "14" ];then fldx=""
 	  	elif [ "$fldx" = "15" ];then fldx=""
 	  	elif [ "$fldx" = "16" ];then fldx="barcode"
 	  	elif [ "$fldx" = "17" ];then fldx="volume"
 	  	elif [ "$fldx" = "18" ];then fldx="family"
 	  	elif [ "$fldx" = "19" ];then fldx="volflags"
 	  	elif [ "$fldx" = "20" ];then fldx="state"
 	  	elif [ "$fldx" = "21" ];then fldx="clflags"
 	  	elif [ "$fldx" = "22" ];then fldx="volretent"
 	  	elif [ "$fldx" = "23" ];then fldx="volaccess"
 	  	elif [ "$fldx" = "24" ];then fldx="validcopies"
 	  	elif [ "$fldx" = "25" ];then fldx="pool"; fi
	 	string_rpt_="$string_rpt_$fldx$colx,"
	done
	string_rpt="$($ECHO "$string_rpt_" | sed -n "s/,$//"gp)"; String_rpt="$string_rpt"
	select_rtn group
	if [ "$?" -eq "1" ];then return; fi
	select_rtn client "$Grpx"
 	if [ "$?" -eq "1" ];then return; fi
	reportx="report8"
	multi_prep_rtn
	clear
	$ECHO "Rendering mminfo report using Sort-Order=$Sortx Group=$Grpx Client=$Clientx, please wait ..."  
	if [ -f $TMP_PATH/customize/nwutil.txt ]
	then
		echo "$WHITE_FG DEBUG-CMD: mminfo -s $Serverx -o$Sortx -av -r \"$string_rpt\"$Query_opt\"$Queryx\" -t \"$Datex\"$DEF"
	fi
	mminfo -s $Serverx -o$Sortx -av -r "$string_rpt"$Query_opt"$Queryx" -t "$Datex" > $TMP_PATH/nwutil-$reportx.rpt
 	display_report_rtn $TMP_PATH/nwutil-$reportx.rpt

 	$ECHO "$DEF$header1"
 	$ECHO "$BLUE_BG$WHITE_FG SS-Flag-Definitions: C=Continued E=Eligible-Recycle F=Finished i=Incomplete I=In-Progress k=Chk-Point-Restart K=Cover N=NDMP P=Snapshot r=Purged R=Raw v=Valid  $DEF"
 	$ECHO "$DEF$header1"
	string_rpt=""; String_rpt=""
 	$ECHO "\n$DEF Report File: $TMP_PATH/nwutil-$reportx.rpt <Press Enter To Continue> \c"; read
}

select_rtn()
{
# ====> This routine is used to interactively select a group(s) and client(s) for all report functions 
#       It must be called twice from a report routine the first with an argument to act on a group followed by a client call 
#       The configuration discovery is also done here and saved to a file which can be refreshed at any time from here
#       Arg-1 must be a value of group or client variables Grpx and Clientx are used to pass the appropriate values to the report routines
#       When multiple groups or client are selected they must be done in a continous string that is comma separated

# ====> $! = groups or clients or savesets
	type="$1"; ref_flg=0; grpx_d="$Grpx"; clientx_d="$Clientx"
	while true
	do
		vcnt=0; nwutil_valx=""
		if [ ! -s $config.allclients.dat -o ! -s $config.allgroups.dat -o "$ref_flg" -ne "0" ]
		then
# ====================> no discovery file to a refresh has been requested Discover Networker groups and clients to present them in two columns
# 			Clear refresh flag and build nsradmin input text
			$ECHO "\n$RED_BG$WHITE_FG NOTE: $DEF Discovering Networker Configuration details, Please wait ..."
			ref_flg=0

  			$ECHO ". type: NSR client" > $config.input
  			$ECHO "show name; client" >> $config.input
  			$ECHO "print" >> $config.input
  			nsradmin -s $Serverx -i $config.input | grep . | grep "name:" | sort -u | awk -F: '{print $2}' | grep -n "^" | sed -n "s/:/)/"gp | sed -n "s/ /+/"gp | sed -n "s/\;//"gp > $config.allclients.dat
  			$ECHO ". type: NSR group" > $config.input
  			$ECHO "show name; group" >> $config.input
  			$ECHO "print" >> $config.input
  			nsradmin -s $Serverx -i $config.input | grep . | grep "name:" |sort -u | awk -F: '{print $2}' | grep -n "^" | sed -n "s/:/)/"gp | sed -n "s/ /+/"gp | sed -n "s/\;//"gp > $config.allgroups.dat
			cp $config.allclients.dat $TMP_PATH/nwutil-client.dat
			cp $config.allgroups.dat $TMP_PATH/nwutil-group.dat
                        rm $config.input > /dev/null 2>&1
		fi
 		if [ "$type" = "group" ]
                then
			$ECHO "\n$CYAN_FG The last refresh of the configuration was performed on [$WHITE_FG$(ls -rtl $config.allgroups.dat | awk '{print $6,$7,$8}') $CYAN_FG]$DEF"
			cp $config.allgroups.dat $TMP_PATH/nwutil-group.dat 
                        ref_flg=0
		elif [ "$type" = "client" ]
                then
# ====================> Discover Networker clients and present them in two columns
			$ECHO " Discovering $type names, please wait..."
			if [ "$Grpx" = "ALL" ]
			then
				 cp $config.allclients.dat $TMP_PATH/nwutil-client.dat 
			else
  				$ECHO ". type: NSR client; group: $Grpx" > $config.input
  				$ECHO "show name; group" >> $config.input
  				$ECHO "print" >> $config.input
  				nsradmin -s $Serverx -i $config.input | grep . | grep "name:" | awk -F: '{print $2}' | sort -u | grep -n "^" | sed -n "s/:/)/"gp | sed -n "s/ /+/"gp | sed -n "s/\;//"gp > $TMP_PATH/nwutil-client.dat
                        	rm $config.input > /dev/null 2>&1
			fi
		
		fi

# ============> Prepare the group or client list to be displayed in two columns
		for list in $(cat $TMP_PATH/nwutil-$type.dat)
		do
			if [ "$vcnt" -lt "1" ]
        		then
              			vcnt=$(expr $vcnt + 1)
				vline="$($ECHO "$list                                                       " | cut -b1-55)"
        		else
              			vcnt=0; vline="$list\n"
        		fi
			nwutil_valx="$nwutil_valx $vline"
		done
		nwutil_valx="$($ECHO "$nwutil_valx" | sed -n "s/)/)  /"gp)"
		$ECHO
# ============> Display the group or client list
		while true
		do
			$ECHO "\n$GREEN_FG$nwutil_valx" | sed -n "s/+/ /"gp
			if [ "$type" = "client" -a "$valx" = "" ]
			then
				valx_d="$Clientx"
			elif [ "$type" = "client" -a "$valx" != "" ]
			then
				valx_d="ALL"; Clientx="ALL"
			else
				valx_d="$Grpx"
			fi
			if [ -s $TMP_PATH/nwutil-saveprofiles.dat ]; then select_msg=" S=Select-Profile,"; else select_msg=""; fi
			$ECHO "\n$BLUE_BG$YELLOW_FG Current-[$type]-Default=$DEF[ $WHITE_FG$valx_d$DEF ]"
			if [ "$type" = "group" ]
			then
				$ECHO "$CYAN_FG\nEnter a $type name by: number, comma separated list, a range #-#, A=All,$select_msg R=Refresh or Q=Quit :>$DEF \c"; read valx
			else
				$ECHO "$CYAN_FG\nEnter a $type name by: number, comma separated list, a range #-#, A=All, R=Refresh or Q=Quit :>$DEF \c"; read valx
			fi

			if [ "$valx" = "q" -o "$valx" = "Q" ]; then return 1; fi
#			if [ "$valx" = "c" -o "$valx" = "C" -a -s $TMP_PATH/nwutil-saveprofiles.dat ]
#			then
#    				$ECHO 
#				grep -v "\[C-" $TMP_PATH/nwutil-savecustom.dat | grep -n "^" | sed -n "s/:/) /"p | tee -a $TMP_PATH/nwutil-savecustom.dat.tmp
#				$ECHO "$CYAN_FG\nSelect the desired profile by number: or E=Edit Q=Quit :$DEF > \c"; read customx
#				if [ "$customx" = "q" -o "$customx" = "Q" -o "$customx" = "" ];then continue; fi
#				if [ "$customx" = "e" -o "$customx" = "E" ]
#				then
#					vi $TMP_PATH/nwutil-savecustom.dat
#					continue
#				fi
#				Customx="$(sed -n "$customx,$customx"p $TMP_PATH/nwutil-savecustom.dat.tmp)"
#				Customex_flag=1; valx=""
#				rm $TMP_PATH/nwutil-savecustom.dat.tmp >/dev/null 2>&1
#				break
#			else
#				Customx=""
#				break
#			fi

			if [ "$valx" = "s" -o "$valx" = "S" -a -s $TMP_PATH/nwutil-saveprofiles.dat ]
			then
    				$ECHO 
				if [ "$String_rpt" = "" ]
				then
					grep -v "\[U-" $TMP_PATH/nwutil-saveprofiles.dat | grep -n "^" | sed -n "s/:/) /"p | tee -a $TMP_PATH/nwutil-saveprofiles.dat.tmp
				else
					grep "\[U-" $TMP_PATH/nwutil-saveprofiles.dat | grep -n "^" | sed -n "s/:/) /"p | tee -a $TMP_PATH/nwutil-saveprofiles.dat.tmp
				fi
				$ECHO "$CYAN_FG\nSelect the desired profile by number: or E=Edit Q=Quit :$DEF > \c"; read profilex
				if [ "$profilex" = "q" -o "$profilex" = "Q" -o "$profilex" = "" ];then continue; fi
				if [ "$profilex" = "e" -o "$profilex" = "E" ]
				then
					vi $TMP_PATH/nwutil-saveprofiles.dat
					continue
				fi
				Profilex="$(sed -n "$profilex,$profilex"p $TMP_PATH/nwutil-saveprofiles.dat.tmp)"
				Profilex_flag=1; valx=""
				rm $TMP_PATH/nwutil-saveprofiles.dat.tmp >/dev/null 2>&1
				break
			else
				Profilex=""
				break
			fi
		done

#  ===========> Used to convert a range #-# to a comma separated list
		if [ "$(echo "$valx" | grep -c "\-")" -ne "0" -a "$Profilex" = "" ]
		then
			valx_new=""
			valx_f="$(echo "$valx" | awk -F- '{print $1}')"
			valx_l="$(echo "$valx" | awk -F- '{print $2}')"
			while true
			do
				valx_new="$valx_new$valx_f,"
				if [ "$valx_f" -lt "$valx_l" ]
				then
					valx_f="$(expr $valx_f + 1)"
				else 
					valx="$(echo "$valx_new" | sed -n "s/,$//"p)"
					break
				fi
			done
		fi

 		if [ "$Profilex" != "" ]
		then
			break
 		elif [ "$valx" = "r" -o "$valx" = "R" ]
  		then
			ref_flg=1; continue
		elif [ "$valx" = "a" -o "$valx" = "A" -a "$type" = "group" ]
		then
			Multi_flag=0; Grpx="ALL" ; valx="ALL"; break
		elif [ "$valx" = "a" -o "$valx" = "A" -a "$type" = "client" ]
		then
			Multi_flag=0; Clientx="ALL" ; valx="ALL"; break
 		elif [ "$(echo "$valx" | grep -c ",")" -ne "0" ] 
		then
			item_select_rtn $type $valx
			break
 		elif [ "$(echo "$valx_d" | grep -c ",")" -ne "0" -a "$valx" -eq "" ]
		then
			item_select_rtn $type "$valx_d"
			break
		elif [ "$valx" = "" -a "$type" = "group" ]
		then
			valx="$valx_d"
			if [ "$type" = "group" -a "$valx_d" = "" ]
			then
				$ECHO "\n$RED_FG ERROR: You must select a group# or A=All$DEF"
				continue
			fi
			break
		elif [ "$valx" = "" -a "$type" = "client" ]
		then
			valx="$valx_d"
			break
		else
			valx_d="$valx"
		fi
		item_select_rtn $type $valx
# ============> Only arrive here if the entry is a single numeric, alpha input, comma separated and null are proceesed prior to reaching here
 		break
	done

	if [ "$type" = "client" -a "$Profilex" = "" ]
	then

		$ECHO "\n$BLUE_BG$YELLOW_FG NOTE:$DEF Date range syntax$WHITE_FG > # h=hour|d=day|w=week|m=month,  Examples: 12 h, 1 d, 3 w, 2 m, mm/dd/yy etc..."
		$ECHO "$CYAN_FG\nEnter a start date or time range to search from. Def=[$WHITE_FG$Datex$CYAN_FG]: >$DEF \c"; read timex
		if [ "$timex" != "" ]
		then
			if [ "$($ECHO "$timex" |egrep -c "h|H")" -ne "0" ];then periodx="hour"
			elif [ "$($ECHO "$timex" |egrep -c "d|D")" -ne "0" ];then periodx="day"
			elif [ "$($ECHO "$timex" |egrep -c "w|W")" -ne "0" ];then periodx="week"
			elif [ "$($ECHO "$timex" |egrep -c "m|M")" -ne "0" ];then periodx="month"; fi
			numx="$($ECHO "$timex" | awk '{print $1}')"
			Datex="$numx $periodx ago"
		fi
		$ECHO "\n$BLUE_BG$YELLOW_FG NOTE-(case-sensitive):$DEF  t=Time c=Client-Name m=Media-Name n=Saveset-Name R=Reverse-Order   Example tc=Sort-By: time->client-name"
		$ECHO "$CYAN_FG\nEnter desired sorting order, Def=[$WHITE_FG$Sortx$CYAN_FG]: >$DEF \c"; read sortx
		if [ "$sortx" != "" ];then Sortx="$sortx"; fi
		$ECHO "$CYAN_FG\nSave current selection criteria for future use? Def=[$WHITE_FG Y/N Def=N $CYAN_FG]: >$DEF \c"; read saveit
		if [ "$saveit" = "Y" -o "$saveit" = "y" ]
		then
			if [ "$String_rpt" != "" ];then uprof="U-"; else uprof=""; fi
		 	$ECHO "$CYAN_FG\nEnter a profile name:$DEF > \c"; read saveit_name	
			mv $TMP_PATH/nwutil-saveprofiles.dat $TMP_PATH/nwutil-saveprofiles.dat.tmp >/dev/null 2>&1
			grep -v "\[$uprof$saveit_name\]" $TMP_PATH/nwutil-saveprofiles.dat.tmp > $TMP_PATH/nwutil-saveprofiles.dat
			$ECHO "$GREEN_FG[$uprof$saveit_name]$DEF|$Sortx|$Datex|$Grpx|$Clientx|$String_rpt" >> $TMP_PATH/nwutil-saveprofiles.dat
			sort -u $TMP_PATH/nwutil-saveprofiles.dat > $TMP_PATH/nwutil-saveprofiles.dat.tmp
			mv $TMP_PATH/nwutil-saveprofiles.dat.tmp $TMP_PATH/nwutil-saveprofiles.dat 
		fi
	fi
	
	if [ "$Profilex" != "" ]
	then
		Sortx="$(echo "$Profilex" | awk -F\| '{print $2}')"; Datex="$(echo "$Profilex" | awk -F\| '{print $3}')"
		Grpx="$(echo "$Profilex" | awk -F\| '{print $4}')"; Clientx="$(echo "$Profilex" | awk -F\| '{print $5}')"
		Profilex_flag=0
	else
		if [ "$($ECHO "$valx" | grep -c "+")" -ne "0" ];then valx="$($ECHO "$valx" | sed -n "s/+/ /"gp)"; fi
		if [ "$Multi_flag" -eq "1" ];then valx="$Grpx"
   		elif [ "$type" = "group" ];then Grpx="$valx"
		else Clientx="$valx"; fi
	fi
}

item_select_rtn()
{
# $2 = group or client number list comma separated $1 = type

	if [ "$1" = "group" -a  "$(echo "$2" |egrep -i -c "a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|x|y|z")" -ne "0" ]
	then
		Grpx="$2"; return
	elif [ "$1" = "client" -a  "$(echo "$2" |egrep -i -c "a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|x|y|z")" -ne "0" ]
	then
		Clientx="$2"; return
	fi

	Multix=""
	if [ "$(echo "$2" | grep -c ",")" -eq "0" ];then selectx="$2"
	elif [ "$(echo "$2" | grep -c ",")" -ne "0" ];then selectx="$(echo "$2" | sed -n "s/,/ /"gp)"; fi

	for x in  $(echo "$selectx")
	do
		if [ "$(expr $x \% 2)" -eq "0" ]
		then
			itemx="$(echo "$nwutil_valx" | grep " $x)" | head -1 | awk '{print $4}')"
			Multix="$Multix$itemx,"
		else
			itemx="$(echo "$nwutil_valx" | grep " $x)" | head -1 | awk '{print $2}')"
			Multix="$Multix$itemx,"
		fi
	done
	Multi_flag=1
  	if [ "$(echo "$Multix" | grep -c "+")" -ne "0" ];then Multix_="$(echo "$Multix" | sed -n "s/+/ /"gp)"; else Multix_="$Multix"; fi
	if [ "$1" = "group" ]
	then
  		Grpx="$(echo "$Multix_" | sed -n "s/,$//"p)"; valx_d="$Grpx"
	elif [ "$1" = "client" ]
	then
  		Clientx="$(echo "$Multix_" | sed -n "s/,$//"p)"; valx_d="$Clientx"
	fi
}

# #########################################################################################################
# MAIN Main main
# #########################################################################################################
HOMEPATH="/adam"; 
if [ -d /home/admin/admeadmin ]
then
	TMP_PATH="/home/admin/admeadmin"
else
	TMP_PATH="/atoadmin"
fi

mkdir $TMP_PATH/nwutil >/dev/null 2>&1; TMP_PATH="$TMP_PATH/nwutil"
if [ "$1" != "" ];then Serverx="$1"; else Serverx="$(hostname)"; fi; mkdir $TMP_PATH/$Serverx >/dev/null 2>&1
TMP_PATH="$TMP_PATH/$Serverx"
config="$TMP_PATH/nwutil_discovery"

#
Fld1_jobid=""; Fld2_bucnt=""; Fld3_bustream=""; Fld4_emflag=""; Fld5_buloc=""; Fld6_retcnt="", Fld7_emlist=""; Displayx=""; Grpx=""; Clientx="ALL"; Datex="1 day ago"
dash="-"; usc="_"; jcnt="0"; Title_=""; color_flag=0; Multix=""; Multi_flag=0; Sortx="tnc"; Client_q=""; Group_q=""; Queryx=""; Query_opt=""; Profilex=""; Profilex_flag=0
String_rpt=""
header1="=================================================================================================================================================================="
# Color Setup
RED_FG="\033[31;1m"; GREEN_FG="\033[32;1m"; BLUE_FG="\033[34;1m"; MAG_FG="\033[35;1m"; CYAN_FG="\033[36;1m"; WHITE_FG="\033[37;1m"; GRAY_FG="\033[30;1m"
RED_BG="\033[41;1m"; GREEN_BG="\033[42;1m"; BLUE_BG="\033[44;1m"; MAG_BG="\033[45;1m"; CYAN_BG="\033[46;1m"; WHITE_BG="\033[47;1m"; YELLOW_FG="\033[1;33m"
DEF="\033[0m"; BOLD="\033[1m"; BLINK="\033[5m"; NOBLINK="\033[25m"; USCORE="\033[4m"; YELLOW_BG="\033[1;43m"
# No Color
# RED_FG=""; GREEN_FG=""; BLUE_FG=""; MAG_FG=""; CYAN_FG=""; WHITE_FG=""; GRAY_FG=""; RED_BG=""; GREEN_BG=""; BLUE_BG=""; MAG_BG=""; CYAN_BG=""; WHITE_BG=""
# DEF=""; BOLD=""; BLINK=""; NOBLINK=""; USCORE="\033[4m"
spin[0]="$RED_FG | $DEF"; spin[1]="$CYAN_FG / $DEF"; spin[2]="$WHITE_FG - $DEF"; spin[3]="$RED_FG \\ $DEF"; spin[4]="$GREEN_FG | $DEF"; spin[5]="$BLUE_FG / $DEF"; spin[6]="$GREEN_FG - $DEF"; spin[7]="$RED_FG \\ $DEF"

menu_rtn
