#!/bin/sh
#
######################################################
## Build script for CorePure64 16.x                  #
##                                                   #
## See .info for details                             #
##                                                   #
## Using the extenion from Lukasz032 for TC-6.x as   #
## example                                           #
##                                                   #
######################################################

######################################################
# Prepare extension creation                         #
######################################################
#download and install dependencies
tce-load -wi talloc-dev.tcz
# libkqueue is not needed for v3, only for v4. See
# https://github.com/FreeRADIUS/freeradius-server/issues/4077
# tce-load -i libkqueue-dev.tcz
# optional dependencies
tce-load -wi openssl-dev.tcz
tce-load -wi krb5-dev.tcz
tce-load -wi json-c-dev.tcz
tce-load -wi openldap-dev.tcz
tce-load -wi perl5.tcz
tce-load -wi mariadb-11.2-dev.tcz
tce-load -wi gdbm-dev.tcz
tce-load -wi curl-dev.tcz
tce-load -wi unbound-dev.tcz
tce-load -wi linux-pam-dev.tcz
tce-load -wi pcre2-dev.tcz
tce-load -wi db-dev.tcz
tce-load -wi ipv6-netfilter-KERNEL.tcz
PYVER=3.9
# download and install python
tce-load -wi python${PYVER}-dev.tcz

#download and install the compile tools
tce-load -wi compiletc.tcz
tce-load -wi squashfs-tools.tcz

######################################################
# Configure extension creation parameters            #
######################################################
# Variables
TODAY=`date +%Y/%m/%d`
PACKAGE="freeradius"
VERSION="3.2.8"
DESCRIPTION="FreeRADIUS a high performance and highly configurable multi-protocol policy server"
DOCDESCRIPTION="Documentation part of FreeRADIUS"
DEVDESCRIPTION="Development files part of FreeRADIUS"
AUTHORS="https://wiki.freeradius.org/project/Acknowledgements"
HOMEPAGE="https://www.freeradius.org/"
LICENSE="GPL-2.0"
ME="rhermsen"
TAGS="freeradius radius policy server"
DOCTAGS="man pages freeradius radius"
DEVTAGS="development freeradius radius"
DESTDIR=/tmp/dest/${PACKAGE}
TMPDIR=/tmp/submit/${PACKAGE}
SRCDIR=/tmp/src/${PACKAGE}
ARCH=`uname -m`

######################################################
# Compile extension                                  #
######################################################

rm -r -f $TMPDIR 2>/dev/null
rm -r -f $DESTDIR 2>/dev/null

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

# Source
[ ! -d  $SRCDIR ] && mkdir -p $SRCDIR
cd $SRCDIR
sudo rm -r $PACKAGE-server-release_3_2_8
[ ! -f "release_3_2_8.tar.gz" ] && wget https://github.com/FreeRADIUS/freeradius-server/archive/refs/tags/release_3_2_8.tar.gz
tar -xzf release_3_2_8.tar.gz
cd $PACKAGE-server-release_3_2_8

# Compile
sudo ln -s /usr/local/mysql/include/mysql.h /usr/local/mysql/include/mysql/mysql.h 
sudo ln -s /usr/local/mysql/include/errmsg.h /usr/local/mysql/include/mysql/errmsg.h
sudo ln -s /usr/local/mysql/include/mysqld_error.h /usr/local/mysql/include/mysql/mysqld_error.h
# Configure it
./configure  --prefix=/usr/local \
--sysconfdir=/usr/local/etc/$PACKAGE \
--localstatedir=/var/$PACKAGE \
--with-mysql-include-dir=/usr/local/mysql/include \
--with-mysql-lib-dir=/usr/local/mysql/lib

time make
mkdir -p $DESTDIR
# time make DESTDIR=$DESTDIR install
sudo R=$DESTDIR make install
sudo chown -R tc:staff $DESTDIR


# Below lines just for savekeeping in case make is used in a build script, and needs troubleshooting.
# https://stackoverflow.com/questions/5820303/how-do-i-force-make-gcc-to-show-me-the-commands
# make -n install
# make SHELL='sh -x' DESTDIR=$DESTDIR install 

######################################################
# Base extension                                     #
######################################################
sudo rm -r ${TMPDIR}* 2>/dev/null

mkdir -p  ${TMPDIR}/usr/local/bin
mkdir -p  ${TMPDIR}/usr/local/sbin
mkdir -p  ${TMPDIR}/usr/local/lib
mkdir -p  ${TMPDIR}/usr/local/share
mkdir -p  ${TMPDIR}/usr/local/etc/${PACKAGE}
mkdir -p  ${TMPDIR}/usr/local/share/doc/${PACKAGE}
mkdir -p ${TMPDIR}/usr/local/tce.installed

mv ${DESTDIR}/usr/local/bin/* ${TMPDIR}/usr/local/bin
mv ${DESTDIR}/usr/local/sbin/* ${TMPDIR}/usr/local/sbin
cd ${DESTDIR}/usr/local/etc/freeradius/
tar -czf default.raddb raddb
mv ${DESTDIR}/usr/local/etc/${PACKAGE}/default.raddb ${TMPDIR}/usr/local/etc/${PACKAGE}
# mv ${DESTDIR}/usr/local/etc/* ${TMPDIR}/usr/local/etc
mv ${DESTDIR}/usr/local/lib/*.so* ${TMPDIR}/usr/local/lib
mv ${DESTDIR}/usr/local/share/${PACKAGE} ${TMPDIR}/usr/local/share
cp ${SRCDIR}/${PACKAGE}-server-release_3_2_8/LICENSE ${TMPDIR}/usr/local/share/doc/${PACKAGE}/

######################################################
# init file                                          #
###################################################### 

######################################################
# Create info file                                   #
######################################################
cd /tmp/submit/
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:       The FreeRADIUS Server Project is a high performance
                and highly configurable multi-protocol policy server, supporting
                RADIUS, DHCPv4 DHCPv6, DNS, TACACS+ and VMPS. It is
                available under the terms of the GNU GPLv2. Using RADIUS
                allows authentication and authorization for a network to be
                centralized, and minimizes the number of changes that have
                to be done when adding or deleting new users to a network.

                Compiled from source for CP64 16.x.

Change-log:     ${TODAY} First version, ${VERSION} (${ME})
Current:        ${TODAY} First version, ${VERSION} (${ME})
EOF

######################################################                            
# Create .dep file                                   #
######################################################
cat <<EOF> ${PACKAGE}.tcz.dep
talloc.tcz
curl.tcz
json-c.tcz
krb5.tcz
linux-pam.tcz
openldap.tcz
unbound.tcz
mariadb-11.2.tcz
perl5.tcz
pcre2.tcz
gcc_libs.tcz
db.tcz
ipv6-netfilter-KERNEL.tcz
EOF



######################################################
# Create install script file                         #
######################################################
cat <<EOF> $TMPDIR/usr/local/tce.installed/${PACKAGE}
#!/bin/sh
[ ! \`id -u radius 2> /dev/null\` ] && adduser -h /usr/local/etc/freeradius/raddb -G staff -D -H radius
ln -s /usr/local/sbin/rc.radiusd /etc/init.d/radiusd
if [ ! -d /usr/local/etc/freeradius/raddb ]; then
        tar -zxf /usr/local/etc/freeradius/default.raddb -C /usr/local/etc/freeradius
        chown -R radius:staff /usr/local/etc/freeradius
        echo usr/local/etc/freeradius/ >> /opt/.filetool.lst
fi
mkdir -p /var/log/radius/radacct
chown -R radius:staff /var/log/radius
chmod -R 700 /var/log/radius
mkdir -p /var/run/radiusd
chown -R radius:staff /var/run/radiusd
chmod -R 755 /var/run/radiusd
EOF

# Move files to doc extension
mkdir -p $TMPDIR-doc/usr/local/share/doc

mv ${DESTDIR}/usr/local/share/doc/${PACKAGE} ${TMPDIR}-doc/usr/local/share/doc/
mv ${DESTDIR}/usr/local/share/man ${TMPDIR}-doc/usr/local/share/

######################################################
# Create info file                                   #
######################################################
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:       The FreeRADIUS Server Project is a high performance
                and highly configurable multi-protocol policy server, supporting
                RADIUS, DHCPv4 DHCPv6, DNS, TACACS+ and VMPS. It is
                available under the terms of the GNU GPLv2. Using RADIUS
                allows authentication and authorization for a network to be
                centralized, and minimizes the number of changes that have
                to be done when adding or deleting new users to a network.

                Compiled from source for CP64 16.x.

Change-log:     ${TODAY} first version, ${VERSION} (${ME})
Current:        ${TODAY} first version, ${VERSION} (${ME})
EOF

# Move files to dev extension
mkdir -p $TMPDIR-dev/usr/local/include
mkdir -p $TMPDIR-dev/usr/local/lib

mv ${DESTDIR}/usr/local/include/${PACKAGE} ${TMPDIR}-dev/usr/local/include
mv ${DESTDIR}/usr/local/lib/*.a ${TMPDIR}-dev/usr/local/lib/

######################################################
# Create info file                                   #
######################################################
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:       The FreeRADIUS Server Project is a high performance
                and highly configurable multi-protocol policy server, supporting
                RADIUS, DHCPv4 DHCPv6, DNS, TACACS+ and VMPS. It is
                available under the terms of the GNU GPLv2. Using RADIUS
                allows authentication and authorization for a network to be
                centralized, and minimizes the number of changes that have
                to be done when adding or deleting new users to a network.

                Compiled from source for CP64 16.x.

Change-log:     ${TODAY} first version, ${VERSION} (${ME})
Current:        ${TODAY} first version, ${VERSION} (${ME})
EOF

######################################################                            
# Create .dep file                                   #
######################################################
cat <<EOF> ${PACKAGE}-dev.tcz.dep
${PACKAGE}.tcz
EOF

######################################################
# Create base extension in temp dir                  #
######################################################
find $TMPDIR/ -type d | xargs chmod -v 755;

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

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 {} \;

# find $TMPDIR/usr/local/etc/${PACKAGE}/ -type f | xargs chmod -v 644;
# chmod 755 $TMPDIR/usr/local/etc/${PACKAGE}/raddb/certs/Makefile
# chmod 755 $TMPDIR/usr/local/etc/${PACKAGE}/raddb/certs/bootstrap
# chmod 755 $TMPDIR/usr/local/etc/${PACKAGE}/raddb/mods-config/realm/*.sh
# chmod 755 $TMPDIR/usr/local/etc/${PACKAGE}/raddb/mods-config/sql/main/sqlite/*.pl
# chmod 755 $TMPDIR/usr/local/etc/${PACKAGE}/raddb/mods-config/sql/main/sqlite/*.sh
# chmod 755 $TMPDIR/usr/local/etc/${PACKAGE}/raddb/mods-config/python3/*.py


chmod -R 775 $TMPDIR/usr/local/tce.installed
sudo chown -R root:root $TMPDIR
sudo chown -R root:staff $TMPDIR/usr/local/tce.installed
# sudo chown -R root:staff $TMPDIR/usr/local/etc/freeradius

cd /tmp/submit/

mksquashfs $TMPDIR ${PACKAGE}.tcz

cd $TMPDIR
sudo sh -c "find usr -not -type d | sed 's/\(.*$\)/\/\1/g' > ${PACKAGE}.tcz.list"
sudo mv ../${PACKAGE}.tcz .
sudo mv ../${PACKAGE}.tcz.dep .
sudo mv ../${PACKAGE}.tcz.info .

# 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 {} \;
chmod 755 $TMPDIR-doc/usr/local/share/doc/${PACKAGE}/rfc/Makefile
chmod 755 $TMPDIR-doc/usr/local/share/doc/${PACKAGE}/rfc/*.pl
chmod 755 $TMPDIR-doc/usr/local/share/doc/${PACKAGE}/schemas/logstash/*.sh
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 | sed 's/\(.*$\)/\/\1/g' > ${PACKAGE}-doc.tcz.list"
sudo mv ../${PACKAGE}-doc.tcz* .
#sudo mv /tmp/submit/${PACKAGE}/${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-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 {} \;
find $TMPDIR-dev/usr/local/lib/ -type f | xargs chmod -v 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 | sed 's/\(.*$\)/\/\1/g' > ${PACKAGE}-dev.tcz.list"
sudo mv ../${PACKAGE}-dev.tcz* .
# sudo mv /tmp/submit/${PACKAGE}/${PACKAGE}-dev.tcz.info .

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

# Cleanup temp directory
sudo rm -r -f usr

