#!/bin/sh
# test we are root
if [ "$(id -u)" != "0" ]; then
   echo "run as root now exitting"
   exit 1
fi

export CFLAGS="-march=armv8-a+crc -mtune=cortex-a72 -Os -pipe" 
export CXXFLAGS="-march=armv8-a+crc -mtune=cortex-a72 -Os -pipe \
-fno-exceptions -fno-rtti"

P=avahi
V=0.8
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc wget squashfs-tools zsync glib2-dev dbus-dev gobject-introspection-dev libdaemon-dev intltool libevent-dev"
for Z in $LIST    
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://github.com/lathiat/$P/releases/download/v$V/$P-$V.tar.gz \
https://www.dropbox.com/s/ti9jkxxr6ebf7vs/avahi-init " $USER
mkdir $P
tar xvf $P*gz
cd $SRC
#  
#########################################################################
./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 
###################################################################
# 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
make -j5 # takes 1 minute 
make install-strip DESTDIR=/tmp/$P  
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/
mv $P/usr/local/lib/*a $P-dev/usr/local/lib/
chmod 644 $P-dev/usr/local/lib/*a 

# dev tce.install, 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
chown -R root:staff $P-dev/usr/local/tce.installed
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
chown root:root $P/usr/local/etc/init.d/$P
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 $SRC/service-type-database/service-types $P/usr/local/share/$P/files

# tce.install
#############
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
LOOP=/tmp/tcloop/avahi/usr/local/share/avahi/files
[ -f /usr/local/etc/avahi/avahi-daemon.conf ] || cp -p $LOOP/avahi-daemon.conf /usr/local/etc/avahi
[ -f /usr/local/etc/avahi/hosts ] || cp -p $LOOP/hosts /usr/local/etc/avahi
EOF
chown -R root:staff $P/usr/local/tce.installed
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
	cd $Z
	find usr -not -type d > /tmp/$Z.tcz.list
        sed 's|usr|/usr|g' -i /tmp/$Z.tcz.list
        cd /tmp
        zsyncmake $Z.tcz
done

ls -hal

echo 'Title:          avahi-dev.tcz
Description:    avahi devs
Version:        0.8
Author:         avahi team
Original-site:  https://github.com/lathiat/avahi/
Copying-policy: LGPL v 2.1
Size:           52K
Extension_by:   aus9
Tags:           dns 
Comments:       dev files

Change-log:     2020/09/19 Original v 0.8 on 12x 
Current:        2020/09/19  ' > $P-dev.tcz.info

echo 'Title:          avahi-gir.tcz
Description:    avahi introspection files
Version:        0.8
Author:         avahi team
Original-site:  https://github.com/lathiat/avahi/
Copying-policy: LGPL v 2.1
Size:           8K
Extension_by:   aus9
Tags:           dns gir
Comments:       gir files

Change-log:     2020/09/19 Original v 0.8 on 12x 
Current:        2020/09/19  ' > $P-gir.tcz.info

echo 'Title:          avahi-locale.tcz
Description:    avahi locales
Version:        0.8
Author:         avahi team
Original-site:  https://github.com/lathiat/avahi/
Copying-policy: LGPL v 2.1
Size:           132K
Extension_by:   aus9
Tags:           dns locale
Comments:       untested by me

Change-log:     2020/09/19 Original v 0.8 on 12x 
Current:        2020/09/19  ' > $P-gir.tcz.info


echo 'Title:          avahi.tcz
Description:    see below
Version:        0.8
Author:         avahi team
Original-site:  https://github.com/lathiat/avahi/
Copying-policy: LGPL v 2.1
Size:           92K
Extension_by:   aus9
Tags:           dns discovery bonjour
Comments:       Avahi is a free implementation of DNS Service 
                Discovery over Multicast DNS, commonly known as 
                and compatible with Apple Bonjour

                $ sudo /usr/local/etc/init.d/avahi [start|stop|restart|status]
                
Change-log:     2020/09/19 Original v 0.8 on 12x 
Current:        2020/09/19    ' > $P.tcz.info

echo 'Title:          libavahi.tcz
Description:    avahi lib files
Version:        0.8
Author:         avahi team
Original-site:  https://github.com/lathiat/avahi/
Copying-policy: LGPL v 2.1
Size:           160K
Extension_by:   aus9
Tags:           dns
Comments:       facilitates service discovery on a local network
                
Change-log:     2020/09/19 Original v 0.8 on 12x 
Current:        2020/09/19    ' > lib$P.tcz.info

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

