#!perl -w

# Perl program Copyright (c)2001 Soren Andersen
#     e-mail:    <libertador@flashmail.com>
#  Licensing: Perl Artistic License.
#  L/M 1/6/01 4:09 AM
#  requires Cygwin 1.1 or later, maybe -- has not been
#  tested on earlier Cygwin versions. `mount' and `cygpath'
#  must be in your PATH or fully qualified by hand-editing
#  this script.

use Win32::API;
use Getopt::Std;
use strict;

use vars qw[ $opt_n $opt_f $opt_t $srt ];
sub DrivesInUse;

#  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#  Options:
#      -n		dry-run only
#      -f		mount floppy drives too
#      -t		just see existing mount points and exit

 getopts( 'nft' );   $srt = 0;

#  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 if ($opt_t)   {
	 my $rcd = system("mount");
	 exit 0;
 }
my $floppies = ($opt_f)? '0' : '[a-b]';
my $cygprfx  = join q[], grep( /\/[a-z]+/i,
         `mount --show-cygdrive-prefixes` );
chomp $cygprfx;
$cygprfx =~s@^(/[a-z]+).+@$1@;
my $cygdrive = `cygpath -paw $cygprfx`;
chomp $cygdrive;
( $cygdrive =~s@^([A-Z]) \: .*@$1@xi ) or die qq[Careful, need good `cygdrive'.\n];

 for (DrivesInUse()) {
  next if /(?:$cygdrive | $floppies)/xi;
  printf(qq[  %1.3s:  mounting to %2.4s\n], $_, q[/]. lc);
 ( $srt = system( "mount -b -s -f", $_ .":","/". lc ) ) unless $opt_n;
  warn qq[\n Trouble making the mounted drive with $_ - ] .
  qq[ret code not zero: $srt\n $!] if $srt;
 }


 # from "Win32::DriveInfo.pm ver 0.03" by Mike Blazer
#===========================
sub DrivesInUse   {
#===========================
no strict 'subs';
   my (@dr, $i);
   my $h = new Win32::API("kernel32", "GetLogicalDrives", [], N);
   return undef unless $h =~/Win32::API/i;
   my $bitmask = $h->Call();
    for $i(0..25) {
       push (@dr, (A..Z)[$i]) if $bitmask & 2**$i;
    }
   return @dr;
}
