# $Id: Portfile 52879 2009-06-25 02:13:26Z ryandesign@macports.org $

PortSystem              1.0

name                    minivmac-devel
set my_name             minivmac
version                 3.1.0-20090402
revision                5
set version_number      [lindex [split ${version} -] 0]
set version_date        [lindex [split ${version} -] 1]
set version_short_date  [string range ${version_date} 2 end]
categories              emulators aqua
maintainers             ryandesign
homepage                http://minivmac.sourceforge.net/
use_zip                 yes
platforms               macosx
universal_variant       no
build.target
use_parallel_build      yes
dist_subdir             ${my_name}

description \
    a Mac 128K, Mac 512K, Mac 512KE, Mac Plus and Mac SE emulator

long_description \
    Mini vMac is a Macintosh emulator. It emulates the earliest Macs, \
    from the original Mac 128K (built 1984-85) to the Mac SE (1987-1990). \
    The default is to emulate a Mac Plus (1986-1990)\; this is also \
    the best-tested and therefore recommended emulation.

master_sites            sourceforge:${my_name}

distname                ${my_name}${version_short_date}
set my_src              ${distname}.src
set my_src_distfile     ${my_src}.zip
set my_icons            icnsosx-1.0.0
set my_icons_distfile   ${my_icons}.zip

distfiles \
    ${my_src_distfile} \
    ${my_icons_distfile}

checksums \
    ${my_src_distfile} \
        md5     7defdcb859cdda0df3bdc3c252370a70 \
        sha1    f4a80b326380746b88993d53eeb6a7d46768358b \
        rmd160  828eb032ea3ac0f52054d0c8af1d6910ded1dff0 \
    ${my_icons_distfile} \
        md5     2af006506de7549be14f94317918d59a \
        sha1    4ef79bb56b988fedfeb82a8b6cafd8066f5bbab2 \
        rmd160  cef6d7d7578464538d0042b0d98379ae7f18fab7

patchfiles \
    patch-build.diff \
    patch-gcc.diff

set my_appdir           "${applications_dir}/Mini vMac"
set my_target           ""

#   variation_id    variation_suffix    variation_options
set my_variations {
    128K            "128K"              {-m 128K}
    512K            "512K"              {-m 128K -mem 512K}
    512Ke           "512Ke"             {-m 512Ke}
    Plus            "Plus"              {-m Plus -im 1}
    SE              "SE"                {-m SE}
    SEFDHD          "SE FDHD"           {-m SEFDHD}
    Classic         "Classic"           {-m Classic}
}

set my_romfiles {
    Mac128K.ROM
    vMac.ROM
    MacSE.ROM
    SEFDHD.ROM
    Classic.ROM
}

variant unsupported description {Also build unfinished Macintosh II and IIx emulators} {
    set my_variations [concat ${my_variations} {
        II          "II"                {-m II -mem 8M -hres 800 -vres 600 -depth 3}
        IIx         "IIx"               {-m IIx -mem 8M -hres 800 -vres 600 -depth 3}
    }]
    set my_romfiles [concat ${my_romfiles} {
        MacII.ROM
        MacIIx.ROM
    }]
}

platform powerpc {
    set my_target mach
}

platform i386 {
    set my_target imch
}

post-install {
    set found 0
    foreach my_romfile ${my_romfiles} {
        if {[file exists ${my_appdir}/${my_romfile}]} {
            ui_debug "Found ROM file ${my_appdir}/${my_romfile}"
            set found 1
        }
    }
    if {!${found}} {
        ui_msg "****************************************************************"
        ui_msg "Mini vMac requires a ROM file from the type of machine(s) you're"
        ui_msg "emulating in order to work. 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_appdir}."
        ui_msg "****************************************************************"
    }
}

post-extract {
    xinstall -W ${filespath} runbuild.sh ${workpath}
    
    # The disk image mounter likes images to have the .img extension.
    file rename ${workpath}/${my_src}/${my_src}.dsk ${workpath}/src.img
    
    # Mount the source disk image.
    set my_src_disk_mount [my_attach_disk_image ${workpath}/src.img 0]
    
    # Copy the source from the disk image into the workpath.
    file mkdir ${worksrcpath}
    foreach thing {COPYING.txt README.txt output projects source} {
        copy ${my_src_disk_mount}/${thing} ${worksrcpath}
    }
    
    # Unmount the disk image.
    my_detach_disk_image ${my_src_disk_mount}
    
    # Convert CR to LF line endings.
    fs-traverse thing ${worksrcpath} {
        if {[file isfile ${thing}]} {
            foreach extension {.a .c .h .i .plist .r Makefile} {
                if {[string match "*${extension}" ${thing}]} {
                    reinplace "s/\r/\\\n/g" ${thing}
                }
            }
        }
    }
}

build.env-append        CC=${configure.cc}

configure {
    # Build the build system.
    if {![file exists ${worksrcpath}/Build.app]} {
        set my_cmd "cd ${worksrcpath}/projects/gcc_${my_target} && ${build.env} [build_getnicevalue] make [build_getmakejobs]"
        ui_debug ${my_cmd}
        system ${my_cmd}
        file rename ${worksrcpath}/projects/gcc_${my_target}/Build.app ${worksrcpath}
    }
    
    file mkdir ${workpath}/${my_target}
    foreach {variation_id variation_suffix variation_options} ${my_variations} {
        ui_debug "Configuring ${variation_id}"
        
        # Due to limitations of the build system, the variation name cannot be longer than 27 characters.
        set variation_name ${version}_${revision}-${variation_id}
        
        # Write our options file.
        set my_options_file ${workpath}/${variation_name}.txt
        set my_options "-maintainer MacPorts -homepage http://www.macports.org/ -cl -eol unx -nex -t ${my_target} -n ${variation_name} ${variation_options}"
        set my_options_file_id [open ${my_options_file} "w"]
        puts -nonewline ${my_options_file_id} ${my_options}
        close ${my_options_file_id}
        
        # Run the build system.
        set my_cmd "${workpath}/runbuild.sh ${worksrcpath} ${my_options_file}"
        ui_debug ${my_cmd}
        system ${my_cmd}
        
        # Get the build product.
        file rename ${worksrcpath}/output/${variation_name} ${workpath}/${my_target}/${variation_id}
    }
}

build {
    foreach {variation_id variation_suffix variation_options} ${my_variations} {
        ui_debug "Building ${variation_id}"
        
        build.dir ${workpath}/${my_target}/${variation_id}
        set my_cmd "cd ${build.dir} && ${build.env} [build_getnicevalue] ${build.cmd} ${build.target} [build_getmakejobs]"
        ui_debug ${my_cmd}
        system ${my_cmd}
        
        # Only keep a single copy of the Resources to save space.
        if {![file exists ${workpath}/Resources]} {
            copy ${workpath}/${my_target}/${variation_id}/minivmac.app/Contents/Resources ${workpath}
        }
        delete ${workpath}/${my_target}/${variation_id}/minivmac.app/Contents/Resources
    }
}

destroot {
    set my_sharedir ${prefix}/share/${my_name}/
    xinstall -d ${destroot}${my_sharedir}
    copy ${workpath}/Resources ${destroot}${my_sharedir}
    eval xinstall -m 644 [glob ${workpath}/${my_icons}/icons/*.icns] ${destroot}${my_sharedir}/Resources
    
    xinstall -d ${destroot}${my_appdir}
    foreach {variation_id variation_suffix variation_options} ${my_variations} {
        ui_debug "Staging ${variation_id}"
        
        set my_app "${my_appdir}/Mini vMac ${variation_suffix}.app"
        file copy ${workpath}/${my_target}/${variation_id}/minivmac.app \
            ${destroot}${my_app}
        ln -s ${my_sharedir}/Resources ${destroot}${my_app}/Contents
    }
}

proc my_attach_disk_image {disk_image allow_writing} {
    global name workpath
    set mountpoint [exec mktemp -d -q -t ${name}]
    if {${allow_writing}} {
        set flag "-readwrite"
    } else {
        set flag "-readonly"
    }
    set my_cmd "hdiutil attach ${disk_image} -mountpoint ${mountpoint} -private -nobrowse -noautoopen -noautofsck -noverify ${flag}"
    ui_debug ${my_cmd}
    system ${my_cmd}
    return ${mountpoint}
}

proc my_detach_disk_image {mountpoint} {
    set my_cmd "hdiutil detach ${mountpoint} -force"
    ui_debug ${my_cmd}
    system ${my_cmd}
    file delete -force ${mountpoint}
}

livecheck.check         regex
livecheck.version       ${version_short_date}
livecheck.url           ${homepage}develop/
livecheck.regex         ${my_name}(\[0-9\]+)\\.src
