#!/usr/bin/perl 
## @file sysparm_gen
# @brief This script generates userinfo.txt file with new values.
#   For now, only passwords are synchronized.
# @details
# @param $ARGV[0] - full file path to userinfo.txt which will be created
# @return $OK    - OK
# @return $ERROR - ERROR
# @author Alex Arkhipov
# @author Vladimir Ivanov
# @date October 2008
# @version 1.00 - Initial version
# @version 1.01 - Modified under the perlcheck warnings cleanup activity.
# @note
use lib "/opt/nortel/base/lib";

use warnings;
use strict;
use common_variables;

# Functions' declarations
sub main();
sub read_file();
sub write_file();
sub update();
sub update_passwds();

# Variables' declarations
my $out_file    = $ARGV[0];
my $ui_file     = "/admin/userinfo.txt";
my $shadow_file = "/etc/shadow";
my %ui;
my %users;

exit(main());

## @fn  main()
# @brief Main function.
# @return $OK(0)     - system parameters were generated successfully
# @return $ERROR(1)  - could not generate system parameters
# @author  Alex Arkhipov
# @date
sub main()
{
   if (!$out_file)
   {
      print "ERROR: No file path provided. Exiting...\n";
      return $ERROR;
   }

   unless (read_file() && update() && write_file())
   {
      return $ERROR;
   }

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

## @fn  read_file()
# @brief Reads current settings from userinfo file
# @return $TRUE  - read successfully
# @return $FALSE - some errors occurred
# @author  Alex Arkhipov
# @date
sub read_file()
{
   my ($fh, $str, $key, $val);
   unless (open($fh, $ui_file))
   {
      print "ERROR: Cannot open $ui_file file for reading.\n";
      return $FALSE;
   }

   while ($str = <$fh>)
   {
      chomp($str);
      if ($str =~ /^\#/)  # Skip comments
      {
         next;
      }

      $str =~ /^(.*)=(.*)$/;
      $key = $1;
      $val = (defined($2)) ? $2 : '';

      $ui{$key} = $val;
   } ## end while ($str = <$fh>)

   close($fh);
   return $TRUE;
} ## end sub read_file()

## @fn  write_file()
# @brief Writes updated settings to userinfo file
# @return $TRUE  - written successfully
# @return $FALSE - some errors occurred
# @author  Alex Arkhipov
# @date
sub write_file()
{
   my $fh;
   unless (open($fh, "+>", "$out_file"))
   {
      print "Cannot open the $out_file file for writing.\n";
      return $FALSE;
   }

   my @key_set = sort keys %ui;

   foreach my $key (@key_set)
   {
      print $fh $key, "=", $ui{$key}, "\n";
   }

   close($fh);
   return $TRUE;
} ## end sub write_file()

## @fn  update()
# @brief Updates userinfo file
# @return $TRUE  - updated successfully
# @return $FALSE - some errors occurred
# @author  Alex Arkhipov
# @date
sub update()
{
   unless (update_passwds())
   {
      return $FALSE;
   }
   return $TRUE;
} ## end sub update()

## @fn  update_passwds()
# @brief Reads shadow file and updates passwds
# @return $TRUE  - Ok
# @return $FALSE - some errors occurred
# @author  Alex Arkhipov
# @date
sub update_passwds()
{
   my $fh;

   # Read shadow file for current passwords
   unless (open($fh, $shadow_file))
   {
      print "Cannot open $shadow_file file for reading.\n";
      return $FALSE;
   }

   while (my $str = <$fh>)
   {
      chomp($str);
      if ($str =~ /^\#/)  # Skip comments
      {
         next;
      }

      my @fields = split(/:/, $str);
      my $user   = $fields[0];
      my $pass   = $fields[1];
      $users{$user} = $pass;
   } ## end while (my $str = <$fh>)

   close($fh);

   # Update passwords
   foreach my $key (keys(%ui))
   {
      if ($key =~ /(\w*)pw$/)  # If password
      {
         my $user = $1;

         # Find user to update
         foreach my $k (keys(%users))
         {
            if ($user eq $k)
            {
               $ui{$key} = $users{$k};
               last;
            }
         } ## end foreach my $k (keys(%users)...
      } ## end if ($key =~ /(\w*)pw$/...
   } ## end foreach my $key (keys(%ui))

   return $TRUE;
} ## end sub update_passwds()

