#!/usr/bin/perl -w
 #
 # load_docs.pl
 #
 # Summary: Loads html files into a mozilla instance. If mozilla is not loaded
 #          then fork an instance first. 
 #
 # Caveat:  This script requires mozilla.
 #
 use strict; my $pid; 
 
 my @docs = qw(                 
                file:///home/Notes.txt
                file:///usr/non-portage/fox-1.4.25/share/doc/doc.html
                file:///usr/non-portage/fox-1.4.25/share/doc/ref/index.html         
                http://www.google.com       
            );
 # Look for instance of mozilla.
 `mozilla -remote "ping()"`;    
          
 if ( $? ) { 
 # Error, mozilla is not running.           
    if (!defined($pid = fork())) { # Undef branch of fork:       
       die "Cannot fork: $!";      
    } elsif ($pid == 0) {          # Child branch of fork:       
       `mozilla`;                  # Start new instance.... 
    } else {                       # Parent branch of fork:        
       do {
          # Must ensure the child
          # instance is loaded. Is
          # there a system way to 
          # wait without relying 
          # on the -remote ping() 
          # feature... Without 
          # waiting there is a 
          # problem executing the 
          # sub to load the 
          # documentation.                      
          `mozilla -remote "ping()"`;     
          sleep 1;   
       } while ( $? );
                                   # Bugfix!!? we can -remote "ping()" 
       sleep 1;                    # but we can not   -remote openFile 
                                   # after that ... unless there is another
                                   # sleep here for some reason.
       load_docs();
       $pid=waitpid($pid, 0);      # Wait for the pseudo-process containing the
                                   # new mozilla instance to end.           
    }   
 } else { load_docs(); }
 
 sub load_docs {    
    my $element;
    foreach (0..$#docs){
      if ( $_ == 0 ) {
         if ( $docs[$_] =~ /~file:\/\// ) {
            `mozilla -remote "openFile($docs[$_])"` ; } 
         else {
            `mozilla -remote "openURL($docs[$_])"` ;  }
      } 
      else { 
         if ( $docs[$_] =~ /~file:\/\// ) {
            `mozilla -remote "openFile($docs[$_], new-tab)"` ; } 
         else {
            `mozilla -remote "openURL($docs[$_], new-tab)"` ; }      
      }
   }
 }
