# Copyright 2018 Julian Ospald <hasufell@posteo.de>
# Distributed under the terms of the GNU General Public License v2

require gtk-icon-cache desktop-utils
require systemd-service openrc-service [ openrc_confd_files=[ "${FILES}"/openrc/confd ] ]

SUMMARY="Dropbox daemon (pretends to be GUI-less)"
HOMEPAGE="https://www.dropbox.com/"
DOWNLOADS="
    platform:x86? ( https://clientupdates.dropboxstatic.com/dbx-releng/client/dropbox-lnx.x86-${PV}.tar.gz )
    platform:amd64? ( https://clientupdates.dropboxstatic.com/dbx-releng/client/dropbox-lnx.x86_64-${PV}.tar.gz )
"

LICENCES="CC-Attribution-NoDerivs-3.0 FTL MIT LGPL-2 openssl dropbox"
SLOT="0"
PLATFORMS="~amd64 ~x86"
MYOPTIONS="
    platform:
        amd64
        x86
    ( providers: ijg-jpeg jpeg-turbo ) [[ number-selected = exactly-one ]]
"
RESTRICT="mirror strip"

DEPENDENCIES="
    build:
        dev-util/patchelf
    run:
        dev-libs/glib:2
        dev-libs/libffi
        dev-libs/libxml2:2.0
        dev-libs/libxslt
        dev-libs/popt
        media-libs/fontconfig
        media-libs/freetype:2
        sys-libs/zlib
        x11-dri/mesa
        x11-libs/libICE
        x11-libs/libSM
        x11-libs/libX11
        x11-libs/libXcomposite
        x11-libs/libXext
        x11-libs/libXrender
        x11-libs/libxcb
        x11-libs/qtbase:5
        x11-libs/qtdeclarative:5
        x11-libs/qtwebkit:5
"

BUGS_TO="hasufell@posteo"

pkg_setup() {
    exdirectory --allow /opt
}

src_unpack() {
    default
    edo mkdir "${WORK}"
    edo mv "${WORKBASE}"/.dropbox-dist/* "${WORK}"/
    edo mv "${WORK}"/dropbox-lnx.*-${PV}/* "${WORK}"
    edo rmdir "${WORK}"/dropbox-lnx.*-${PV}/
    edo rmdir .dropbox-dist
}

src_prepare() {
    # remove some bundled stuff
    edo rm -v libGL.so.1 libX11* libdrm.so.2 libffi.so.6 libpopt.so.0 wmctrl

    edo patchelf --set-rpath '$ORIGIN' librsyncffi.compiled._librsyncffi*.so
}

src_install() {
    local installdir="/opt/dropbox"

    insinto "${installdir}"
    doins -r *
    chmod a+x "${IMAGE}${installdir}"/{dropbox,dropbox_py3,dropboxd}
    dodir /usr/$(exhost --target)/bin
    dosym "../../../${installdir}/dropboxd" "/usr/$(exhost --target)/bin/dropbox"

    insinto /usr/share/icons/hicolor/16x16/status
    doins images/hicolor/16x16/status/*

    install_desktop_entry "Name=Dropbox"

    install_systemd_files
    install_openrc_files

    dodoc README ACKNOWLEDGEMENTS
}