#!/usr/bin/perl

use warnings;
use strict;

# predeclare subs because we use Win32::GuiTest not during BEGIN
use subs qw(SendKeys FindWindowLike WaitWindow);

# TODO does access run in wine?

unless(defined($ARGV[0]) && defined($ARGV[1]))
{
  die <<"END";
Usage: $0 <INPUT-FILENAME.MDB> <OUTPUT-FILENBAME.MDB>

Convert INPUT-FILENAME.MDB into OUTPUT-FILENAME.MDB using M\$ Access.
Tested with a German Access 2000 under Win98 and Win2k.

This script only runs under the POSIX emulation layer Cygwin under Windows.

END
}
die "File not found: $ARGV[0]" unless -f $ARGV[0];

if($^O ne 'cygwin')
{
  print STDERR "This script only runs under the "
    . "POSIX emulation layer Cygwin under Windows.\n";
  exit 2
}

# Do this now only to allow to start and print help even without this module.
# A version number of 1.50.5 gives an error - it is invalid.
# So we can't test the version here completely.
eval 'use Win32::GuiTest 1.50 qw(:ALL)';
die $@ if $@;

if($Win32::GuiTest::VERSION lt '1.50.5')
{
  warn <<"END";
WaitWindow() doesn't exist in Win32::GuiTest 1.50.3, which you get from CPAN
currently.  Get 1.50.5 from
http://sourceforge.net/project/showfiles.php?group_id=104592 and apply the
patch to build in Cygwin from
http://sourceforge.net/tracker/index.php?func=detail&aid=1446410&group_id=104592&atid=638614
END
}

my $mdb = `cygpath -wa "$ARGV[0]"`;
chomp $mdb;
warn "Filename does not end in .mdb" unless $mdb =~ /(.*)\.mdb$/;
my $jet4 = $ARGV[1];
my $jet4_quoted = quotemeta $jet4;
my $jet4_cygwin = `cygpath -u $jet4_quoted`;
chomp $jet4_cygwin;
if(-f $jet4_cygwin)
{
  print "$jet4 already exists - exit\n";
  exit
}
print "Will convert $mdb into $jet4 ($jet4_cygwin)\n";

# in Win98 'start' is a separate program in PATH, found by bash
system 'start msaccess.exe';
if($? == -1)
{
  # in Win2k start is integrated into cmd.exe
  system 'cmd /c start msaccess.exe';
}
die "Failed to start msaccess.exe" if $? == -1;
die "msaccess.exe died with exit status ".($?&127) if $? & 127;

sleep 2;
my @ws = FindWindowLike undef, 'Microsoft Access';
die "No window 'Microsoft Access' found" unless @ws;

my $OMain;
foreach (@ws)
{
  my $c = GetClassName($_);
  print "window handle: $_ class: $c\n";
  $OMain = $_ if($c eq 'OMain');
  if($c eq '#32770')
  {
    SendKeys("~$mdb~");
    # This string makes us fail with non German accesses
    my $convertOrOpen = WaitWindow /^(Datenbank konvertieren\/ffnen)$/, 2;
    die "Could not find the dialog which asks whether convert or open"
      unless $convertOrOpen;
    SendKeys "{PAUSE}~{PAUSE}$jet4~";
    sleep 9;
    #MenuSelect '&Datei|&Beenden';
    #MenuSelect "&Schlieen", 0, GetSystemMenu($OMain, 0);
    SendKeys '%DB';
    print "Access controlled successfully\n";
    exit 0
  }
}

die "Access controlling failed"

