# $Id: Portfile 67988 2010-05-23 20:17:47Z ryandesign@macports.org $

PortSystem                  1.0

name                        mystonline-cider
set my_name                 mystonline
version                     2010
revision                    2
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_extract             bin:expect:expect

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 {
    copy ${distpath}/${installer_dmg} ${worksrcpath}
    set my_dmg_mount [my_attach_disk_image ${worksrcpath}/${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 "s|@WINEPREFIX@|${wineprefix}|g" ${worksrcpath}/MOUL.sh ${worksrcpath}/chown-data.c
    reinplace "s|@ASSETS@|${assets_dir}|g" ${worksrcpath}/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 "cd ${worksrcpath} && ${configure.cc} 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) \
                            \n\n* To play Myst Online, you need an account, which you can create for free by visiting \
                            ${homepage} and clicking Play. \
                            \n* 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} \
                            \n\nThis 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: \
                            \n\n* Several icons appear and disappear in the dock while the game is starting. \
                            \n* The game may crash (unexpectedly quit) when you quit it. \
                            \n* If you need to force-quit the game while in fullscreen mode, press Command-Option-Escape. \
                            \n* The game starts in fullscreen mode at 800x600 resolution. To switch to a \
                            higher resolution, use the in-game graphics settings. \
                            \n* To switch between fullscreen and windowed mode, press Command-Return. \
                            \n* Intel GMA 950 integrated graphics cards are not supported. Some graphics including your avatar \
                            will not display correctly. \
                            \n* If sounds are distorted or nonexistent, install the sounddecompress port and follow the instructions. \
                            \n* The microphone cannot be used to talk to other players, though you can hear other players talking to you. \
                            \n* On Macs with NVIDIA graphics cards, water does not show up properly in Er'cana. \
                            \n* On Macs with NVIDIA graphics cards that have not upgraded to Mac OS X 10.6.3, \
                            Eder Kemo has severe performance problems. \
                            \n\nOther 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. \
                            \n\nTo play Myst Online, open ${app_package}.

universal_variant           no

# 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,
    # so that we can agree to the license agreement.
    system "expect ${filespath}/attach.exp '${disk_image}' '${mountpoint}'"
    return ${mountpoint}
}

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

livecheck.type              none
