###################################################
#  RAID utility CGI Proguram                      #
#       SAN Project                               #
#                 Copy right. 2004  Hitachi.Lmt.  #
###################################################
#
# History  :Version       :DATE        : Coder       : P# & Summary
# RAID 500 :50-03-02/00-03:2004.10.22  : T.Kishimoto : U5002223708
#          :50-03-0X/00-03:2004.10.22  : K.Usami     : UPRM2223552 SLPR
#
# subroutine name  :check_userlist
# filename         :check_userlist.cgi
# 
# Augument : void
#
# Return   : == 1  Normal END
#            != 1  Error  END
#
# Document
# 

sub check_userlist {

    local($userlist,$len,$adj) ;
    local($i,$j) ;
    local(@tmp,@tmp1,@buf) ;

    $userlist="./csv/userlist.csv" ;

# Read USERLIST.CSV
    unless (open(FH,"< $userlist")) {
      sleep 1 ;
      unless (open(FH,"< $userlist")) {
        sleep 1 ;
        unless (open(FH,"< $userlist")) {
            return (-1) ;                # File Open Error
        }
      }
    }
# remake UPRM2223552 SLPR
    while (<FH>) {
	chomp;
        $buf[$.] = $_ ;                     # Read All DATA
    }
    close(FH) ;
    if ($buf[1] =~ /^root,/) {
        @tmp1 = split(/,/,$buf[1]) ;
        $len = length($tmp1[2]) ;
        if($len != 128){
	  $flg=1;
        }
# if not admin authority
        if($tmp1[3] == 0){
	  $flg=1;
        }
    }
    foreach $i (1 .. $#buf) {
       @tmp1 = split(/,/,$buf[$i]);
	$len = length($tmp1[2]) ;
        if($len != 128){
	  $flg=1;
        }
       if($tmp1[5] eq ""){
	  $flg=1;
	}	
    } 
    if($flg==0){
       return(0);
    }


# Write USERLIST.CSV
    unless (open(FH,"> $userlist")) {
      sleep 1 ;
      unless (open(FH,"> $userlist")) {
        sleep 1 ;
        unless (open(FH,"> $userlist")) {
            return (-1) ;                # File Open Error
        }
      }
    }

    foreach $i (1 .. $#buf) {
        if ($buf[$i] =~ /^root,/) {
#        if (($buf[$i] =~ /^root/) || ($buf[$i] =~ /^administrator/)) {
            @tmp = split(/,/,$buf[$i]) ;
            $len = length($tmp[2]) ;
            $adj = 'F' x (128 - $len) ;
            if($tmp[5] eq ""){
               $tmp[5]="65535";
             }
             print FH "$tmp[0],$tmp[1],$adj$tmp[2],1";
             for $j (4 .. $#tmp) {
               print FH ",$tmp[$j]" ;
             }
             print FH "\n";
        } else {
             @tmp = split(/,/,$buf[$i]) ;
             $hexrow = $tmp[2] ;
             $len = length($hexrow) ;
             if($tmp[3] eq ""){
               $tmp[3]="0";
             }
             if($tmp[3]==0){
		$prechara="0";
             }else{
		$prechara="F";
             }
             $adj = $prechara x (128 - $len) ;
             if($tmp[5] eq ""){
               $tmp[5]="65535";
             }
             print FH "$tmp[0],$tmp[1],$adj$tmp[2],$tmp[3]" ;
             for $j (4 .. $#tmp) {
                print FH ",$tmp[$j]" ;
             }
             print FH "\n";
        }
    }

    close(FH) ;

    return (1) ;

}
1;
