# -*- 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                        atari800
version                     5.0.0
revision                    0
platforms                   darwin
categories                  emulators
license                     GPL-2+

maintainers                 {krischik @krischik} openmaintainer
description                 Atari 800 emulator
long_description            Atari800 is an emulator of the classic Atari 800 8bit computer.

set tagversion              [string toupper ${name}]_[string map {. _} ${version}]
set patchversion            ${version}

homepage                    https://atari800.github.io
master_sites                git:https://github.com/atari800/atari800/releases/download/${tagversion}:prog                                   \
                            git:https://github.com/krischik/macports-user-krischik/releases/download/atari800-${patchversion}:appbundles    \
                            sourceforge:https://prdownloads.sourceforge.net/atari800:rom1                                                   \
                            http://www.emulators.com/freefile:rom2

set prog                    atari800-${version}-src.tgz
set rom1                    xf25.zip
set rom2                    pcxf380.zip
set appbundles              atari800-appbundles-r${patchversion}.tar.gz
set share                   atari800-share-r${patchversion}.tar.gz

distfiles                   ${prog}:prog                                        \
                            ${rom1}:rom1                                        \
                            ${rom2}:rom2                                        \
                            ${share}:appbundles                                 \
                            ${appbundles}:appbundles

extract.only                ${prog}                                             \
                            ${share}                                            \
                            ${appbundles}

checksums                   ${prog} \
                            rmd160  abf550021a903fadeff9061cafe74d1793cd45b6 \
                            sha256  eaa2df7b76646f1e49d5e564391707e5a4b56d961810cff6bc7c809bfa774605 \
                            size    1847596 \
                            ${rom1} \
                            rmd160  e3960898223dd4d1b9a9cc5cfb1251bd726ea5a7 \
                            sha256  98ae0ad10413dd6f35ed80f5662dba6d790def70c7829046e52012a03b574b8a \
                            size    188942 \
                            ${rom2} \
                            rmd160  e3061a54b6e9c88379f4366b2ccad3cf3b8e5270 \
                            sha256  2c0cf7e30ae8a486fc03903de4ebb1d7a40f0d9db3bfcb5dd4861e0cf5da67a5 \
                            size    590041 \
                            ${share} \
                            rmd160  bede7f4df7fb64549763ef6b1cb40555f2ea6e4e \
                            sha256  4f2b20c42f1d199adbbb86eb102dc0959ce800a440351319ae9a3b1d526742ae \
                            size    75646 \
                            ${appbundles} \
                            rmd160  2a212c652624b13c14f780cb176cac5c0ce9a1dd \
                            sha256  c341ff8d531138c29556ec7d531651011f291b7af34c027282af9d003d8ba07c \
                            size    5041480

depends_lib                 port:libsdl                                         \
                            port:libpng                                         \
                            port:libsdl_image                                   \
                            port:libsdl_sound                                   \
                            port:readline                                       \
                            port:ncurses                                        \
                            port:zlib

installs_libs               no

configure.args              --mandir='\${prefix}/share/man'                     \
                            --datadir='\${prefix}/share/atari800'               \
                            --sysconfdir='\${prefix}/etc/${name}'               \
                            --disable-rserial                                   \
                            --disable-sdltest
configure.cxxflags-append   "-I${prefix}/include"
configure.env-append        "CFLAGS=-O2 -I${prefix}/include -I${prefix}/include/SDL -D_GNU_SOURCE=1 -D_THREAD_SAFE"

post-extract {
    system "unzip -q ${distpath}/${rom1} -d ${workpath}/xf25"
    system "unzip -q ${distpath}/${rom2} -d ${workpath}/pcxf380"
}

post-destroot {
    xinstall -m 775 -d ${destroot}${prefix}/share/atari800

    xinstall -m 444                             \
        -W ${workpath}/xf25                     \
        ATARIBAS.ROM                            \
        ATARIOSB.ROM                            \
        ATARIXL.ROM                             \
        DEMOS1.XFD                              \
        DEMOS2.XFD                              \
        MYDOS45D.ATR                            \
        ${destroot}${prefix}/share/atari800

    xinstall -m 444                             \
        -W ${workpath}/xf25                     \
        DOS25.XFD                               \
        ${destroot}${prefix}/share/atari800/DOS25_1.XFD

    xinstall -m 444                             \
        -W ${workpath}/pcxf380                  \
        ADVENT1.ATR                             \
        ANALOG51.XFD                            \
        ANALOG52.XFD                            \
        COMDEM51.XFD                            \
        DEMOS.XFD                               \
        GAMES5.ATR                              \
        MYDOS45D.XFD                            \
        STARTREK.XFD                            \
        ${destroot}${prefix}/share/atari800

    xinstall -m 444                             \
        -W ${workpath}/pcxf380                  \
        DOS25.XFD                               \
        ${destroot}${prefix}/share/atari800/DOS25_2.XFD

    xinstall -m 444                             \
        -W ${workpath}/share-r${patchversion}   \
        Sparta32g.XFD                           \
        ${destroot}${prefix}/share/atari800
    xinstall -m 776                             \
        -W ${workpath}/share-r${patchversion}   \
        Setup.command                           \
        ${destroot}${prefix}/share/atari800
    reinplace s|@PREFIX@|${prefix}|g            \
        ${destroot}${prefix}/share/atari800/Setup.command
}

platform macosx {
    post-destroot {
        xinstall -d ${destroot}${applications_dir}/Atari-800
        foreach comp { Atari-130XE-Basic Atari-130XE-MyDos Atari-1400XL-Basic Atari-1400XL-MyDos Atari-320XE-Basic Atari-320XE-Sparta Atari-800-Basic Atari-800-Dos Atari-800XL-Basic Atari-800XL-Dos} {
            xinstall -d -m 775 ${destroot}${applications_dir}/Atari-800/${comp}.app
            xinstall -d -m 775 ${destroot}${applications_dir}/Atari-800/${comp}.app/Contents
            xinstall -d -m 775 ${destroot}${applications_dir}/Atari-800/${comp}.app/Contents/MacOS
            xinstall -d -m 775 ${destroot}${applications_dir}/Atari-800/${comp}.app/Contents/Resources

            xinstall -m 775                                                                         \
                ${workpath}/appbundles-r${patchversion}/${comp}.app/Contents/MacOS/Start.command    \
                ${destroot}${applications_dir}/Atari-800/${comp}.app/Contents/MacOS/Start.command

            xinstall -m 444                                                                         \
                ${workpath}/appbundles-r${patchversion}/${comp}.app/Contents/Resources/Icon.icns    \
                ${destroot}${applications_dir}/Atari-800/${comp}.app/Contents/Resources/Icon.icns

            xinstall -m 444                                                                         \
                ${workpath}/appbundles-r${patchversion}/${comp}.app/Contents/Info.plist             \
                ${destroot}${applications_dir}/Atari-800/${comp}.app/Contents/Info.plist

            reinplace s|@PREFIX@|${prefix}|g                                                        \
                ${destroot}${applications_dir}/Atari-800/${comp}.app/Contents/MacOS/Start.command
        }
    }
}
