#!/usr/bin/perl
#############################################################
## @file clihelp
# @brief Prints listing of CLI commands
# @details Prints out all the CLI commands available to a user based on their path
# @author Kushal Agarwal
# @date July 2008
# @version 1.00 - Initial version
# @version 1.01 - Changes to meet perl coding standards
# @version 1.02 - Two colons were removed before shared functions and variables.
# @version 1.03 - Same set for different user error is fixed. Slightly changed commands output and help header text.

use lib "/opt/nortel/base/lib";

use warnings;
use strict;

use File::Basename;
use common_variables;
use baselog;
use base_system;
## @skipline

sub clihelp();

exit(clihelp());

##################################################################
# @fn clihelp
# @brief Prints listing of available commands
# @details
# @return $OK  - command was successful
# @return $ERROR - command had errors
##################################################################
sub clihelp()
{
   my @path    = split(':', $ENV{'PATH'});
   my $tmpFile = "/tmp/clihelp.txt" . $$;

   my $helpFd;
   if (!open($helpFd, '>', $tmpFile))
   {
      baselog($LOG_ERROR, "Unable to create temporary file $tmpFile\n", $TRUE);
      return $ERROR;
   }
   print $helpFd "CLI Help\n";
   print $helpFd "This is the list of commands available to be executed.\n";
   print $helpFd "For usage information on any command try one of these keys:\n";
   print $helpFd "   <command> -h \n   or \n   <command> --help \n   or \n   <command> help \ndepending on command.\n";
   print $helpFd "For detailed information on any command type:\n";
   print $helpFd "   man <command>\n";
   print $helpFd "Press 'q' to exit this help!\n\n";

   foreach my $path (@path)
   {
      if ($path =~ "nortel")
      {
         my @cmdList = ();
         do_system([$LS, $path], {$dsOUT => \@cmdList});
         my $dirName  = dirname($path);
         my $pathName = basename($dirName);
         if (scalar(@cmdList))
         {
            print $helpFd "\n$pathName/bin:\n\n";
            foreach my $cmdList (@cmdList)
            {
               print $helpFd "  " . $cmdList;
            }
         } ## end if (scalar(@cmdList))
      } ## end if ($path =~ "nortel")
   } ## end foreach my $path (@path)

   close $helpFd;
   do_system([$MORE, $tmpFile], {$dsINTERACT => $TRUE});
   if (!do_system([$RM, '-rf', $tmpFile]))
   {
      baselog($LOG_ERROR, "Unable to remove temporary file $tmpFile\n", $TRUE);
      return $ERROR;
   }

   return $OK;
} ## end sub clihelp()
