#!/bin/sh

tce-load -wil compiletc wget squashfs-tools zsync glib2-dev dbus-dev gobject-introspection-dev \
		libdaemon-dev intltool libevent-dev automake libtool gettext-dev \
		python3.14-setuptools
		

SRCPATH="../../../sources"

VERSION="0.9-rc5"
P=avahi
SRC=$P-${VERSION}
SRCDIR=`pwd`/$SRC
WRKDIR=avahi-${VERSION}
TMPDIR=/tmp/$P


# Export variables needed for compilation
case $(find /lib | grep ld-linux) in
    *armhf*)
       export CFLAGS="-Os -pipe -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"
       export CXXFLAGS="-Os -pipe -fno-exceptions -fno-rtti -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"
       BIT32="linux32"
    ;;
    *aarch64*)
       export CFLAGS="-Os -pipe -march=armv8-a+crc -mtune=cortex-a72"
       export CXXFLAGS="-Os -pipe -fno-exceptions -fno-rtti -march=armv8-a+crc -mtune=cortex-a72"
       BIT32=""
    ;;
esac

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

tar xf ${SRCPATH}/${SRC}.tar.gz

cd $WRKDIR

patch -Np1 -i ../$SRCPATH/avahi-0.9-rc5-ipv6_race_condition_fix-1.patch

./autogen.sh
#
#########################################################################
${BIT32} ./configure --prefix=/usr/local --disable-static --localstatedir=/var \
--runstatedir=/var --with-distro=none \
--disable-gdbm --disable-gtk --disable-gtk3 --disable-mono --disable-monodoc \
--disable-python --disable-qt3 --disable-qt4 --disable-qt5 --enable-compat-libdns_sd \
--with-avahi-user=tc --with-avahi-group=staff --with-autoipd-user=tc --with-autoipd-group=staff \
--with-avahi-priv-access-group=staff --enable-introspection=yes  --with-systemdsystemunitdir=no \
--disable-manpages --disable-libsystemd
###################################################################
# prefix:                        /usr/local
# sysconfdir:                    ${prefix}/etc
# localstatedir:                 /var
# fix below-------avahi socket:/run/avahi-daemon/socket ##################################
# dbus-1 system.d dir:           ${prefix}/etc/dbus-1/system.d
# dbus-1 version:                1.12.20
# dbus-1 system socket:          unix:path=/var/run/dbus/system_bus_socket
# C Compiler:                    gcc
# CFLAGS:-march=armv8-a+crc -mtune=cortex-a72 -Os -pipe -fstack-protector -std=c99 -Wall -W -Wextra -pedantic -pipe -Wformat -Wold-style-definition -Wdeclaration-after-statement -Wfloat-equal -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wredundant-decls -Wmissing-noreturn -Wshadow -Wendif-labels -Wpointer-arith -Wbad-function-cast -Wcast-qual -Wcast-align -Wwrite-strings -fdiagnostics-show-option -Wno-cast-qual -fno-strict-aliasing
# Enable GLIB:                  yes
# Enable GLIB GObject:          yes
# Enable GObject Introspection: yes
# Enable GTK 2.0:               no
# Enable GTK 3.0:               no
# Enable D-Bus:                 yes
# With XML:                     expat
# Enable GDBM:                  no
# Enable DBM:                   no
# Enable libdaemon:             yes
# Enable Python:                no
# Enable pygobject:             no
# Enable python-dbus:           no
# Enable QT3:                   no
# Enable QT4:                   no
# Enable QT5:                   no
# Enable Mono:                  no
# Enable Monodoc:               no
# Distribution/OS:              none
# User for avahi-daemon:        tc
# Group for avahi-daemon:       staff
# Priviliged access group for Avahi clients: staff
# User for avahi-autoipd:       tc
# Group for avahi-autoipd:      staff
# Enable chroot():              no
# Enable Linux inotify:         yes
# Enable stack-smashing protection:yes
# systemd unit directory:                    
# Building libavahi-core        yes
# Building avahi-daemon:        yes
# Building avahi-dnsconfd:      yes
# Building libavahi-client:     yes
# Building avahi-utils:         yes
# Building avahi-python:        no   (You need python, pygobject and python-dbus!)
# Building libavahi-glib:       yes
# Building libavahi-gobject:    yes
# Building avahi-discover-standalone: no
# Building libavahi-qt3:        no
# Building libavahi-qt4:        no
# Building libavahi-qt5:        no
# Building avahi-sharp:         no
# Building avahi-compat-libdns_sd:yes
# Building avahi-compat-howl:   no
# Building tests:               yes
# Building avahi-core documentation:no
# Building avahi-autoipd:       yes
# Building libavahi-ui:         no
# Building libavahi-libevent:	yes
#########################################
# fix fails part 2 "--runstatedir=/var" FAILURE in Makefile 
sed 's|avahi_runtime_dir = /run|avahi_runtime_dir = /var/run|' -i Makefile
sed 's|avahi_socket = /run/avahi-daemon/socket|avahi_socket = /var/run/avahi-daemon/socket|' -i Makefile
${BIT32} make -j6 # takes 1 minute 
make install-strip DESTDIR=$TMPDIR

cd ..
cp $P-init /tmp
cp $P-tce /tmp

cd /tmp

# /run made not /var/run


# dev
#####
mkdir -p $P-dev/usr/local/lib
mv $P/usr/local/include $P-dev/usr/local/
mv $P/usr/local/lib/pkgconfig $P-dev/usr/local/lib/
rm -f $P/usr/local/lib/*.la

# dev tce.intstall, borrowed from arch build
#############
mkdir -p $P-dev/usr/local/tce.installed
echo '#!/bin/sh
LOOP=/tmp/avahi-dev/usr/local/include/avahi-compat-libdns_sd
[ -f /usr/local/include/dns_sd.h ] || ln -s $LOOP/dns_sd.h  /usr/local/include/dns_sd.h
' > $P-dev/usr/local/tce.installed/$P-dev
sudo chown -R root:staff $P-dev/usr/local/tce.installed
sudo chmod -R 775 $P-dev/usr/local/tce.installed

# gir
#####
mkdir -p $P-gir/usr/local/lib/
mv $P/usr/local/lib/girepository-1.0 $P-gir/usr/local/lib/
mkdir -p $P-gir/usr/local/share
mv $P/usr/local/share/gir* $P-gir/usr/local/share/

# libavahi
###############
mkdir -p lib$P/usr/local/lib
mv $P/usr/local/lib/lib$P* lib$P/usr/local/lib/
mv $P/usr/local/lib/libdns* lib$P/usr/local/lib/
rm -rf $P/usr/local/lib

# locale
#########
mkdir -p $P-locale/usr/local/share
mv $P/usr/local/share/locale $P-locale/usr/local/share

# main
######
rm -rf $P/usr/local/share/man

# daemon
#########
mkdir -p $P/usr/local/etc/init.d
# various cat/echo methods do not display correctly so use the download instead
mv $P-init $P/usr/local/etc/init.d/$P
sudo chown root:root $P/usr/local/etc/init.d/$P
sudo chmod 755 $P/usr/local/etc/init.d/$P

# ref https://linux.die.net/man/5/avahi-daemon.conf
DC=$P/usr/local/etc/avahi/avahi-daemon.conf
sed 's|#rlimit-c|rlimit-c|' -i $DC
sed 's|#rlimit-data=8388608|rlimit-data=4194304|' -i $DC
sed 's|#rlimit-fsize=0|rlimit-fsize=0|' -i $DC
sed 's|#rlimit-nofile=768|rlimit-nofile=15|' -i $DC
sed 's|#rlimit-stack=8388608|rlimit-stack=4194304|' -i $DC
sed 's|#rlimit-nproc=3|rlimit-nproc=3| ' -i $DC

mkdir -p $P/usr/local/share/$P/files
mv $P/usr/local/etc/avahi/avahi-daemon.conf $P/usr/local/share/$P/files
cp $SRCDIR/service-type-database/service-types $P/usr/local/share/$P/files

# tce.install
#############
mkdir -p $P/usr/local/tce.installed
mv $P-tce $P/usr/local/tce.installed/$P
sudo chown -R root:staff $P/usr/local/tce.installed
sudo chmod -R 775 $P/usr/local/tce.installed


# TCZ them
#######
LIST="$P $P-dev $P-gir $P-locale lib$P"
for Z in $LIST
do
        mksquashfs $Z $Z.tcz
        md5sum $Z.tcz > $Z.tcz.md5.txt
        echo $VERSION >$Z.tcz.ver
        cd $Z
        find usr -not -type d > /tmp/$Z.tcz.list
        sed 's|usr|/usr|g' -i /tmp/$Z.tcz.list
        cd /tmp
done

echo 'avahi.tcz
glib2-dev.tcz
dbus-dev.tcz
gobject-introspection-dev.tcz
libdaemon-dev.tcz
intltool.tcz
libevent-dev.tcz ' > $P-dev.tcz.dep
echo 'libavahi.tcz
nss-mdns.tcz
libdaemon.tcz ' > $P.tcz.dep
echo 'dbus.tcz
gcc_libs.tcz
glib2.tcz' > libavahi.tcz.dep
