#!/bin/sh
#
######################################################
# Build script for Core 15.x                         #
#                                                    #
# See .info for details                              #
######################################################

######################################################
# Configure extension creation parameters            #
######################################################
PYVER=3.9
# Variables
PACKAGE=frr
VERSION="8.2.2" 
TODAY=`date +%Y/%m/%d`
DESTDIR=/tmp/dest/${PACKAGE}
TMPDIR=/tmp/submit/${PACKAGE}
DESCRIPTION="FRRouting is a routing software suite"
DOCDESCRIPTION="Documentation part of the FRRouting routing software suite"
DEVDESCRIPTION="Development files part of the FRRouting routing software suite"
AUTHORS="See Original-site"
HOMEPAGE="https://frrouting.org/"
LICENSE="LGPL"
ME="rhermsen"
TAGS="frr frrouting zebra routing quagga"
DOCTAGS="man pages frr frrouting zebra routing quagga"
DEVTAGS="development frr frrouting zebra routing quagga"
ARCH=`uname -m`

######################################################
# Prepare extension creation                         #
######################################################

# Load extensions needed for compiling Frr

# Compile tools:
tce-load -wi compiletc.tcz
tce-load -wi squashfs-tools.tcz
# autoconf has perl5.tcz as a dependency
tce-load -wi autoconf.tcz
tce-load -wi automake.tcz
# tce-load -wi libtool.tcz
tce-load -wi libltdl.tcz
tce-load -wi pkg-config.tcz
tce-load -wi intltool.tcz
tce-load -wi libtool-dev.tcz

# Configure dependancy
tce-load -wi json-c-dev.tcz 
tce-load -wi readline-dev.tcz
# https://github.com/FRRouting/frr/pull/3155
# tce-load -wi libxml2.tcz
# tce-load -wi libxml2-dev.tcz
# The following 3.0 Configure dependancies are not applicable anymore for 4.0
# see: https://github.com/FRRouting/frr/issues/1706
#tce-load -wi XML-NamespaceSupport.tcz
#tce-load -wi perl_XML-SAX-Base.tcz
#tce-load -wi perl_XML-SAX.tcz
#tce-load -wi perl_XML-LibXML.tcz

if [ $ARCH == "i686" ]
then
tce-load -wi libcares-dev.tcz
elif [ $ARCH == "x86_64" ]
then
tce-load -wi c-ares-dev.tcz
fi

tce-load -wi texinfo.tcz

# new 4.0 Configure dependancy
#tce-load -wi python3.6.tcz
tce-load -wi python${PYVER}-dev.tcz

# new 5.0 Configure dependancies
tce-load -wi python${PYVER}-sphinx.tcz

if [ $ARCH == "i686" ]
then
tce-load -wi python${PYVER}-setuptools.tcz
fi

# new 6.0 Configure dependancies
tce-load -wi python${PYVER}-docutils.tcz

# new 7.0 Configure dependancies
# https://frrouting.org/release/7.0/
# libyang (>= 1.0.184) was not found on your system

# new 7.2 Configure dependancies
tce-load -wi libcap-dev.tcz

# new 8.0 Configure dependancies
# https://frrouting.org/release/8.0/
tce-load -wi elfutils-dev.tcz
# libyang (>= 2.0.0) was not found on your system
tce-load -wi libyang-dev.tcz

# Other
tce-load -wi ipv6-netfilter-KERNEL.tcz
tce-load -wi logrotate.tcz
tce-load -wi bash.tcz

# Workdir
sudo rm -r /tmp/${PACKAGE} 2>/dev/null
mkdir /tmp/${PACKAGE}
cd /tmp/${PACKAGE}

# Source
# wget https://github.com/FRRouting/frr/archive/frr-7.5.1.tar.gz
# wget https://github.com/FRRouting/frr/archive/frr-8.2.2.tar.gz
cp /mnt/vda1/tce/${PACKAGE}-${VERSION}.tar.gz .
tar xzf /tmp/${PACKAGE}/${PACKAGE}-${VERSION}.tar.gz
cd /tmp/${PACKAGE}/${PACKAGE}-${PACKAGE}-${VERSION}

sudo rm -r -f $TMPDIR 2>/dev/null
sudo rm -r -f $TMPDIR-doc 2>/dev/null
sudo rm -r -f $TMPDIR-dev 2>/dev/null

######################################################
# Compile extension                                  #
######################################################
sudo rm -r ${DESTDIR}* 2>/dev/null

# Export variables needed for compilation
ARCH=`uname -m`
if [ $ARCH == "i686" ]
then
export CFLAGS="-Os -pipe -march=i486 -mtune=i686"
export CXXFLAGS="-Os -pipe -fno-exceptions -fno-rtti -march=i486 -mtune=i686"
elif [ $ARCH == "x86_64" ]
then
echo
export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe"
else
exit 1
fi
export LDFLAGS="-Wl,-O1"
#export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

./bootstrap.sh

# Configure it
# https://www.gnu.org/prep/standards/html_node/Directory-Variables.html
./configure  --prefix=/usr/local \
--sysconfdir=/usr/local/etc/${PACKAGE} \
--localstatedir=/var/run/${PACKAGE} \
--runstatedir=/var/run/${PACKAGE} \
--bindir=/usr/local/bin \
--sbindir=/usr/local/sbin \
--enable-user=tc \
--enable-group=staff \
--enable-vty-group=staff \
--enable-ldpd \
--enable-pimd \
--enable-multipath=2 \
--enable-rtadv

# Compile
make
mkdir -p $DESTDIR
make DESTDIR=$DESTDIR install

# Install in base temp dir
mkdir -p $TMPDIR/usr/local/bin
mkdir -p $TMPDIR/usr/local/sbin
mkdir -p $TMPDIR/usr/local/lib/frr/modules
#mkdir -p $TMPDIR/usr/local/etc/init.d
mkdir -p $TMPDIR/usr/local/etc/${PACKAGE}
#mkdir -p $TMPDIR/usr/local/var/${PACKAGE}
mkdir -p $TMPDIR/usr/local/tce.installed
mkdir -p ${TMPDIR}/usr/local/share/doc/${PACKAGE}/
mkdir -p ${TMPDIR}/usr/local/share/yang/

mv ${DESTDIR}/usr/local/bin/* $TMPDIR/usr/local/bin
# examples look to be moved to online documentation (only)
#mv ${DESTDIR}/usr/local/etc/${PACKAGE}/* $TMPDIR/usr/local/etc/${PACKAGE}
mv ${DESTDIR}/usr/local/lib/lib*so* $TMPDIR/usr/local/lib
mv ${DESTDIR}/usr/local/lib/frr/modules/*.so $TMPDIR/usr/local/lib/frr/modules
mv ${DESTDIR}/usr/local/sbin/* $TMPDIR/usr/local/sbin
cp  /tmp/${PACKAGE}/${PACKAGE}-${PACKAGE}-${VERSION}/COPYING* ${TMPDIR}/usr/local/share/doc/${PACKAGE}/
mv ${DESTDIR}/usr/local/share/yang/* $TMPDIR/usr/local/share/yang/

##################################################
# daemons file                                   #
##################################################
cp /tmp/${PACKAGE}/${PACKAGE}-${PACKAGE}-${VERSION}/tools/etc/frr/daemons $TMPDIR/usr/local/etc/frr/daemons.sample
sed -i 's/can be found in/can be found online at/g' $TMPDIR/usr/local/etc/frr/daemons.sample
sed -i 's/\/usr\/share\/doc\/frr\/examples\/./https:\/\/docs.frrouting.org/g' $TMPDIR/usr/local/etc/frr/daemons.sample
sed -i 's/\/etc\/init.d\/frr/ sudo frrinit.sh <command> [namespace]/g' $TMPDIR/usr/local/etc/frr/daemons.sample
sed -i 's/user and group "frr"/user "tc" and group "staff"/g' $TMPDIR/usr/local/etc/frr/daemons.sample
sed -i 's/^# group "frrvty"/# group "staff"/g' $TMPDIR/usr/local/etc/frr/daemons.sample
sed -i '/^ospfd=no/a # ospfd_instances="1,2"' $TMPDIR/usr/local/etc/frr/daemons.sample
sed -i "s/\/usr\/lib\/frr\/frrinit.sh/\/usr\/local\/etc\/init.d\/frrinit.sh/g" $TMPDIR/usr/local/etc/frr/daemons.sample


###################################################                                      
# Create init-functions file                                #
###################################################
#/lib/lsb/init-functions
mkdir -p $TMPDIR/usr/local/lib/frr
cat <<EOF> $TMPDIR/usr/local/lib/frr/init-functions
#!/usr/local/bin/bash

log_success_msg () {
    echo "\$@" >&2
    [ -x /usr/bin/logger ] && echo "\$@" \\
        | /usr/bin/logger -t frr -p daemon.info "\$@"
}

log_failure_msg () {
    echo "\$@" >&2
    [ -x /usr/bin/logger ] && echo "\$@" \\
        | /usr/bin/logger -t frr -p daemon.err "\$@"
}

log_warning_msg () {
    echo "\$@" >&2
    [ -x /usr/bin/logger ] && echo "\$@" \\
        | /usr/bin/logger -t frr -p daemon.warn "\$@"
}
EOF


##################################################
# init file                                      #
################################################## 
sed -i 's/\/lib\/lsb\/init-functions/\/usr\/local\/lib\/frr\/init-functions/g' $TMPDIR/usr/local/sbin/frrinit.sh
sed -i 's/log_failure_msg "Unknown command: $1"/echo "Usage:  \[sudo\] frrinit.sh { start | stop | restart | force-reload | status | reload } \[namespace\]"/g' $TMPDIR/usr/local/sbin/frrinit.sh

sed -i '18 G' $TMPDIR/usr/local/sbin/frrinit.sh
sed -i '18 a fi' $TMPDIR/usr/local/sbin/frrinit.sh
sed -i '18 a \    exit 1' $TMPDIR/usr/local/sbin/frrinit.sh
sed -i '18 a \    echo "This script must be run by root \(sudo\)" >\&2' $TMPDIR/usr/local/sbin/frrinit.sh
sed -i '18 a if \[ "$\(id -u\)" -ne 0 \]; then' $TMPDIR/usr/local/sbin/frrinit.sh

##################################################                                                                                                                   
# *.py file                             #                                                                                                                   
##################################################                                                                                                                   
sudo sed -i 's/#!\/usr\/bin\/env python3/#!\/usr\/local\/bin\/python3/g' $TMPDIR/usr/local/sbin/*.py

##################################################                                                                                                                   
# frr-reload.py file                             #                                                                                                                   
##################################################                                                                                                                   
# https://stackoverflow.com/questions/2429511/why-do-people-write-usr-bin-env-python-on-the-first-line-of-a-python-script
sudo sed -i 's/\/usr\/bin/\/usr\/local\/bin/g' $TMPDIR/usr/local/sbin/frr-reload.py
sudo sed -i 's/\/etc\/frr/\/usr\/local\/etc\/frr/g' $TMPDIR/usr/local/sbin/frr-reload.py


###################################################                                      
# Create info file                                #
###################################################
cd /tmp/submit/
if [ $ARCH == "i686" ]
then
cat <<EOF> ${PACKAGE}.tcz.info
Title:          ${PACKAGE}.tcz
Description:    ${DESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   ${ME}
Tags:           ${TAGS}
"Comments:	Frr is a routing software package that provides TCP/IP based"
                routing services with routing protocols support such as RIPv1,
                RIPv2, RIPng, OSPFv2, OSPFv3, IS-IS, BGP-4, and BGP-4+.
                Frr also supports special BGP Route Reflector and Route Server behavior.
                In addition to traditional IPv4 routing protocols, Frr also supports
                IPv6 routing protocols. With SNMP daemon which supports SMUX and
                AgentX protocol, Frr provides routing protocol MIBs.

                Configuration examples can now be found online at:
                http://docs.frrouting.org

                The IPv6 kernel modules are not automatically installed.
                If IPv6 is desired, install ipv6-netfilter-x.x.x-tinycore.tcz via
                the Apps GUI, or cli (tce-load -wi ipv6-netfilter-KERNEL).
                If you want to make use of namespaces install iproute2.tcz via
                the Apps GUI, or cli (tce-load -wi iproute2).

                The startup script is located at /usr/local/sbin/frrinit.sh.
                You can configure the routing protocol daemons  to start in file:
                /usr/local/etc/frr/daemons.
                If not present cp /usr/local/etc/frr/daemons.sample to
                /usr/local/etc/frr/daemons and enable to protocols to use.
                Use the startup script with "sudo frrinit.sh" <options>.
                Usage:
                sudo frrinit.sh { start | stop | restart | force-reload | status | reload } [namespace]
                The reload option is to reload a configuration file without restarting the daemons.

                After the enabled daemons are running you can login with:
                vtysh

                The install script can help to do the basic setup of FRR.
                By default it will prepare FRR to use vtysh (one configuration for all daemons).
                If you don't want the install script to do the basic setup for you,
                you can disable it with creating a file NOSETUPHELP in /tmp 
                (touch /tmp/NOSETUPHELP) before installation.

                Watchfrr, a daemon monitoring connectivity with routing daemons,
                is enabled by default.


Change-log:     2018/01/23 First Version, 3.0.3
                2018/02/25 adapted frr script to work with TCL, 3.0.3
                2018/04/20 New major version, 4.0
                2019/02/10 New major version, 5.0.2
                2019/02/24 fixed script issues, watchfrr support, 5.0.2
                2020/04/02 New major version, 6.0.2
                ${TODAY} New major version, ${VERSION}
Current:        ${TODAY} New major version, ${VERSION}
EOF
elif [ $ARCH == "x86_64" ]
then
cat <<EOF> ${PACKAGE}.tcz.info
Title:          ${PACKAGE}.tcz
Description:    ${DESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   brezular ${ME}
Tags:           ${TAGS}
"Comments:	Frr is a routing software package that provides TCP/IP based"
                routing services with routing protocols support such as RIPv1,
                RIPv2, RIPng, OSPFv2, OSPFv3, IS-IS, BGP-4, and BGP-4+.
                Frr also supports special BGP Route Reflector and Route Server behavior.
                In addition to traditional IPv4 routing protocols, Frr also supports
                IPv6 routing protocols. With SNMP daemon which supports SMUX and
                AgentX protocol, Frr provides routing protocol MIBs.

                Configuration examples can now be found online at:
                http://docs.frrouting.org

                The IPv6 kernel modules are not automatically installed.
                If IPv6 is desired, install ipv6-netfilter-x.x.x-tinycore.tcz via
                the Apps GUI, or cli (tce-load -wi ipv6-netfilter-KERNEL).
                If you want to make use of namespaces install iproute2.tcz via
                the Apps GUI, or cli (tce-load -wi iproute2).

                The startup script is located at /usr/local/sbin/frrinit.sh.
                You can configure the routing protocol daemons  to start in file:
                /usr/local/etc/frr/daemons.
                If not present cp /usr/local/etc/frr/daemons.sample to
                /usr/local/etc/frr/daemons and enable to protocols to use.
                Use the startup script with "sudo frrinit.sh" <options>.
                Usage:
                sudo frrinit.sh { start | stop | restart | force-reload | status | reload } [namespace]
                The reload option is to reload a configuration file without restarting the daemons.

                After the enabled daemons are running you can login with:
                vtysh

                The install script can help to do the basic setup of FRR.
                By default it will prepare FRR to use vtysh (one configuration for all daemons).
                If you don't want the install script to do the basic setup for you,
                you can disable it with creating a file NOSETUPHELP in /tmp 
                (touch /tmp/NOSETUPHELP) before installation.

                Watchfrr, a daemon monitoring connectivity with routing daemons,
                is enabled by default.


Change-log:     2017/10/28 first version 3.1-dev (brezular)
                2018/07/14 version 5.0.1  (brezular)
                ${TODAY} New major version, ${VERSION}  ${ME}
Current:        ${TODAY} New major version, ${VERSION}  ${ME}
EOF
fi
 
###################################################                            
# Create .dep file                                #
###################################################
if [ $ARCH == "i686" ]
then
cat <<EOF> ${PACKAGE}.tcz.dep
json-c.tcz
libcares.tcz
bash.tcz
python${PYVER}.tcz
libyang.tcz
libcap.tcz
EOF
elif [ $ARCH == "x86_64" ]
then
cat <<EOF> ${PACKAGE}.tcz.dep
json-c.tcz
c-ares.tcz
bash.tcz
python${PYVER}.tcz
libyang.tcz
libcap.tcz
EOF
fi

###################################################                                 
# Create install script file                      #                                 
###################################################                                 
cat <<EOF> $TMPDIR/usr/local/tce.installed/${PACKAGE}
if [ -d /usr/local/etc/frr ]; then
        chown -R tc:staff /usr/local/etc/frr
fi
if [ -r /usr/local/etc/frr/daemons ]; then
        echo "After a version upgrade verify if /usr/local/etc/frr/daemons requires changes." | tee /dev/null
        echo "See /usr/local/etc/frr/daemons.sample for the latest example." | tee /dev/null
else
        sudo -u tc cp /usr/local/etc/frr/daemons.sample  /usr/local/etc/frr/daemons
        sudo -u tc mkdir /var/frr
        TCEDIR=\`readlink /etc/sysconfig/tcedir\`/optional
        if [ -r /tmp/NOSETUPHELP ]; then
                SETUPHELP="no"
        else
                SETUPHELP="yes"



                if [ ! -e "\$TCEDIR"/iana-etc.tcz ] && [ -n \$TCEDIR ]; then
                        sudo -u tc tce-load -wi iana-etc.tcz
                fi

                if [ ! -f /usr/local/tce.installed/iana-etc ] && [ -n \$TCEDIR ]; then
                        sudo -u tc tce-load -i iana-etc.tcz
                fi

                if [ ! -r /usr/local/etc/frr/frr.conf ]; then
                        touch /usr/local/etc/frr/frr.conf
                fi

                if  [ ! -f /usr/local/etc/frr/vtysh.conf ]; then
                        echo "username admin nopassword" > /usr/local/etc/frr/vtysh.conf
                        echo "service integrated-vtysh-config" >> /usr/local/etc/frr/vtysh.conf
                fi
                chown tc:staff /usr/local/etc/frr/*.conf

                echo "usr/local/etc/frr/daemons" >>  /opt/.filetool.lst                                                                                                      
                echo "etc/services" >>  /opt/.filetool.lst                                                                                                                   
                echo "usr/local/etc/frr/vtysh.conf" >>  /opt/.filetool.lst                                                                                                   
                echo "usr/local/etc/frr/frr.conf" >>  /opt/.filetool.lst

                sed -i 's/^hpstgmgr.*2600\/udp/#hpstgmgr       2600\/udp/g' /etc/services
                sed -i 's/^hpstgmgr.*2600\/tcp.*$/zebrasrv        2600\/tcp   # zebra service, replacing hpstgmgr/g' /etc/services
                sed -i 's/^discp-client.*2601\/udp/#discp-client   2601\/udp/g' /etc/services                                                          
                sed -i 's/^discp-client.*2601\/tcp.*$/zebra           2601\/tcp   # zebra vty, replacing discp-client/g' /etc/services      
                sed -i 's/^discp-server.*2602\/udp/#discp-server   2602\/udp/g' /etc/services                                                          
                sed -i 's/^discp-server.*2602\/tcp.*$/ripd            2602\/tcp   # RIPd vty, replacing discp-server/g' /etc/services       
                sed -i 's/^servicemeter.*2603\/udp/#servicemeter   2603\/udp/g' /etc/services                                                          
                sed -i 's/^servicemeter.*2603\/tcp.*$/ripngd          2603\/tcp   # RIPngd vty, replacing servicemeter/g' /etc/services     
                sed -i 's/^nsc-ccs.*2604\/udp/#nsc-ccs        2604\/udp/g' /etc/services                                                                    
                sed -i 's/^nsc-ccs.*2604\/tcp.*$/ospfd           2604\/tcp   # OSPFd vty, replacing nsc-ccs/g' /etc/services                
                sed -i 's/^nsc-posa.*2605\/udp/#nsc-posa       2605\/udp/g' /etc/services                                                                  
                sed -i 's/^nsc-posa.*2605\/tcp.*$/bgpd            2605\/tcp   # BGPd vty, replacing nsc-posa/g' /etc/services               
                sed -i 's/^netmon.*2606\/udp/#netmon         2606\/udp/g' /etc/services                                                                      
                sed -i 's/^netmon.*2606\/tcp.*$/ospf6d          2606\/tcp   # OSPF6d vty, replacing netmon/g' /etc/services                 
                sed -i 's/^connection.*2607\/udp/#connection     2607\/udp/g' /etc/services                                                              
                sed -i 's/^connection.*2607\/tcp.*$/ospfapi         2607\/tcp   # ospfapi, replacing connection/g' /etc/services            
                sed -i 's/^wag-service.*2608\/udp/#wag-service    2608\/udp/g' /etc/services                                                            
                sed -i 's/^wag-service.*2608\/tcp.*$/isisd           2608\/tcp   # ISISd vty, replacing wag-service/g' /etc/services        
                sed -i 's/^system-monitor.*2609\/udp/#system-monitor 2609\/udp/g' /etc/services                                                      
                sed -i 's/^system-monitor.*2609\/tcp.*$/babeld          2609\/tcp   # BABELd vty, replacing system-monitor/g' /etc/services 
                sed -i 's/^versa-tek.*2610\/udp/#versa-tek      2610\/udp/g' /etc/services                                                                
                sed -i 's/^versa-tek.*2610\/tcp.*$/nhrpd           2610\/tcp   # nhrpd vty, replacing versa-tek/g' /etc/services            
                sed -i 's/^lionhead.*2611\/udp/#lionhead       2611\/udp/g' /etc/services                                                                  
                sed -i 's/^lionhead.*2611\/tcp.*$/pimd            2611\/tcp   # PIMd vty, replacing lionhead/g' /etc/services               
                sed -i 's/^qpasa-agent.*2612\/udp/#qpasa-agent    2612\/udp/g' /etc/services                                                           
                sed -i 's/^qpasa-agent.*2612\/tcp.*$/ldpd            2612\/tcp   # LDPd vty, replacing qpasa-agent/g' /etc/services         
                sed -i 's/^smntubootstrap.*2613\/udp/#smntubootstrap 2613\/udp/g' /etc/services
                sed -i 's/^smntubootstrap.*2613\/tcp.*$/eigprd          2613\/tcp   # EIGRPd vty, replacing smntubootstrap/g' /etc/services
                sed -i 's/^cmadmin.*2617\/udp/#cmadmin        2617\/udp/g' /etc/services                                                                   
                sed -i 's/^cmadmin.*2617\/tcp.*$/bfdd            2617\/tcp   # bfdd vty, replacing cmadmin/g' /etc/services                 
                sed -i 's/^priority-e-com.*2618\/udp/#priority-e-com 2618\/udp/g' /etc/services                                                     
                sed -i 's/^priority-e-com.*2618\/tcp.*$/fabricd         2618\/tcp   # fabricd vty, replacing priority-e-com/g' /etc/services
                sed -i 's/^bruce.*2619\/udp.*$/#bruce          2619\/udp   # vrrpd vty, replacing bruce/g' /etc/services
                sed -i 's/^bruce.*2619\/tcp.*$/vrrpd           2619\/tcp   # vrrpd vty, replacing bruce/g' /etc/services
        fi
fi
EOF

# Delete compilation work directory

cd ..
#rm -r -f $WRKDIR

# Adjust directory access rigths

find $TMPDIR/ -type d | xargs chmod -v 755;

# Strip executables

find $TMPDIR | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded

# Move files to doc extension

mkdir -p $TMPDIR-doc/usr/local/share/man/man1
mkdir -p $TMPDIR-doc/usr/local/share/man/man8
mkdir -p $TMPDIR-doc/usr/local/share/info

mv ${DESTDIR}/usr/local/share/man/man1/* $TMPDIR-doc/usr/local/share/man/man1        
mv ${DESTDIR}/usr/local/share/man/man8/* $TMPDIR-doc/usr/local/share/man/man8 
mv ${DESTDIR}/usr/local/share/info/* $TMPDIR-doc/usr/local/share/info      

###################################################                                     
# Create info file                                #                                      
###################################################                                      
cd /tmp/submit/
if [ $ARCH == "i686" ]
then
cat <<EOF> ${PACKAGE}-doc.tcz.info
Title:          ${PACKAGE}-doc.tcz
Description:    ${DOCDESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   ${ME}
Tags:           ${DOCTAGS}
Comments:       Frr is a routing software package that provides TCP/IP based
                routing services with routing protocols support such as RIPv1,
                RIPv2, RIPng, OSPFv2, OSPFv3, IS-IS, BGP-4, and BGP-4+.
                Frr also supports special BGP Route Reflector and Route Server behavior.
                In addition to traditional IPv4 routing protocols, Frr also supports
                IPv6 routing protocols. With SNMP daemon which supports SMUX and
                AgentX protocol, Frr provides routing protocol MIBs.

                Configuration examples can now be found online at:
                http://docs.frrouting.org
                                                                                         
Change-log:     2018/01/23 First Version, 3.0.3
                2018/04/20 New major version, 4.0
                2019/02/10 New major version, 5.0.2
                2019/02/24 fixed script issues, watchfrr support, 5.0.2
                2020/04/02 New major version, 6.0.2
                ${TODAY} New major version, ${VERSION}
Current:        ${TODAY} New major version, ${VERSION}
EOF
elif [ $ARCH == "x86_64" ]
then
cat <<EOF> ${PACKAGE}-doc.tcz.info
Title:          ${PACKAGE}-doc.tcz
Description:    ${DOCDESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   ${ME}
Tags:           ${DOCTAGS}
Comments:       Frr is a routing software package that provides TCP/IP based
                routing services with routing protocols support such as RIPv1,
                RIPv2, RIPng, OSPFv2, OSPFv3, IS-IS, BGP-4, and BGP-4+.
                Frr also supports special BGP Route Reflector and Route Server behavior.
                In addition to traditional IPv4 routing protocols, Frr also supports
                IPv6 routing protocols. With SNMP daemon which supports SMUX and
                AgentX protocol, Frr provides routing protocol MIBs.

                Configuration examples can now be found online at:
                http://docs.frrouting.org
                                                                                         
Change-log:     ${TODAY} New major version, ${VERSION} (${ME})
Current:        ${TODAY} New major version, ${VERSION} (${ME})
EOF
fi


# omit .la files from dev
mkdir -p $TMPDIR-dev/usr/local/lib
mkdir -p $TMPDIR-dev/usr/local/include/frr

mv ${DESTDIR}/usr/local/lib/lib*.a $TMPDIR-dev/usr/local/lib                                                                                    
# omit .la files from dev
# mv ${DESTDIR}/usr/local/lib/frr/modules/*.*a $TMPDIR-dev/usr/local/lib/frr/modules
mv ${DESTDIR}/usr/local/include/frr/* $TMPDIR-dev/usr/local/include/frr/


###################################################                                                              
# Create info file                                #                                                              
###################################################                                                              
cd /tmp/submit/
if [ $ARCH == "i686" ]
then
cat <<EOF> ${PACKAGE}-dev.tcz.info                                                                 
Title:          ${PACKAGE}-dev.tcz                                                                                
Description:    ${DEVDESCRIPTION}                                                                                
Version:        ${VERSION}                                                                                       
Author:         ${AUTHORS}                                                                                       
Original-site:  ${HOMEPAGE}                                                                                      
Copying-policy: ${LICENSE}                                                                                       
Size:           ${size}                                                                                          
Extension_by:   ${ME}                                                                                            
Tags:           ${DEVTAGS}                                                                                       
Comments:       Frr is a routing software package that provides TCP/IP based                                     
                routing services with routing protocols support such as RIPv1,                                   
                RIPv2, RIPng, OSPFv2, OSPFv3, IS-IS, BGP-4, and BGP-4+.                                          
                Frr also supports special BGP Route Reflector and Route Server behavior.                         
                In addition to traditional IPv4 routing protocols, Frr also supports                             
                IPv6 routing protocols. With SNMP daemon which supports SMUX and                                 
                AgentX protocol, Frr provides routing protocol MIBs.                                             
                                                                                                                 
Change-log:     2018/01/23 First Version, 3.0.3
                2018/04/20 New major version, 4.0
                2019/02/10 New major version, 5.0.2
                2019/02/24 fixed script issues, watchfrr support, 5.0.2
                2020/04/02 New major version, 6.0.2
                ${TODAY} New major version, ${VERSION}
Current:        ${TODAY} New major version, ${VERSION}
EOF
elif [ $ARCH == "x86_64" ]
then
cat <<EOF> ${PACKAGE}-dev.tcz.info
Title:          ${PACKAGE}-dev.tcz
Description:    ${DEVDESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   brezular ${ME}                                                                                            
Tags:           ${DEVTAGS}
Comments:       Frr is a routing software package that provides TCP/IP based                                     
                routing services with routing protocols support such as RIPv1,                                   
                RIPv2, RIPng, OSPFv2, OSPFv3, IS-IS, BGP-4, and BGP-4+.                                          
                Frr also supports special BGP Route Reflector and Route Server behavior.                         
                In addition to traditional IPv4 routing protocols, Frr also supports                             
                IPv6 routing protocols. With SNMP daemon which supports SMUX and                                 
                AgentX protocol, Frr provides routing protocol MIBs.                                             
                                                                                                                 
Change-log:     2017/10/28 first version, 3.0.3 (brezular)
                2018/07/14 New major version, 5.0. (brezular)
                ${TODAY} New major version, ${VERSION} (${ME})
Current:        ${TODAY} New major version, ${VERSION} (${ME})
EOF
fi


###################################################
# Create .dep file                                #
###################################################
if [ $ARCH == "i686" ]
then
cat <<EOF> ${PACKAGE}-dev.tcz.dep
ipv6-netfilter-KERNEL.tcz
frr.tcz
json-c-dev.tcz
readline-dev.tcz
libcares-dev.tcz
EOF
elif [ $ARCH == "x86_64" ]
then
cat <<EOF> ${PACKAGE}-dev.tcz.dep
ipv6-netfilter-KERNEL.tcz
frr.tcz
json-c-dev.tcz
readline-dev.tcz
c-ares-dev.tcz
EOF
fi


###################################################
# Create base extension in temp dir               #
###################################################

cd $TMPDIR
find $TMPDIR -perm 777 -exec chmod 755 {} \;
find $TMPDIR -perm 555 -exec chmod 755 {} \;
find $TMPDIR -perm 444 -exec chmod 644 {} \;
find $TMPDIR -perm 666 -exec chmod 644 {} \;
find $TMPDIR -perm 664 -exec chmod 644 {} \;
sudo chown -R root:root $TMPDIR
sudo chown -R root:staff $TMPDIR/usr/local/tce.installed
sudo chmod -R 775 $TMPDIR/usr/local/tce.installed
#sudo chown tc:staff $TMPDIR/usr/local/var/frr
sudo chown -R tc:staff $TMPDIR/usr/local/etc/frr
sudo chmod 755 $TMPDIR/usr/local/lib/frr/init-functions

cd ..
mksquashfs $TMPDIR ${PACKAGE}.tcz
cd $TMPDIR
sudo sh -c "find usr -not -type d > ${PACKAGE}.tcz.list"
sudo mv ../${PACKAGE}.tcz .
sudo mv ../${PACKAGE}.tcz.info .
sudo mv ../${PACKAGE}.tcz.dep .

# Create md5 file

sudo sh -c "md5sum ${PACKAGE}.tcz > ${PACKAGE}.tcz.md5.txt"

# Cleanup temp directory

sudo rm -r -f usr

###################################################
# Create doc extension in temp dir                #
###################################################

cd $TMPDIR-doc
find $TMPDIR-doc -perm 777 -exec chmod 755 {} \;
find $TMPDIR-doc -perm 555 -exec chmod 755 {} \;
find $TMPDIR-doc -perm 444 -exec chmod 644 {} \;
find $TMPDIR-doc -perm 666 -exec chmod 644 {} \;
find $TMPDIR-doc -perm 664 -exec chmod 644 {} \;
sudo chown -R root:root $TMPDIR-doc
cd ..
mksquashfs $TMPDIR-doc ${PACKAGE}-doc.tcz
cd $TMPDIR-doc
sudo sh -c "find usr -not -type d > ${PACKAGE}-doc.tcz.list"
sudo mv ../${PACKAGE}-doc.tcz .
sudo mv ../${PACKAGE}-doc.tcz.info .

# Create md5 file

sudo sh -c "md5sum ${PACKAGE}-doc.tcz > ${PACKAGE}-doc.tcz.md5.txt"

# Cleanup temp directory

sudo rm -r -f usr

###################################################                                                              
# Create dev extension in temp dir                #                                                              
###################################################                                                              

cd $TMPDIR-dev
find $TMPDIR-dev -perm 777 -exec chmod 755 {} \;
find $TMPDIR-dev -perm 555 -exec chmod 755 {} \;
find $TMPDIR-dev -perm 444 -exec chmod 644 {} \;
find $TMPDIR-dev -perm 666 -exec chmod 644 {} \;
find $TMPDIR-dev -perm 664 -exec chmod 644 {} \;
sudo chown -R root:root $TMPDIR-dev
cd ..
mksquashfs $TMPDIR-dev ${PACKAGE}-dev.tcz
cd $TMPDIR-dev
sudo sh -c "find usr -not -type d > ${PACKAGE}-dev.tcz.list"
sudo mv ../${PACKAGE}-dev.tcz .
sudo mv ../${PACKAGE}-dev.tcz.info .
sudo mv ../${PACKAGE}-dev.tcz.dep .

# Create md5 file

sudo sh -c "md5sum ${PACKAGE}-dev.tcz > ${PACKAGE}-dev.tcz.md5.txt"

# Cleanup temp directory

sudo rm -r -f usr

