[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/se3-unattended/var/se3/unattended/install/bin/ -> appsonly.pl (source)

   1  # perl script for apps-only-installs, called by appsonly.bat
   2  # contributed by Gerhard Hofmann, gerhard.hofmann@planat.de
   3  # place appsonly.bat, appsonly.pl, choice.exe, setenv.exe,
   4  # pathman.exe into \\server\install\bin
   5  # some documentation included in appsonly.bat
   6  # Download of appsonly.bat, appsonly.pl and some "helper" apps:
   7  # http://unattended.cvs.sourceforge.net/unattended/unattended/install/bin/
   8  # Release 2008.07.29
   9  
  10  use Win32::NetResource;
  11  #use warnings;
  12  #use strict;
  13  use Carp;
  14  use File::Spec::Win32;
  15  use File::Copy;
  16  #use Unattend::IniFile;
  17  #use Unattend::WinMedia;
  18  use Win32::Console ;
  19  
  20  
  21  
  22  $autologinpwd = "";
  23  
  24  
  25  # Select from among zero or more strings.
  26  sub multi_choice (@) {
  27      my ($prompt, @strings) = @_;
  28  
  29      scalar @strings > 0
  30          or return ();
  31  
  32      my %selected = map { $_ => 0 } @strings;
  33  
  34      my $menu_state = { 'prompt' => $prompt };
  35  
  36    LOOP:
  37      while (1) {
  38          my @choices =
  39              ('Select/deselect all' =>
  40               sub { my $sel = (0 < scalar grep { $selected{$_} == 0
  41                                                  } @strings);
  42                     # If anything is not selected, select all; else,
  43                     # deselect all.
  44                     %selected = map { $_ => $sel } @strings;
  45                 },
  46               'All done ; continue' =>
  47               sub {
  48                   no warnings 'exiting';
  49                   last LOOP;
  50               },
  51               map { 
  52                   my $str = $_;
  53                   (sprintf "[%s] %s", $selected{$str} ? '*' : ' ', $str)
  54                       => sub { $selected{$str} = !$selected{$str} }
  55                 } @strings,
  56               );
  57  
  58          my $func = menu_choice ($menu_state, @choices);
  59          &$func ();
  60      }
  61  
  62      my %sort_index;
  63      foreach my $i (0 .. scalar @strings - 1) {
  64          $sort_index{$strings[$i]} = $i;
  65      }
  66  
  67      my @selections = grep { $selected{$_} } keys %selected;
  68      return sort { $sort_index{$a} <=> $sort_index {$b} } @selections;
  69  }
  70  
  71  
  72  # Create a menu of options.  Takes an even number of arguments which
  73  # are display / return pairs.  For example:
  74  #
  75  #     menu_choice ('option X' => 'foo', 'option Y' => 'bar')
  76  #
  77  # ...returns 'foo' if the user selects option X and 'bar' if the user
  78  # selects option Y.
  79  sub menu_choice (@) {
  80      my @args = @_;
  81      my @choice_map;
  82      my $opts = { };
  83  
  84      # Current page
  85      my $page = 0;
  86      # Prompt
  87      my $prompt = '';
  88  
  89      ref $args[0] eq 'HASH'
  90          and $opts = shift @args;
  91      
  92      # Process magic options hash.
  93      foreach my $key (keys %$opts) {
  94          if ($key eq 'page') {
  95              $page = $opts->{$key};
  96          }
  97          elsif ($key eq 'prompt') {
  98              $prompt = $opts->{$key} . "\n";
  99          }
 100      }
 101  
 102      scalar @args % 2 == 0
 103          or croak "menu_choice called with odd number of arguments";
 104  
 105      # Total number of choices
 106      my $count = scalar @args / 2;
 107  
 108      # Choices to display per page
 109      my $per_page = 15;
 110  
 111      my $pages = int(($count + $per_page - 1) / $per_page);
 112  
 113      my $ret;
 114    LOOP:
 115      while (1) {
 116          print "\n$prompt";
 117          $pages > 1
 118              and printf "(Page %d/%d)\n", $page+1, $pages;
 119  
 120          my $start = $page * $per_page;
 121  
 122          my $i = 0;
 123          my $choices = '';
 124  
 125          # Generate current page of choices.
 126          while ($i < $per_page && $start + $i < $count) {
 127              my $elt = 2 * ($start + $i);
 128              my $hexd = sprintf '%X', $i+1;
 129              print "$hexd) $args[$elt]\n";
 130              $choices .= $hexd;
 131              # Capture value for sub below
 132              my $val = $args[$elt + 1];
 133              $choice_map[$i] = sub { no warnings 'exiting';
 134                                      $ret = $val;
 135                                      last LOOP;
 136                                  };
 137              $i++;
 138          }
 139  
 140          # If we have multiple pages, generate Next/Previous option.
 141          if ($pages > 1) {
 142              print "N/P) Next/Previous page\n";
 143              $choices .= 'N';
 144              $choice_map[$i] = sub { $page = ($page + 1) % $pages };
 145              $i++;
 146              $choices .= 'P';
 147              $choice_map[$i] = sub { $page = ($page + $pages - 1) % $pages };
 148              $i++;
 149          }
 150  
 151          print "X) Exit this program\n";
 152          $choices .= 'X';
 153          
 154          # user has chosen exit -> disable autologin
 155          $choice_map[$i] = sub { print "Exiting.\n"; system ("%z_path%\\bin\\autolog.pl --logon=0") ; exit 1; };
 156          
 157          $i++;
 158  
 159          my $sel = choice ('Select: ', $choices);
 160  
 161          my $func = $choice_map[$sel];
 162          &$func ();
 163      }
 164  
 165      # Record which page we ended up on
 166      $opts->{'page'} = $page;
 167  
 168      return $ret;
 169  }
 170  
 171  ## "choice" implementation, generic between DOS and Unix.
 172  sub choice ($;$) {
 173      my ($prompt, $choices) = @_;
 174      my $ret;
 175  
 176      defined $choices
 177          or $choices = 'YN';
 178  
 179      # Canonicalize stuff to uppercase
 180      $choices = uc $choices;
 181  
 182      if ($is_linux) {
 183          my %choice_map;
 184          foreach my $i (0 .. (length $choices) - 1) {
 185              my $char = substr $choices, $i, 1;
 186              $choice_map{$char} = $i;
 187          }
 188          print "$prompt [$choices] ";
 189          my $key;
 190          while (1) {
 191              $key = readkey ();
 192              $key = uc $key;
 193              (exists $choice_map{$key})
 194                  and last;
 195          }
 196          print "$key\n";
 197          $ret = $choice_map{$key};
 198      }
 199      else {
 200          system "$ENV{Z_PATH}\\bin\\choice.exe", "/c:$choices", $prompt;
 201          $ret = ($? >> 8) - 1;
 202      }
 203  
 204      return $ret;
 205  }
 206  
 207  
 208  # set autologin
 209  # autologinpwd passed from appsonly.bat? If yes, skip dialog
 210  if ($ENV{autologinpwd} eq "")
 211  {
 212    print "enable autologin (y/n)? ";
 213    chomp ($autologin = <STDIN>);
 214    if ($autologin eq "y")
 215    {
 216      print "autologin user will be: $ENV{USERNAME}\n";
 217      print "autologin domain will be: $ENV{USERDOMAIN}\n";
 218  
 219      print "autologin password: \n";
 220      $cons = new Win32::Console(STD_INPUT_HANDLE); 
 221      $oldMode = $cons->Mode; 
 222      $cons->Mode(~(ENABLE_LINE_INPUT|ENABLE_ECHO_INPUT) & $oldMode ); 
 223      while (1) 
 224      { 
 225        $char = $cons->InputChar(1) ;
 226        last if ord $char == 13 ; 
 227        print '*' ; 
 228        $autologinpwd .= $char ; 
 229      } 
 230      $cons->Mode($oldMode) ; 
 231      system("%z%\\bin\\autolog.pl --logon=1 --user=$ENV{USERNAME} --domain=$ENV{USERDOMAIN} --password=$autologinpwd");
 232    }
 233  }
 234  else
 235  {
 236      $autologinpwd = $ENV{autologinpwd};
 237      system("%z%\\bin\\autolog.pl --logon=1 --user=$ENV{USERNAME} --domain=$ENV{USERDOMAIN} --password=$autologinpwd");
 238  }
 239  
 240  # generate list of all bat files in the scripts directory
 241  chdir "$ENV{Z}\\scripts";
 242  @batfiles = glob("*.bat");
 243  
 244  
 245  # if .bat file names were passed from appsonly.bat -> use the given .bat file names
 246  # in other case, show the multiple choice dialog
 247  # select multiple bat files
 248  if ($ENV{script1} eq "") 
 249  {
 250    @selectedfiles = multi_choice('Please choose app(s) to install.', @batfiles);
 251  }
 252  else
 253  {
 254    if ($ENV{script8} ne "")
 255    {
 256        push @selectedfiles, $ENV{script8};
 257    }
 258    if ($ENV{script7} ne "")
 259    {
 260        push @selectedfiles, $ENV{script7};
 261    }
 262    if ($ENV{script6} ne "")
 263    {
 264        push @selectedfiles, $ENV{script6};
 265    }
 266    if ($ENV{script5} ne "")
 267    {
 268        push @selectedfiles, $ENV{script5};
 269    }
 270    if ($ENV{script4} ne "")
 271    {
 272        push @selectedfiles, $ENV{script4};
 273    }
 274    if ($ENV{script3} ne "")
 275    {
 276        push @selectedfiles, $ENV{script3};
 277    }
 278    if ($ENV{script2} ne "")
 279    {
 280        push @selectedfiles, $ENV{script2};
 281    }
 282    if ($ENV{script1} ne "")
 283    {
 284        push @selectedfiles, $ENV{script1};
 285    }
 286  }
 287  
 288  # put command to disable autologin (after all installs) onto the to-do stack
 289  system ("%z_path%\\bin\\todo.pl", "autolog.pl --logon=0");
 290  
 291  # call todo.pl for any selected bat file and call it once with --go
 292  $i = 0;
 293  #print $#selectedfiles;
 294  while ($i <= $#selectedfiles) 
 295  {
 296    #print $selectedfiles[$i];
 297    system ("%z_path%\\bin\\todo.pl", $selectedfiles[$i]);
 298    $i++;
 299  }
 300  
 301  system ("%z_path%\\bin\\todo.pl", "--go");
 302  


Generated: Tue Mar 17 22:47:18 2015 Cross-referenced by PHPXref 0.7.1