#!/opt/bin/perl
#<copyright>
# ----------------------------------------------------------
# Sun Proprietary/Confidential Code
# Copyright 2001, Sun Microsystems, Inc. All rights reserved.
# ----------------------------------------------------------
#</copyright>

#
# fix the first line of the perl program to use the right perl program.
#

$PERL = $ARGV[0];
if (!-f $PERL) {
  print "Cannot run $perl, aborting\n";
  exit(1);
}

foreach $pr ('rashttp','rasagent','testt3',
     'ras_install.exe', 'checkcron','save_password','clearcache',
     'notify','rasmonitor', 'ras_admin', 'ras_fccheck',
     'sendNewTopo','config_solution','ras_revcheck','rascgi' ) {

      &fix("/opt/SUNWstade", "bin/$pr", 1);
}

foreach $pr ('checkAgent', 'readToc'){

      &fix("/opt/SUNWstade", "sbin/$pr", 1);
}

&fix("/opt/SUNWstade", "Diags/bin/linktest", 1);
&fix("/opt/SUNWstade", "Diags/bin/t3volverify", 1);
&fix("/opt/SUNWstade", "Diags/bin/t3ofdg", 1);
&fix("/var/sadm/pkg/SUNWstade", "install/preremove", 2);


sub fix {
  my($DIR, $pr, $line) = @_;

  my $pr2 = $pr;
  my $ix = rindex($pr2, "/");
  $pr2 = substr($pr2, $ix+1);

  if (open(W, ">/tmp/$pr2.tmp")) {
    open(O, "$DIR/$pr");
    if ($line == 1) {
      $l = <O>;  
      print W "#!$PERL -I$DIR/lib\n";
    } else {
      $l = <O>;
      print W $l;
      $l = <O>;
      print W " $PERL <<'EOF'\n";
    }

    while ($l = <O>) {
       print W $l;
    }
    close(O);
    close(W);
  } else {
    print "Error: Cannot write to /tmp/$pr2.tmp: $! \n";
  }
  open(W, ">$DIR/$pr");
  open(O, "/tmp/$pr2.tmp");

  while ($l = <O>) {
     print W $l;
  }
  close(O); close(W);
  unlink("/tmp/$pr2.tmp");
}

  
