#!/usr/bin/perl



open( HOST, "hostname |" ) ;

chop( $hostname = <HOST> ) ;

close( HOST ) ;



die "Usage: allfix [-options] [match-pattern]\n",

    "    Send specified files to fix.\n",

    "    Host<$hostname> must be <mutsu.konami>.\n",

    "  options:\n",

    "\t-from <name> name(usr?????) for updating person\n",

    "\t-mine update all my files\n" if ( $#ARGV == -1 || $hostname ne "mutsu.konami" ) ;



$files = "" ;

$from  = $ENV{USER} ;



for( $i=0 ; $i<=$#ARGV ; $i++ )

{

    $from = $ARGV[++$i], next if ( $ARGV[$i] eq "-from" ) ;

    $mine_flg = 1, next if ( $ARGV[$i] eq "-mine"   ) ;

    $pattern = $ARGV[$i] if ( $locate eq "" ) ;

}



%user = ("usr00339" => "¼",

	 "usr00725" => "",

	 "usr01098" => "ʿ",

	 "usr01475" => "",

	 "usr05739" => "˭",

	 "usr02011" => "໳",

	 "usr02774" => "",

	 "usr03806" => "¼",

	 "usr04098" => "" ) ;

$from = $user{$from} if ( $user{$from} ) ;



open( TMP, "/exports/mj001data/httpd/cgi-bin/msg/entries" ) ;

foreach( <TMP> )

{

    s/<OPTION>// ;

    s/<\/OPTION>// ;

    $file  = $_ ;

    $file =~ s/\[[^\]]*\]\[[^\]]*\]// ;

    if ( $mine_flg ? /\[$from:/||/:$from\]/ : $file =~ $pattern )

    {

	chop ;

	$tags{'files'} = $_ ;

	$cmd_line .= &encode( "files" )."&" ;

	print "$file" ;

	chop( $file ) ;

    }

}

close( TMP ) ;



die "No files for Fixing!!\n" if ( $tags{'files'} eq "" ) ;

undef( $tags{'files'} ) ;





%tags = ("from"   => $from,

	 "where"  => "Fix",

	 "flag"   => "",

	 "bbs"    => "0",

	 "entry"  => "msg/entries",

	 "method" => "" ) ;



$cmd_line .= &encode("from"     ,

		     "where"    ,

		     "bbs"      ,

		     "entry"  ,

		     "method"    ) ;



print "Is it OK ?" ;

<STDIN> ;



&fork( "/exports/mj001data/httpd/cgi-bin/fix.cgi '$cmd_line' > /dev/null" ) ;



exit( 0 ) ;





#

# Subroutines

#

#

#

sub fork

{

    local $pid ;



    unless ( $pid = fork )

    {

	exec( $_[0] ) ;

	exit 0 ;

    }

    waitpid( $pid, 0 ) ;

}



sub encode

{

    local @tag_name = @_ ;

    local $str, $tmp ;



    foreach( @tag_name )

    {

	$tmp = $tags{$_} ;

	$tmp =~ s/(\W)/sprintf("%%%.2X", ord($1))/ge ;

	$tmp =~ s/%20/+/g ;

	$tmp =~ s/%2E/\./g ;

	$str .= $_."=".$tmp."&" ;

    }

    chop( $str ) ;

    return $str ;

}



1 ;



