# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem                  1.0

name                        mystonline-cider
set my_name                 mystonline
version                     2010
revision                    4
platforms                   darwin
categories                  games x11
maintainers                 ryandesign
homepage                    http://mystonline.com/
dist_subdir                 ${my_name}
master_sites                http://archives.gametap.com/static/uru-client/mac/
set installer_dmg           mac_myst_setup.dmg
set launcher_exe            UruLauncher.exe
distfiles                   ${installer_dmg}
extract.mkdir               yes
extract.only

description                 Myst Online: URU Live Again

long_description            ${description} (MO:ULagain) running via the Cider wrapper from GameTap

checksums                   md5     3b3b5af000b32a4415e6aa7d944083fb \
                            sha1    253e14987a9375d66f92373b11e2dda43872b2a8 \
                            rmd160  c156b0edd20a691248269f6e3ab9996fddae74c8

depends_run                 port:mystonline-bootstrap

set libexec_dir             ${prefix}/libexec/${name}
set share_dir               ${prefix}/share/${my_name}
set bootstrap_dir           ${share_dir}/bootstrap
set assets_dir              ${share_dir}/data
set wineprefix              ${share_dir}/cider
set app_name                "Myst Online (Cider)"
set app_package             ${applications_dir}/${app_name}.app

post-extract {
    set my_dmg_mount [my_attach_disk_image ${distpath}/${installer_dmg}]
    copy "${my_dmg_mount}/Myst Online.app" ${worksrcpath}/app
    my_detach_disk_image ${my_dmg_mount}
    
    xinstall -m 644 -W ${filespath} MOUL.sh chown-data.c ${worksrcpath}
}

post-patch {
    reinplace "s|@BOOTSTRAP_LAUNCHER@|${bootstrap_dir}/${launcher_exe}|g" ${worksrcpath}/MOUL.sh
    reinplace "s|@LAUNCHER@|${assets_dir}/${launcher_exe}|g" ${worksrcpath}/MOUL.sh
    reinplace "s|@LIBEXEC@|${libexec_dir}|g" ${worksrcpath}/MOUL.sh
    reinplace -W ${worksrcpath} "s|@WINEPREFIX@|${wineprefix}|g" MOUL.sh chown-data.c
    reinplace -W ${worksrcpath} "s|@ASSETS@|${assets_dir}|g" MOUL.sh chown-data.c
    reinplace "s|@APP_PACKAGE@|${app_package}|g" ${worksrcpath}/chown-data.c
}

use_configure               no

build {
    # Delete the main app executable (which does nothing but launch the child app)
    # and replace it with our script (which does that and more)
    delete "${worksrcpath}/app/Contents/MacOS/Uru Live"
    xinstall -W ${worksrcpath} MOUL.sh "${worksrcpath}/app/Contents/MacOS/Uru Live"
    
    # Move the wine prefix and symlink it back to where the app expects it
    set transgaming_dir "${worksrcpath}/app/Contents/Resources/Myst Online.app/Contents/Resources/transgaming"
    move ${transgaming_dir} ${worksrcpath}/wineprefix
    ln -s ${wineprefix} ${transgaming_dir}
    
    # Link "Uru Live" to our assets dir
    set program_files "${worksrcpath}/wineprefix/c_drive/Program Files"
    delete "${program_files}/Uru Live"
    ln -s ${assets_dir} "${program_files}/Uru Live"
    
    file mkdir ${worksrcpath}/assets
    ln -s "${app_package}/Contents/Resources/Myst Online.app/Contents/Resources" ${worksrcpath}/assets/Cider
    
    # Update the URL from which server status messages are obtained
    foreach a {Login Updater} {
        reinplace "s|support.mystonline.com/serverstatus/urulivelive.php|support.cyanworlds.com/serverstatus/moullive.php|g" \
            "${worksrcpath}/app/Contents/Resources/Myst Online.app/Contents/Resources/URU Live ${a}.app/Contents/Info.plist"
    }
    
    # Make sure we show the updated terms and conditions
    set tos "${worksrcpath}/app/Contents/Resources/Myst Online.app/Contents/Resources/URU Live EULA.app/Contents/Resources/TOS.txt"
    delete ${tos}
    ln -s ${assets_dir}/TOS.txt ${tos}
    
    # Build the chown wrapper program
    system -W ${worksrcpath} "${configure.cc} ${configure.cc_archflags} chown-data.c -o chown-data"
}

destroot {
    xinstall -d ${destroot}${libexec_dir} \
                ${destroot}${share_dir} \
                ${destroot}${applications_dir}
    
    xinstall -m 4755 -W ${worksrcpath} chown-data ${destroot}${libexec_dir}
    
    copy ${worksrcpath}/app ${destroot}${app_package}
    
    copy ${worksrcpath}/assets ${destroot}${assets_dir}
    
    copy ${worksrcpath}/wineprefix ${destroot}${wineprefix}
}

notes "
Welcome to Myst Online: Uru Live Again (MO:ULagain)

* To play Myst Online, you need an account, which you can create for free by visiting\
${homepage} and clicking Play.
* Myst Online automatically updates itself.\
The first time you open Myst Online, it will download several gigabytes of game data.\
Be patient\; this will take some time.\
If you already have the game data from a non-MacPorts installation of Myst Online,\
you can save time by copying or moving it into ${assets_dir}

This version of Myst Online runs via the Cider wrapper\
developed for the GameTap version of Myst Online which ran from 2007 to 2008.\
Peculiarities of this method of running Myst Online include:

* Several icons appear and disappear in the dock while the game is starting.
* The game may crash (unexpectedly quit) when you quit it.
* If you need to force-quit the game while in fullscreen mode, press Command-Option-Escape.
* The game starts in fullscreen mode at 800x600 resolution.\
To switch to a higher resolution, use the in-game graphics settings.
* To switch between fullscreen and windowed mode, press Command-Return.
* Intel GMA 950 integrated graphics cards are not supported.\
Some graphics including your avatar will not display correctly.
* If sounds are distorted or nonexistent, install the sounddecompress port and follow the instructions.
* The microphone cannot be used to talk to other players, though you can hear other players talking to you.
* On Macs with NVIDIA graphics cards, water does not show up properly in Er'cana.
* On Macs with NVIDIA graphics cards that have not upgraded to Mac OS X 10.6.3,\
Eder Kemo has severe performance problems.

Other methods of running Myst Online on a Mac include the Wine version in the mystonline-wine port,\
and running the game in Windows via VMware, Parallels or Boot Camp.

To play Myst Online, open ${app_package}.
"

supported_archs             i386
universal_variant           no

# Mounts a disk image.
proc my_attach_disk_image {disk_image} {
    global workpath
    set tmp_disk_image_dir [mkdtemp "${workpath}/.tmp/disk_image.XXXXXXXX"]
    set tmp_disk_image ${tmp_disk_image_dir}/[file tail ${disk_image}].cdr
    system "hdiutil convert -quiet -ov -format UDTO -o '${tmp_disk_image}' '${disk_image}'"
    set mountpoint [mkdtemp "${workpath}/.tmp/mountpoint.XXXXXXXX"]
    system "hdiutil attach -quiet '${tmp_disk_image}' -mountpoint '${mountpoint}' -private -nobrowse -noautoopen -noautofsck -noverify -readonly"
    return ${mountpoint}
}

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

livecheck.type              none