# $Id: Portfile 67388 2010-05-07 06:34:07Z ryandesign@macports.org $

PortSystem              1.0

name                    minivmac
conflicts               minivmac-devel
set my_name             minivmac
version                 3.1.3
categories              emulators aqua
maintainers             ryandesign
license                 GPLv2
homepage                http://minivmac.sourceforge.net/
use_zip                 yes
platforms               macosx
universal_variant       no
use_parallel_build      yes
dist_subdir             ${my_name}
distname                ${my_name}-${version}

description             an emulator of the Macintosh Plus and other early Macintosh models

long_description        The Mini vMac emulator helps preserve software made for early \
                        Macintosh computers -- those sold by Apple from 1984 to 1996 \
                        based on Motorola's 680x0 microprocessors. \
                        The best-tested version emulates a Macintosh Plus, but there are \
                        also emulations of the Macintosh 128K, 512K, 512Ke, SE, Classic, \
                        and SE FDHD, and work on Macintosh II emulation is in progress.

master_sites            sourceforge:${my_name}:minivmac \
                        macports:${my_name}:macports \
                        http://download.info.apple.com/Apple_Support_Area/Apple_Software_Updates/English-North_American/Macintosh/System/Older_System/System_7.0.x/:apple

set my_src                                  ${distname}.src
set my_src_distfile                         ${my_src}.zip
set my_bootstrap_distfile                   ${my_name}-bootstrap-3.1.2_1.zip
set my_blanks                               blanks
set my_blanks_distfile                      ${my_blanks}-0.2.0.zip
set my_clipin                               clipin-1.1.0
set my_clipin_distfile                      ${my_clipin}.zip
set my_icons                                icnsosx-1.0.0
set my_icons_distfile                       ${my_icons}.zip
set my_system_disk_container                System_7.0.1.smi
set my_system_disk_container_distfile       ${my_system_disk_container}.bin

distfiles               ${my_src_distfile}:minivmac \
                        ${my_bootstrap_distfile}:macports \
                        ${my_icons_distfile}:minivmac \
                        ${my_clipin_distfile}:minivmac \
                        ${my_blanks_distfile}:minivmac \
                        ${my_system_disk_container_distfile}:apple

extract.only            ${my_src_distfile} \
                        ${my_bootstrap_distfile} \
                        ${my_icons_distfile} \
                        ${my_blanks_distfile} \
                        ${my_clipin_distfile}

checksums               ${my_src_distfile} \
                        md5     5243ae167cc00920e03b4abca2b40af5 \
                        sha1    b9967ac258baf1a15785e884c306bf0d42097d45 \
                        rmd160  2af69ebf5420f8f8d0baece03664dc7326e13e38 \
                        ${my_bootstrap_distfile} \
                        md5     65fa9b7fb19a24dc6c9e711387c1bc30 \
                        sha1    4ad44559e75894d45e91ae62cc7ddb420062f968 \
                        rmd160  58884cac487a537b5d00fc6093fdfecb6ccc5a2f \
                        ${my_icons_distfile} \
                        md5     2af006506de7549be14f94317918d59a \
                        sha1    4ef79bb56b988fedfeb82a8b6cafd8066f5bbab2 \
                        rmd160  cef6d7d7578464538d0042b0d98379ae7f18fab7 \
                        ${my_clipin_distfile} \
                        md5     e094de97d1c6c214a80fb74895124c3e \
                        sha1    725f622ad6681c77e7f9f4a64d05ac1d6a103beb \
                        rmd160  5699b56c8db62b2568c8de88b6d419180b1fb73d \
                        ${my_blanks_distfile} \
                        md5     b16321d0d78048d5ec9b635f4f86b19c \
                        sha1    9b8d7e77bd05633709907f8806d976279d7a20b7 \
                        rmd160  a0784c17c5a5ca2e159762fb836c1c800762b213 \
                        ${my_system_disk_container_distfile} \
                        md5     8a4776350b15b5a516cdf7dd18e27290 \
                        sha1    d4c8b8653de9c7a9b2594dd6126ec7120e3e2e9e \
                        rmd160  89fc3780c9c5d575a8da4068dc810c1f56c70211

depends_extract         port:bsdiff \
                        bin:expect:expect

if {${os.major} < 9} {
    depends_extract-append port:macutil
}

set my_app_dir          "${applications_dir}/Mini vMac"
set my_rom_dir          "~/Library/Preferences/Gryphel/mnvm_rom"
set my_bootstrap_app    "Mini vMac Bootstrap.app"
set my_rom_file         vMac.ROM
set my_base_options     "-maintainer MacPorts -homepage http://www.macports.org/ -cl -eol unx -nex"
set my_archs            {}

options my_variations
my_variations           {128K}      {-m 128K} \
                        {512K}      {-m 128K -mem 512K} \
                        {512Ke}     {-m 512Ke} \
                        {Plus}      {-m Plus -im 1} \
                        {SE}        {-m SE} \
                        {SE FDHD}   {-m SEFDHD} \
                        {Classic}   {-m Classic}

variant unsupported description {Also build unfinished Macintosh II and IIx emulators} {
    my_variations-append \
                        {II}        {-m II -mem 8M -hres 800 -vres 600 -depth 3} \
                        {IIx}       {-m IIx -mem 8M -hres 800 -vres 600 -depth 3}
}

# The universal variant is disabled until MacPorts 1.8.3 is released.
if {0} {
# To build the bootstrap, use "sudo port install bootstrap=yes".
# The variant is hidden because it is useful only to the port maintainer.
if {[tbool bootstrap]} {
    variant bootstrap conflicts unsupported requires universal description {Build the bootstrap version of Mini vMac} {
        my_variations   {Bootstrap} {-m Plus -sound 0 -bg 1 -speed a}
        depends_build-append port:p7zip
        post-destroot {
            exec 7za a ${destroot}${my_app_dir}/${my_name}-bootstrap-${version}_${revision}.zip ${destroot}${my_app_dir}/${my_bootstrap_app}
        }
    }
    default_variants +bootstrap
}

variant universal {
    configure.universal_args
    configure.universal_archs ppc i386
    set my_archs ${configure.universal_archs}
}
}
if {[info exists supported_archs]} {
    supported_archs i386 ppc
}
if {![variant_exists universal] || ![variant_isset universal]} {
    switch ${os.arch} {
        powerpc {
            configure.build_arch ppc
        }
        default {
            configure.build_arch ${os.arch}
        }
    }
    set my_archs ${configure.build_arch}
}

pre-fetch {
    if {"" == [my_real_rom_file]} {
        ui_msg "****************************************************************"
        ui_msg "To build Mini vMac, you will need a ROM file from a Macintosh"
        ui_msg "Plus or 512Ke. And to run it, you need a ROM file from the type"
        ui_msg "of Mac you want to emulate. ROM files are not included with this"
        ui_msg "package because they are copyrighted Apple software."
        ui_msg "To create a ROM file, download the CopyRoms program from"
        ui_msg "${homepage}extras/copyroms.html"
        ui_msg "and transfer it to a real physical early Macintosh that you own."
        ui_msg "Run the CopyRoms program there, then transfer the ROM file back"
        ui_msg "to this Mac and put it in ${my_rom_dir}"
        ui_msg "(which can be an alias or symlink if desired) or"
        ui_msg "${my_app_dir}."
        ui_msg "****************************************************************"
        return -code error "${my_rom_file} not found"
    }
}

worksrcdir              stuff
extract.mkdir           yes

post-extract {
    # Decode the system disk container image.
    my_system "sh ${filespath}/demacbinary.sh ${distpath}/${my_system_disk_container_distfile} ${worksrcpath}"
    
    # Copy the Disk Tools image from the container image. The binary patchfile applied with bspatch
    # places an alias of the output disk at System Folder:Preferences:Gryphel:Build:output.
    set my_system_disk_container_mount [my_attach_disk_image ${worksrcpath}/${my_system_disk_container}]
    my_system "bspatch '${my_system_disk_container_mount}/Disk Tools.image' '${worksrcpath}/Disk Tools.dsk' '${filespath}/Disk Tools.dsk.bsdiff'"
    my_detach_disk_image ${my_system_disk_container_mount}
    
    # Get a large empty disk image to store the output.
    my_system "unzip -q ${worksrcpath}/${my_blanks}/hfs24M.zip -d ${worksrcpath}"
    
    # Make the data directory, where Mini vMac looks for the files it needs.
    set my_mnvm_dat "${worksrcpath}/${my_bootstrap_app}/Contents/mnvm_dat"
    file mkdir ${my_mnvm_dat}
    
    # Symlink the ROM file into the data directory.
    ln -s [my_real_rom_file] ${my_mnvm_dat}
    
    # Symlink the disk images into the data directory with sequentially-numbered
    # names so Mini vMac will find them and automatically mount them.
    ln -s "${worksrcpath}/Disk Tools.dsk" "${my_mnvm_dat}/disk1.dsk"
    ln -s ${worksrcpath}/output.img "${my_mnvm_dat}/disk2.dsk"
    ln -s ${worksrcpath}/${my_src}/${my_src}.dsk "${my_mnvm_dat}/disk3.dsk"
    ln -s ${worksrcpath}/${my_clipin}/${my_clipin}.dsk "${my_mnvm_dat}/disk4.dsk"
}

pre-configure {
    # Set the name of the bootstrap app in the configure script.
    xinstall -m 755 ${filespath}/configure.applescript.in ${worksrcpath}/configure.applescript
    reinplace "s|@BOOTSTRAP_APP@|${my_bootstrap_app}|g" ${worksrcpath}/configure.applescript
    
    # Copy the large blank disk image to store the output.
    # Use the .img extension so that we can mount it on Mac OS X later.
    copy -force ${worksrcpath}/hfs24M.DSK ${worksrcpath}/output.img
    
    # Unrestrict the permissions on the output disk image so the Build program
    # can write its output to it.
    file attributes ${worksrcpath}/output.img -permissions 0777
    
    # Build up the combined configure args for all variations.
    set i 0
    foreach my_arch ${my_archs} {
        set my_target [my_arch_to_target ${my_arch}]
        foreach {my_machine my_variation_options} ${my_variations} {
            if {${i} > 0} {
                configure.args-append {\;}
            }
            configure.args-append \
                ${my_base_options} \
                -t ${my_target} \
                ${my_variation_options} \
                -n ${version}_${revision}-[strsed [string tolower ${my_machine}] {g/[^a-z0-9_-]//}]-${my_target}
            incr i
        }
    }
    
    # Launch the bootstrap Mini vMac so the configure script can talk to it.
    global my_bootstrap_pid
    if {[catch {exec "${worksrcpath}/${my_bootstrap_app}/Contents/MacOS/minivmac" &} my_bootstrap_pid]} {
        return -code error "couldn't launch bootstrap"
    }
}

configure.cmd           osascript
configure.pre_args      ./configure.applescript

build.dir               ${workpath}/variations

post-configure {
    # Kill the bootstrap Mini vMac.
    global my_bootstrap_pid
    exec kill ${my_bootstrap_pid}
    
    file mkdir ${build.dir}
    set my_variation_dirs {}
    set my_output_disk_mount [my_attach_disk_image ${worksrcpath}/output.img]
    foreach my_arch ${my_archs} {
        file mkdir ${build.dir}/${my_arch}
        set my_target [my_arch_to_target ${my_arch}]
        foreach {my_machine unused} ${my_variations} {
            set my_src_dir ${my_output_disk_mount}/${version}_${revision}-[strsed [string tolower ${my_machine}] {g/[^a-z0-9_-]//}]-${my_target}
            if {[file isdirectory ${my_src_dir}]} {
                set my_variation_dir ${my_arch}/${my_machine}
                lappend my_variation_dirs [strsed ${my_variation_dir} {g/ /\\\\ /}]
                
                # Get the configured source code.
                file copy ${my_src_dir} ${build.dir}/${my_variation_dir}
                
                # Use the right compiler.
                reinplace "s|gcc|${configure.cc}|g" ${build.dir}/${my_variation_dir}/Makefile
                
                # Remove references to SDKs -- the 10.4u SDK is not installed by default on 10.6
                # and even if you install it manually it does not work here.
                reinplace -E {s|-isysroot /Developer/SDKs/[^ ]+||g} ${build.dir}/${my_variation_dir}/Makefile
            }
        }
    }
    my_detach_disk_image ${my_output_disk_mount}
    
    # Set up the global Makefile.
    xinstall ${filespath}/Makefile.in ${build.dir}/Makefile
    reinplace "s|@SUBDIRS@|[join ${my_variation_dirs}]|" ${build.dir}/Makefile
    
    # If no variation directories were produced by configuration, either the flags we
    # passed in were bad, or the automation script got out of sync with the emulator;
    # if the latter, the delays in configure.applescript may need to be adjusted.
    if {"" == ${my_variation_dirs}} {
        return -code error "configuration failed!"
    }
}

set my_share_dir        ${prefix}/share/${my_name}

post-build {
    set i 0
    foreach my_arch ${my_archs} {
        set my_target [my_arch_to_target ${my_arch}]
        foreach {my_machine unused} ${my_variations} {
            if {[variant_exists universal] && [variant_isset universal]} {
                reinplace "s|${my_target}|umch|" ${build.dir}/${my_arch}/${my_machine}/minivmac.app/Contents/Info.plist
            }
            if {![variant_exists bootstrap] || ![variant_isset bootstrap]} {
                # To save space, keep only a single copy of the Resources directory.
                set my_resources_dir ${build.dir}/${my_arch}/${my_machine}/minivmac.app/Contents/Resources
                if {0 == ${i}} {
                    move ${my_resources_dir} ${build.dir}
                } else {
                    delete ${my_resources_dir}
                }
                ln -s ${my_share_dir}/Resources ${my_resources_dir}
            }
            incr i
        }
    }
    
    if {![variant_exists bootstrap] || ![variant_isset bootstrap]} {
        # Install the prettier icons.
        eval xinstall -m 644 [glob ${worksrcpath}/${my_icons}/icons/*.icns] ${build.dir}/Resources
    }
}

destroot {
    if {![variant_exists bootstrap] || ![variant_isset bootstrap]} {
        # Install the global Resources directory.
        xinstall -d ${destroot}${my_share_dir}
        copy ${build.dir}/Resources ${destroot}${my_share_dir}
    }
    
    # Install the programs.
    foreach my_arch ${my_archs} {
        if {[variant_exists universal] && [variant_isset universal]} {
            set my_dest ${workpath}/pre-dest/${my_arch}
        } else {
            set my_dest ${destroot}
        }
        xinstall -d ${my_dest}${my_app_dir}
        foreach {my_machine unused} ${my_variations} {
            copy ${build.dir}/${my_arch}/${my_machine}/minivmac.app \
                "${my_dest}${my_app_dir}/Mini vMac ${my_machine}.app"
        }
    }
    if {[variant_exists universal] && [variant_isset universal]} {
        merge ${workpath}/pre-dest
    }
}

# Returns the full path to the ROM file, wherever the user has it,
# or the empty string if the user doesn't have it.
proc my_real_rom_file {} {
    global filespath my_app_dir my_rom_dir my_rom_file
    if {[file exists ${my_app_dir}/${my_rom_file}]} {
        return ${my_app_dir}/${my_rom_file}
    }
    set my_real_rom_dir [exec osascript ${filespath}/readalias.applescript [file normalize ${my_rom_dir}]]
    if {[file exists ${my_real_rom_dir}/${my_rom_file}]} {
        return ${my_real_rom_dir}/${my_rom_file}
    }
    return ""
}

# Converts normal arch names into Mini vMac target names.
proc my_arch_to_target {arch} {
    switch ${arch} {
        i386 {
            return imch
        }
        ppc {
            return mach
        }
        default {
            return -code error "unsupported architecture ${arch}"
        }
    }
}

# Mounts a disk image.
proc my_attach_disk_image {disk_image} {
    global my_name filespath
    set mountpoint [mkdtemp "/tmp/${my_name}.XXXXXXXX"]
    # Use this expect script instead of using hdiutil directly, because the
    # system disk image has a license agreement we must agree to.
    my_system "expect ${filespath}/attach.exp '${disk_image}' '${mountpoint}'"
    return ${mountpoint}
}

# Unmounts a disk image.
proc my_detach_disk_image {mountpoint} {
    my_system "hdiutil detach ${mountpoint} -force"
    file delete -force ${mountpoint}
}

proc my_system {cmd} {
     ui_debug ${cmd}
     system ${cmd}
}

livecheck.type          regex
livecheck.url           ${homepage}doc/download.html
livecheck.regex         ${my_name}-(\[0-9.\]+)\\.src
