#!perl
#-----------------------------------------------------------------#
#  whichMacPerl
#  pudge
#  Check which version of MacPerl to install
#
#  Created:       Chris Nandor (pudge@pobox.com)         04-Dec-97
#  Last Modified: Chris Nandor (pudge@pobox.com)         17-Dec-97
#
#  Additional modifications by Alan Fry <ajf@afco.demon.co.uk>, 
#    Paul Schinder <schinder@leprss.gsfc.nasa.gov>,
#    Matthias Neeracher <neeri@iis.ee.ethz.ch>,
#    Erryn Duncan <errynd@dlux.net>.
#
#  Testing thanks to Mark Howard, Andreas Marcel Riechert, 
#    Tim Rand, Mark Manning, Alex Satrapa, Marcus Sen.
#-----------------------------------------------------------------#
#  The runtime application version of this script ("whichMacPerl") 
#  will run on any Mac with System 7.  The text version of this 
#  script ("whichMacPerl.pl") will run on any Mac that has both 
#  the MacPerl 5.1.5r4 (or better) distribution and the BigMacPerl 
#  distribution.
#-----------------------------------------------------------------#
use Mac::Files;
use Mac::Resources;
use Mac::Memory;
use Mac::Gestalt;
MacPerl::Quit(1);    # only quit if a Runtime
my($ext, %files, %yes, @which, $reg, $vers, $file, $prompt, $id, $nvers, $reqs);

#==========================================================#
# Init Vars
#==========================================================#
$ext = FindFolder(kOnSystemDisk(),kExtensionFolderType());
%files = (
  'ObjectSupportLib',['1.2',"$ext:ObjectSupportLib"],
  'AppleScriptLib',['1.2.2',"$ext:AppleScriptLib"],
  'CFM-68K Runtime Enabler',['4.0',
    (
      -e "$ext: CFM-68K Runtime Enabler" ?
      "$ext: CFM-68K Runtime Enabler" :
      "$ext:CFM-68K Runtime Enabler"
    )
  ],
);
$reg = 'I recommend installing the regular MacPerl distribution';
%which = (
  'appl',"$reg.",
  'bigappl',"$reg\nand then the BigMacPerl distribution.\n\n" .
            "Alternatively, follow the above upgrade tips and install\n" .
            'the CFM-68K distribution instead of the BigMacPerl distribution.',
  'cfm68k',"$reg and then the CFM-68K distribution.",
);

$upgrade = 0;

#==========================================================#
# Main Checks
#==========================================================#
if ($Gestalt{gestaltSysArchitecture()} eq gestaltPowerPC()) {
  print "You're running on a PowerPC-based Mac OS system.\n\n";
  if ($Gestalt{gestaltSystemVersion()} >= hex(800)) {
    foreach (keys %files) {
      &checkVers($files{$_}->[0], $files{$_}->[1], $_)
        if (/AppleScriptLib/);
    }
    print "\nWARNING: You need to have the above extensions up to date.\n",
          "They should be available on your System disks.\n\n"
      if $upgrade;
  } else {
    foreach (keys %files) {
      &checkVers($files{$_}->[0], $files{$_}->[1], $_)
        if (/AppleScriptLib|ObjectSupportLib/);
    }
    print "\nWARNING: You need to have the above extensions up to date.\n",
          "They should be available on your System disks.\n\n"
      if $upgrade;
    print "I recommend strongly upgrading to Mac OS 7.5.3 or greater.\n\n"
      if ($Gestalt{gestaltSystemVersion()} < hex(753));
  }
  recommend('appl');
} else {
  print "You're running on a Motorola 680x0-based Mac OS system.\n\n";
}
if ($Gestalt{gestaltProcessorType()} < gestalt68020()) {
  print "Wow! You seem to run on a rather old Mac.\n",
        "I'm afraid there is not much advice I can give you here.\n\n";
  recommend('appl');
}
foreach (keys %files) {
  if ($Gestalt{gestaltSystemVersion()} >= hex(800)) {
    &checkVers($files{$_}->[0], $files{$_}->[1], $_)
      if (/AppleScriptLib/);
  } else {
    &checkVers($files{$_}->[0], $files{$_}->[1], $_);
  }
}
print "\n";
if ($Gestalt{gestaltSystemVersion()} < hex(753)) {
  print "I'd recommend upgrading to Mac OS 7.5.3 or greater.\n\n";
  $upgrade = 1;
}
if ($upgrade) {recommend('bigappl')}
else {recommend('cfm68k')}

#==========================================================#
# Recommend Version
#==========================================================#
sub recommend {
  print "$which{$_[0]}\n\n";
  print "Hit the <RETURN> or <ENTER> key to quit.\n";
  while(<STDIN>){exit}
}

#==========================================================#
# Check Library Versions
#==========================================================#
sub checkVers {
  ($vers, $file, $prompt, $reqs, $id, $nvers) = @_;
  if (defined(OpenResFile($file))) {
    $id = Get1Resource('vers',1);
    if (defined($id)) {
      $nvers = $id->get();
    }
    CloseResFile(CurResFile());
  }
  $nvers =~ s/^.*?([\d\.]+), ©.*$/$1/m;
  write;
  $upgrade = $upgrade || !($nvers && $vers && $nvers ge $vers);
}

#==========================================================#
# Library Version Formats
#==========================================================#
format STDOUT_TOP =
Checking the versions of some system files:
  File                         Version Needed  Version Present
  ---------------------------  --------------  ---------------
.
format STDOUT =
  @<<<<<<<<<<<<<<<<<<<<<<<<<<  @>>>>>>>>>>>>>  @>>>>>>>>>>>>>>
  $prompt,                     $vers,          $nvers
.

#==========================================================#
