#!/bin/sh
#
######################################################
# Build script for RPI                               #
#                                                    #
# See .info for details                              #
######################################################

STD_DEPS="compiletc squashfs-tools libtool"
BUILD_DEPS="openssl-dev"
TCZ_DEPS=""
DEV_TCZ_DEPS=""
FULL_DEPS="$STD_DEPS $BUILD_DEPS"

for deps in $FULL_DEPS; do
	tce-status -i | grep -q ${deps%.*}
	if [ $? -ne 0 ]; then
		tce-load -i $deps >/dev/null 2>&1
		if [ $? -eq 0 ]; then
			echo "Loaded $deps..."
		else
			echo "Failed to load $deps... exiting"
			exit 1
		fi
	fi
done


######################################################
# Configure extension creation parameters            #
######################################################

SRCNAM=openssh-8.4p1.tar.gz
WRKDIR=openssh-8.4p1
EXTNAM=openssh
TMPDIR=/tmp/openssh

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

# Remove dirs and files left from previous creation

rm -r -f $WRKDIR

rm -r -f $TMPDIR
rm -r -f $TMPDIR-doc

# Crete temporary directory

mkdir -p $TMPDIR

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

# Export variables needed for compilation

case $(uname -m) in
	armv*)
		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"
	;;
	aarch64)
		export CFLAGS="-Os -pipe -march=armv8-a+crc -mtune=cortex-a72"
		export CXXFLAGS="-Os -pipe -fexceptions -fno-rtti -march=armv8-a+crc -mtune=cortex-a72"
	;;
esac
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

# Unpack source in current directory

tar -xf $SRCNAM

# Configure it


cd $WRKDIR

./configure \
--prefix=/usr/local \
--sysconfdir=/usr/local/etc/ssh \
--without-libedit \
--localstatedir=/var \
--sysconfdir=/usr/local/etc/ssh \
--libexecdir=/usr/local/lib/openssh \
--with-privsep-path=/var/lib/sshd \
--with-privsep-user=nobody \
--with-xauth=/usr/local/bin/xauth \
--with-md5-passwords

# Compile

make -j4

# Install in base temp dir

make install DESTDIR=$TMPDIR

# Delete compilation work directory

cd ..
rm -r -f $WRKDIR

# Delete unneded files

rm -r -f $TMPDIR/var

# 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


# Make TC sopecific changes

mv $TMPDIR/usr/local/etc/ssh/ssh_config $TMPDIR/usr/local/etc/ssh/ssh_config.example
mv $TMPDIR/usr/local/etc/ssh/sshd_config $TMPDIR/usr/local/etc/ssh/sshd_config.example

mkdir -p $TMPDIR/usr/local/etc/init.d
cp openssh.ini $TMPDIR/usr/local/etc/init.d/openssh
chmod -R 775 $TMPDIR/usr/local/etc/init.d
chown -R root.staff $TMPDIR/usr/local/etc/init.d

# Move files to doc extension

mkdir -p $TMPDIR-doc/usr/local
mv $TMPDIR/usr/local/share $TMPDIR-doc/usr/local

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

cd $TMPDIR
cd ..
mksquashfs $TMPDIR $EXTNAM.tcz
cd $TMPDIR
find usr -not -type d > $EXTNAM.tcz.list
mv ../$EXTNAM.tcz .

# Create md5 file

md5sum $EXTNAM.tcz > $EXTNAM.tcz.md5.txt

# Cleanup temp directory

rm -r -f usr

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

cd $TMPDIR-doc
cd ..
mksquashfs $TMPDIR-doc $EXTNAM-doc.tcz
cd $TMPDIR-doc
find usr -not -type d > $EXTNAM-doc.tcz.list
mv ../$EXTNAM-doc.tcz .

# Create md5 file

md5sum $EXTNAM-doc.tcz > $EXTNAM-doc.tcz.md5.txt

# Cleanup temp directory

rm -r -f usr
