SUMMARY = "A TCP/IP Daemon simplifying the communication with GPS devices"
SECTION = "console/network"
LICENSE = "BSD"
LIC_FILES_CHKSUM = "file://COPYING;md5=d217a23f408e91c94359447735bc1800"
DEPENDS = "dbus dbus-glib ncurses python libusb1 chrpath-replacement-native pps-tools"
PROVIDES = "virtual/gpsd"
PR="m7"

EXTRANATIVEPATH += "chrpath-native"

# sysclockcrash is fixed for gpsd
# suppress text in binary is fixed for gpsctl

SRC_URI = "${SAVANNAH_GNU_MIRROR}/${BPN}/${BP}.tar.gz \
    file://0001-SConstruct-prefix-includepy-with-sysroot-and-drop-sy.patch \
    file://0004-SConstruct-disable-html-and-man-docs-building-becaus.patch \
    file://0001-include-sys-ttydefaults.h.patch \
    file://0006-ubxtimelps.patch \
    file://0006-itu_r_tf_460_6.patch \
    file://gpsd-default \
    file://gpsd \
    file://gpsd_ubx_fixed.sh \
    file://gpsd_ubx_settime.sh \
    file://gpsd_fixed.sh \
    file://gpsd_settime.sh \
    file://gpsd.rules \
    file://gpsd.service \
"
SRC_URI[md5sum] = "3b11f26b295010666b1767b308f90bc5"
SRC_URI[sha256sum] = "5cb1e6d880ec9a52c62492dd0e3d77451b7c7ad625895bd652f6354215aec23e"

inherit scons update-rc.d python-dir pythonnative systemd

INITSCRIPT_NAME = "gpsd"
INITSCRIPT_PARAMS = "defaults 10"

SYSTEMD_OESCONS = "${@bb.utils.contains('DISTRO_FEATURES', 'systemd', 'true', 'false',d)}"

export STAGING_INCDIR
export STAGING_LIBDIR

# Necessary for python
export BUILD_SYS
export HOST_SYS

PACKAGECONFIG ??= "${@bb.utils.contains('DISTRO_FEATURES', 'bluetooth', 'bluez', '', d)}"
PACKAGECONFIG[bluez] = "bluez='false',${BLUEZ}"
PACKAGECONFIG[qt] = "qt='yes',qt='no',qt4-x11-free"

# Remove unneeded features.  netfeed is needed by gpsmon
EXTRA_OESCONS = " \
    sysroot=${STAGING_DIR_TARGET} \
    libQgpsmm='false' \
    debug='true' \
    strip='false' \
    chrpath='yes' \
    systemd='${SYSTEMD_OESCONS}' \
    libdir='${libdir}' \
    ublox='yes' \
    ubloxtimels='yes' \
    pps='yes' \
    gpsd_group='gps' \
    ntpshm='yes' \
    prefix='usr' \
    control_socket='false' \
    nofloats='yes' \
    ashtech='false' \
    earthmate='false' \
    evermore='false' \
    fury='false' \
    fv18='false' \
    garmin='false' \
    garmintxt='false' \
    geostar='false' \
    greis='false' \
    itrax='false' \
    mtk3301='false' \
    navcom='false' \
    nmea0183='true' \
    nmea2000='false' \
    oncore='false' \
    sirf='false' \
    skytraq='false' \
    superstar2='false' \
    tnt='false' \
    tripmate='false' \
    tsip='false' \
    ublox='true' \
    aivdm='false' \
    gpsclock='false' \
    isync='false' \
    ntrip='false' \
    oceanserver='false' \
    passthrough='false' \
    rtcm104v2='false' \
    rtcm104v3='false' \
    bluez='false' \
    ipv6='false' \
    netfeed='true' \
    usb='false' \
    ${PACKAGECONFIG_CONFARGS} \
"
# this cannot be used, because then chrpath is not found and only static lib is built
# target=${HOST_SYS}

do_compile_prepend() {
    export PKG_CONFIG_PATH="${PKG_CONFIG_PATH}"
    export PKG_CONFIG="PKG_CONFIG_SYSROOT_DIR=\"${PKG_CONFIG_SYSROOT_DIR}\" pkg-config"
    export STAGING_PREFIX="${STAGING_DIR_HOST}/${prefix}"
    export LINKFLAGS="${LDFLAGS}"
}

do_install() {
    export PKG_CONFIG_PATH="${PKG_CONFIG_PATH}"
    export PKG_CONFIG="PKG_CONFIG_SYSROOT_DIR=\"${PKG_CONFIG_SYSROOT_DIR}\" pkg-config"
    export STAGING_PREFIX="${STAGING_DIR_HOST}/${prefix}"
    export LINKFLAGS="${LDFLAGS}"

    export DESTDIR="${D}"
    # prefix is used for RPATH and DESTDIR/prefix for instalation
    ${STAGING_BINDIR_NATIVE}/scons prefix=${prefix} install ${EXTRA_OESCONS}|| \
      bbfatal "scons install execution failed."
}

do_install_append() {
    install -d ${D}/${sysconfdir}/init.d
    install -m 0755 ${WORKDIR}/gpsd ${D}/${sysconfdir}/init.d/
    install -d ${D}/${sysconfdir}/default
    install -m 0644 ${WORKDIR}/gpsd-default ${D}/${sysconfdir}/default/gpsd.default
    install -d ${D}${sbindir}
    install -m 0755 ${WORKDIR}/gpsd_ubx_fixed.sh ${D}${sbindir}/gpsd_ubx_fixed
    install -m 0755 ${WORKDIR}/gpsd_ubx_settime.sh ${D}${sbindir}/gpsd_ubx_settime
    install -m 0755 ${WORKDIR}/gpsd_fixed.sh ${D}${sbindir}/gpsd_fixed
    install -m 0755 ${WORKDIR}/gpsd_settime.sh ${D}${sbindir}/gpsd_settime


    #support for udev
    install -d ${D}/${sysconfdir}/udev/rules.d
    install -m 0644 ${S}/gpsd.rules ${D}/${sysconfdir}/udev/rules.d
    install -d ${D}${base_libdir}/udev/
    install -m 0755 ${S}/gpsd.hotplug ${D}${base_libdir}/udev/

    #support for python
    install -d ${D}/${PYTHON_SITEPACKAGES_DIR}/gps
    install -m 755 ${S}/gps/*.py ${D}/${PYTHON_SITEPACKAGES_DIR}/gps

    #support for systemd
    install -d ${D}${systemd_unitdir}/system/
    install -m 0644 ${WORKDIR}/${BPN}.service ${D}${systemd_unitdir}/system/${BPN}.service
    install -m 0644 ${S}/systemd/${BPN}.socket ${D}${systemd_unitdir}/system/${BPN}.socket
    
    # support for dev (development)
    mkdir -p -m 0644 ${D}/${includedir}/gps
    install -m 0644 ${S}/gpsd.h ${D}/${includedir}/gps
    install -m 0644 ${S}/os_compat.h ${D}/${includedir}/gps
    install -m 0644 ${S}/revision.h ${D}/${includedir}/gps
    install -m 0644 ${S}/gpsdclient.h ${D}/${includedir}/gps
    install -m 0644 ${S}/gpsd_config.h ${D}/${includedir}/gps
    install -m 0644 ${S}/compiler.h ${D}/${includedir}/gps
    install -m 0644 ${S}/ppsthread.h ${D}/${includedir}/gps

    # New ubxtool
    install -m 0755 ${S}/ubxtool ${D}/${bindir}
    install -m 0644 ${S}/ubxtool.1 ${D}/${mandir}/man1
}

RDEPENDS_${PN}-conf += "bash"

pkg_postinst_${PN}-conf() {
    update-alternatives --install ${sysconfdir}/default/gpsd gpsd-defaults ${sysconfdir}/default/gpsd.default 10
}

pkg_postrm_${PN}-conf() {
    update-alternatives --remove gpsd-defaults ${sysconfdir}/default/gpsd.default
}

PACKAGES =+ "libgps libgpsd python-pygps-dbg python-pygps gpsd-udev gpsd-conf gpsd-gpsctl gpspipe gps-utils"

# File does not exist: ${libdir}/libQgpsmm.prl 
FILES_${PN}-dev += "${libdir}/pkgconfdir/libgpsd.pc ${libdir}/pkgconfdir/libgps.pc \
		    ${libdir}/libQgpsmm.prl \
                    ${includedir}/gps.h ${includedir}/libgpsmm.h ${includedir}/gps"

FILES_${PN} += "${sbindir}/gpsd_*"

FILES_python-pygps-dbg += " ${libdir}/python*/site-packages/gps/.debug"

RDEPENDS_${PN} = "gpsd-gpsctl bash"

# gpsd-udev is for USB, which we are not using
RRECOMMENDS_${PN} = "gpsd-conf gpsd-machine-conf"

SUMMARY_${PN}-udev = "udev relevant files to use gpsd hotplugging"
FILES_${PN}-udev = "${base_libdir}/udev ${sysconfdir}/udev/*"
RDEPENDS_${PN}-udev += "eudev gpsd-conf"

SUMMARY_libgpsd = "C service library used for communicating with gpsd"
FILES_libgpsd = "${libdir}/libgpsd.so.*"

SUMMARY_libgps = "C service library used for communicating with gpsd"
FILES_libgps = "${libdir}/libgps.so.*"

SUMMARY_${PN}-conf = "gpsd configuration files and init scripts"
FILES_${PN}-conf = "${sysconfdir}" 
CONFFILES_${PN}-conf = "${sysconfdir}/default/gpsd.default"

SUMMARY_${PN}-gpsctl = "Tool for tweaking GPS modes"
FILES_${PN}-gpsctl = "${bindir}/gpsctl"

SUMMARY_gpspipe = "Tool to connectg to gpsd and retrieve sentences"
FILES_gpspipe = "${bindir}/gpspipe"

SUMMARY_gps-utils = "Utils used for simulating, monitoring,... a GPS"
FILES_gps-utils = "${bindir}/*"
RDEPENDS_gps-utils = "python-pygps"

SUMMARY_python-pygps = "Python bindings to gpsd"
FILES_python-pygps = "${PYTHON_SITEPACKAGES_DIR}/*"
RDEPENDS_python-pygps = " \
    python-core \
    python-io \
    python-threading \
    python-terminal \
    python-curses \
    gpsd \
    python-json"

RPROVIDES_${PN} += "${PN}-systemd"
RREPLACES_${PN} += "${PN}-systemd"
RCONFLICTS_${PN} += "${PN}-systemd"
SYSTEMD_SERVICE_${PN} = "${BPN}.socket ${BPN}ctl@.service"


ALTERNATIVE_${PN} = "gpsd-defaults"
ALTERNATIVE_LINK_NAME[gpsd-defaults] = "${sysconfdir}/default/gpsd"
ALTERNATIVE_TARGET[gpsd-defaults] = "${sysconfdir}/default/gpsd.default"