# Description: The D programming language reference compiler
# URL: http://www.dlang.org
# Packager: alienus at nutyx dot org, tnut at nutyx dot org
# Depends on: unzip
name=dmd
version=2.068.1
release=1

source=(http://downloads.dlang.org/releases/2015/dmd.$version.zip
dmd.desktop dmd.logo_48.png)

build(){

    [ "`uname -m`" == "i686" ] && archstr=32
    [ "`uname -m`" == "x86_64" ] && archstr=64

    # dmd
    cd $SRC/dmd2/src/dmd
    make AUTO_BOOTSTRAP=1 -f posix.mak MODEL=$archstr
    PATH=.:$PATH
    make -f posix.mak MODEL=$archstr
    install -Dm755 dmd $PKG/usr/bin/dmd

    # druntime
    cd $SRC/dmd2/src/druntime
    make -f posix.mak MODEL=$archstr DMD=../dmd/dmd
    mkdir -p $PKG/usr/include/d
    cp -r import/* $PKG/usr/include/d

    # libphobos
    cd $SRC/dmd2/src/phobos
    make -f posix.mak MODEL=$archstr DMD=../dmd/dmd
    install -Dm644 generated/linux/release/$archstr/libphobos2.a \
        $PKG/usr/lib/libphobos2.a 

    # Elf file dumper
    install -Dm755 $SRC/dmd2/linux/bin$archstr/dumpobj \
        $PKG/usr/bin/dumpobj
    # Elf file disassembler
    install -Dm755 $SRC/dmd2/linux/bin$archstr/obj2asm \
        $PKG/usr/bin/obj2asm
    # Run D program as a script
    install -Dm755 $SRC/dmd2/linux/bin$archstr/rdmd \
        $PKG/usr/bin/rdmd

    install -Dm755 $SRC/dmd2/linux/bin$archstr/dman \
        $PKG/usr/bin/dman
    install -Dm755 $SRC/dmd2/linux/bin$archstr/ddemangle \
        $PKG/usr/bin/ddemangle

    # D compiler configuration file
    mkdir -p $PKG/etc
    echo -e "[Environment]\nDFLAGS=-I/usr/include/d -L-L/usr/lib -L--no-warn-search-mismatch -L--export-dynamic" \ > $PKG/etc/dmd.conf

    # D samples 
    mkdir -p $PKG/usr/share/d/samples/
    cp -r $SRC/dmd2/samples/d/* $PKG/usr/share/d/samples/

    # standard d modules
    cd $SRC/dmd2/src/phobos
    mkdir -p $PKG/usr/include/d
    cp -Rf std $PKG/usr/include/d
    cp -Rf etc $PKG/usr/include/d
    cp -f {index,unittest}.d $PKG/usr/include/d

    # man pages
    cd $SRC/dmd2
    mkdir -p $PKG/usr/share/man/{man1,man5}
    cp -Rf man/man1/*.1 $PKG/usr/share/man/man1/
    cp -Rf man/man5/*.5 $PKG/usr/share/man/man5/

    # doc
    mkdir -p $PKG/usr/share/doc/$pkgname-$pkgver/
    cp -Rf html/d/* $PKG/usr/share/doc/$pkgname-$pkgver/
                
    # desktop integration
    install -Dm644 $SRC/$name.desktop \
        $PKG/usr/share/applications/$name.desktop
    install -Dm 644 $SRC/$name.logo_48.png \
        $PKG/usr/share/icons/hicolor/48x48/apps/dlogo_48.png
}
