#!/usr/bin/perl

#use strict;
use Term::ANSIColor;

system ('clear');
###############################################################################################################
print   color("red"),   "###############################################################",color("reset"),"\n",
        color("white"), "Presenting:                                                    ",color("reset"),"\n",
        color("white"), "       Exersize 11.8                                          ",color("reset"),"\n",
        color("red"),   "###############################################################",color("reset"),"\n";
print   "Part 1:",color("magenta"),"\tRun this program with 2 arguments.                ",color("reset"),"\n",
                  color("magenta"),"\t\tThe first argument will be either UID, PID, or CMD",color("reset"),"\n",
	          color("magenta"),"\t\tThe second arguent will be a search string.       ",color("reset"),"\n",
		  color("magenta"),"\tThe program will output coluns UID, PID, or CMD   ",color("reset"),"\n",
		  color("magenta"),"\tfrom ps -Af, that match the string.               ",color("reset"),"\n";

###############################################################################################################

# non-interactive mode
  # Check to see if run with two arguments,
  #                 fist argument is either uid, pid, or cmd
  #                 second argument is not empty
  unless ( (@ARGV eq 2 ) && ($ARGV[0] =~ m/uid|pid|cmd/i) &&  ( $ARGV[1] )) {
     die "Must run $0 with 2 arguments:  stile11.8.pl <UID|PID|CMD> <Search String>:\t$!\n";
  }
# assign the variables
  $arguments=@ARGV;
  my $column = lc(shift @ARGV);
  my $search = lc(shift @ARGV); 

 print color("green"),"#########################################",color("reset"),"\n";
print                  "Part1:\t Use backquotes to get ps output.\n";
  @ps_raw =&Ps_with_backquotes();
  @ps_sub =&Search_for_string ( $column,$search, @ps_raw );
  &Print_matching_strings (@ps_sub);

print color("green"),"##################################################################",color("reset"),"\n";
print                "Part2:\t Use open() with a pipe on the right to get the ps output.\n";
  @ps_raw =&Ps_with_open();
  @ps_sub =&Search_for_string ( $column,$search, @ps_raw );
  &Print_matching_strings (@ps_sub);

exit;
###############################################################################################################
sub Ps_with_backquotes {     
	print color("green"),"########### Ps_with_Backquotes ##########",color("reset"),"\n";	
	@ps_raw=( `ps -Af` );	
        return @ps_raw;
}
###############################################################################################################
sub Ps_with_open {           
	print color("green"),"######################### Ps_with_Open ###########################",color("reset"),"\n";
	open (PS, "ps -Af |");
	while (<PS>){  
	  push (@ps_raw, "$_" ); 
	}
	close (PS);
	return @ps_raw;
}
###############################################################################################################
sub Search_for_string {     
       $column = shift;
       $search = shift;
       @ps_raw=@_ ;
       @ps_sub="";
       $total_number_of_processes=@ps_raw;
       
       print color("red"),"Processes total:   $total_number_of_processes",color("reset"),"\n";
        
	# Process each line of @ps_raw
	foreach $line (@ps_raw){
	  # split the line into it's parts
	  ($uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd)= split ( " ", $line );
	  # If the required $column contains the $search string, add it to the @ps_sub array
	  if ( ${$column} =~  m/$search/i ){	  
	    $new_line="$uid\t$pid\t$cmd\n";
	    push (@ps_sub, $new_line);
	  }
	}
	$match_number_of_processes=@ps_sub;
	print color("red"),"Process matching:  $match_number_of_processes",color("reset"),"\n";
	return @ps_sub ;
}
###############################################################################################################
sub Print_matching_strings {
	@ps_sub= @_;
	print color("yellow"),"Matching Lines:",color("reset"),"\n";
	print "\tUID\tPID\tCMD\n";
	foreach $line ( @ps_sub ){
	  chomp ( $line );
	  print "\t$line\n";
	}
}
###############################################################################################################
