#!/bin/sh
#
######################################################
## Build script for piCore 16.x                        #
##                                                    #
## See .info for details                              #
######################################################

######################################################
# Prepare extension creation                         #
######################################################
#download and install dependencies
tce-load -wi openssl.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="dropbear"
VERSION="2024.86"
DESCRIPTION="Small SSH server and client"
DOCDESCRIPTION="Document part of dropbear, Small SSH server and client"
DEVDESCRIPTION="Development files part of dropbear, Small SSH server and client"
AUTHORS="Matt Johnston"
HOMEPAGE="https://matt.ucc.asn.au/dropbear/dropbear.html"
LICENSE="https://secure.ucc.asn.au/hg/dropbear/raw-file/tip/LICENSE"
ME="rhermsen"
TAGS="dropbear ssh dbclient"
DOCTAGS="man pages dropbear ssh dbclient"
DEVTAGS="development dropbear ssh dbclient"
DESTDIR=/tmp/${PACKAGE}
TMPDIR=/tmp/submit/${PACKAGE}
SRCDIR=/tmp/src/${PACKAGE}
ARCH=`uname -m`


# Remove dirs and files left from previous creation
rm -r -f $SRCDIR/${PACKAGE}-${VERSION} 2>/dev/null
rm -r -f $WRKDIR 2>/dev/null
rm -r -f $DESTDIR 2>/dev/null

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                                  #
######################################################

# Export variables needed for compilation
ARCH=`uname -m`
if [ $ARCH == "i686" ]
then
export CFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export CXXFLAGS="-Os -pipe -fno-exceptions -fno-rtti -march=i486 -mtune=i686"
# export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe"
elif [ $ARCH == "x86_64" ]
then
export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe"
elif [[ $ARCH == "armv7*" ]]
then
echo
export CFLAGS="-flto=auto -Os -pipe -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"
export CXXFLAGS="-flto=auto -Os -pipe -fno-exceptions -fno-rtti -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp" BITS="linux32"
# no specific arch dependent parameters
elif [ $ARCH == "aarch64" ]
then
export CFLAGS="-flto=auto -Os -pipe -march=armv8-a+crc -mtune=cortex-a72"
export CXXFLAGS="-flto=auto -Os -pipe -fno-exceptions -fno-rtti -march=armv8-a+crc -mtune=cortex-a72" BITS=""
echo
# no specific arch dependent parameters
else
exit 1
fi
export LDFLAGS="-Wl,-O1"
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

# Download source in SRCDIR
[ ! -d  $SRCDIR ] && mkdir -p $SRCDIR
cd $SRCDIR
[ ! -f   ${SRCDIR}/${PACKAGE}-${VERSION}.tar.bz2 ] && wget https://matt.ucc.asn.au/dropbear/releases/${PACKAGE}-${VERSION}.tar.bz2
[ ! -f   ${SRCDIR}/banner ] && wget http://www.tinycorelinux.net/6.x/armv6/tcz/src/dropbear/banner
[ ! -f   ${SRCDIR}/dropbear ] && wget http://www.tinycorelinux.net/6.x/armv6/tcz/src/dropbear/dropbear


# Unpack source in current directory
tar xjf $SRCDIR/${PACKAGE}-${VERSION}.tar.bz2

[ -f   ${SRCDIR}/localoptions.h ] && cp ${SRCDIR}/localoptions.h $SRCDIR/${PACKAGE}-${VERSION}

# Configure it
cd $SRCDIR/${PACKAGE}-${VERSION}
./configure  --prefix=/usr/local --disable-lastlog

# Compile
mkdir $DESTDIR
make PRGRAMS="dropbear dbclient dropbearkey dropbearconvert scp" MULTI=1
make PRGRAMS="dropbear dbclient dropbearkey dropbearconvert scp" MULTI=1 strip
make PRGRAMS="dropbear dbclient dropbearkey dropbearconvert scp" MULTI=1 DESTDIR=$DESTDIR install
# make scp

######################################################
# 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/etc/dropbear
mkdir -p $TMPDIR/usr/local/etc/init.d
mkdir -p $TMPDIR/usr/local/share/doc/dropbear

mv $DESTDIR/usr/local/bin/dropbearmulti $TMPDIR/usr/local/bin
# mv scp $TMPDIR/usr/local/bin
cp  ${SRCDIR}/${PACKAGE}-${VERSION}/LICENSE $TMPDIR/usr/local/share/doc/dropbear

# Add TC specific files
cp  ${SRCDIR}/banner $TMPDIR/usr/local/etc/dropbear
cp  ${SRCDIR}/dropbear $TMPDIR/usr/local/etc/init.d
sed -i 's/dropbear_rss_host_key/dropbear_rsa_host_key/g' $TMPDIR/usr/local/etc/init.d/dropbear
chmod +x $TMPDIR/usr/local/etc/init.d/dropbear

# Create symlinks
cd $TMPDIR/usr/local/bin
ln -s dropbearmulti ssh
ln -s dropbearmulti dbclient
ln -s dropbearmulti dropbearkey
ln -s dropbearmulti dropbearconvert
ln -s dropbearmulti scp
cd $TMPDIR/usr/local/sbin
ln -s ../bin/dropbearmulti dropbear

###################################################
# Create info file                                #
###################################################
cd /tmp/submit/
if [ $ARCH == "i686" ]
then
cat <<EOF> ${PACKAGE}.tcz.info
Title:          ${EXTNAM}.tcz
Description:    ${DESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   bmarkus,jim
Tags:           ${TAGS}
Comments:       dropbear docs
                Dropbear is a relatively small SSH server and client.
                Use /usr/local/etc/init.d/dropbear start|stop|retart|status
                to manage service as root
                Less than 1024 bit RSA keys are not supported anymore.
                From Dropbear 2022.83 DSA keys are not supported anymore.
                
Change-log:     2014/09/28 First version, 2014.65
                2015/05/23 Fix init script type-o (thanks to M.S. for reporting)
                2020/10/27 Update to 2020.80 compiled in key paths, xauth fix
                ${TODAY} updated version, ${VERSION} (${ME})
Current:        ${TODAY} updated version, ${VERSION} (${ME})
EOF
elif [ $ARCH == "x86_64" ]
then
cat <<EOF> ${PACKAGE}.tcz.info
Title:          ${EXTNAM}.tcz
Description:    ${DESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   bmarkus,jim
Tags:           ${TAGS}
Comments:       dropbear docs
                Dropbear is a relatively small SSH server and client.
                Use /usr/local/etc/init.d/dropbear start|stop|retart|status
                to manage service as root
                Less than 1024 bit RSA keys are not supported anymore.
                From Dropbear 2022.83 DSA keys are not supported anymore.
                
Change-log:     2014/09/28 First version, 2014.65
                2015/05/23 Fix init script type-o (thanks to M.S. for reporting)
                2020/11/11 Update to 2020.80 compiled in key paths, xauth fix
                ${TODAY} updated version, ${VERSION} (${ME})
Current:        ${TODAY} updated version, ${VERSION} (${ME})
EOF
elif [[ $ARCH == "armv7*" ]]
then
cat <<EOF> ${PACKAGE}.tcz.info
Title:          ${EXTNAM}.tcz
Description:    ${DESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   bmarkus,jim
Tags:           ${TAGS}
Comments:       dropbear
                Dropbear is a relatively small SSH server and client.
                Use "sudo /usr/local/etc/init.d/dropbear start|stop|retart|status"
                to manage service as root
                Less than 1024 bit RSA keys are not supported anymore.
                From Dropbear 2022.83 DSA keys are not supported anymore.
                
Change-log:     2014/09/28 First version, 2014.65
                2015/05/23 Fix init script type-o (thanks to M.S. for reporting)
                2020/11/11 Update to 2020.80 compiled in key paths, xauth fix
                ${TODAY} updated version, ${VERSION} (${ME})
Current:        ${TODAY} updated version, ${VERSION} (${ME})
EOF
elif [ $ARCH == "aarch64" ]
then
cat <<EOF> ${PACKAGE}.tcz.info
Title:          ${EXTNAM}.tcz
Description:    ${DESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   bmarkus,jim
Tags:           ${TAGS}
Comments:       dropbear
                Dropbear is a relatively small SSH server and client.
                Use "sudo /usr/local/etc/init.d/dropbear start|stop|retart|status"
                to manage service as root
                Less than 1024 bit RSA keys are not supported anymore.
                From Dropbear 2022.83 DSA keys are not supported anymore.
                
Change-log:     2014/09/28 First version, 2014.65
                2015/05/23 Fix init script type-o (thanks to M.S. for reporting)
                2020/11/11 Update to 2020.80 compiled in key paths, xauth fix
                ${TODAY} updated version, ${VERSION} (${ME})
Current:        ${TODAY} updated version, ${VERSION} (${ME})
EOF
fi

# 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

mv $DESTDIR/usr/local/share/man/man1/dbclient.1 $TMPDIR-doc/usr/local/share/man/man1
mv $DESTDIR/usr/local/share/man/man1/dropbear* $TMPDIR-doc/usr/local/share/man/man1
mv $DESTDIR/usr/local/share/man/man8/dropbear* $TMPDIR-doc/usr/local/share/man/man8

###################################################
# 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:   bmarkus,jim
Tags:           ${DOCTAGS}
Comments:       dropbear docs
                Dropbear is a relatively small SSH server and client.
                Use /usr/local/etc/init.d/dropbear start|stop|retart|status
                to manage service as root
                Less than 1024 bit RSA keys are not supported anymore.
                From Dropbear 2022.83 DSA keys are not supported anymore.
                
Change-log:     2014/09/28 First version, 2014.65
                2020/10/27 Update to 2020.80 compiled in key paths,xauth fix
                ${TODAY} updated version, ${VERSION} (${ME})
Current:        ${TODAY} updated version, ${VERSION} (${ME})
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:   bmarkus,jim
Tags:           ${DOCTAGS}
Comments:       dropbear docs
                Dropbear is a relatively small SSH server and client.
                Use /usr/local/etc/init.d/dropbear start|stop|retart|status
                to manage service as root
                Less than 1024 bit RSA keys are not supported anymore.
                From Dropbear 2022.83 DSA keys are not supported anymore.
                
Change-log:     2014/09/28 First version, 2014.65
                2020/11/11 Update to 2020.80 compiled in key paths,xauth fix
                ${TODAY} updated version, ${VERSION} (${ME})
Current:        ${TODAY} updated version, ${VERSION} (${ME})
EOF
elif [[ $ARCH == "armv7*" ]]
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:   bmarkus,jim
Tags:           ${DOCTAGS}
Comments:       dropbear docs
                Dropbear is a relatively small SSH server and client.
                Use /usr/local/etc/init.d/dropbear start|stop|retart|status
                to manage service as root
                Less than 1024 bit RSA keys are not supported anymore.
                From Dropbear 2022.83 DSA keys are not supported anymore.
                
Change-log:     2014/09/28 First version, 2014.65
                2020/11/11 Update to 2020.80 compiled in key paths,xauth fix
                ${TODAY} updated version, ${VERSION} (${ME})
Current:        ${TODAY} updated version, ${VERSION} (${ME})
EOF
elif [ $ARCH == "aarch64" ]
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:   bmarkus,jim
Tags:           ${DOCTAGS}
Comments:       dropbear docs
                Dropbear is a relatively small SSH server and client.
                Use /usr/local/etc/init.d/dropbear start|stop|retart|status
                to manage service as root
                Less than 1024 bit RSA keys are not supported anymore.
                From Dropbear 2022.83 DSA keys are not supported anymore.
                
Change-log:     2014/09/28 First version, 2014.65
                2020/11/11 Update to 2020.80 compiled in key paths,xauth fix
                ${TODAY} updated version, ${VERSION} (${ME})
Current:        ${TODAY} updated version, ${VERSION} (${ME})
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
# --no-fix required for submitqc if below non-default owner or permissions are used
# sudo chown -R root:staff $TMPDIR/usr/local/tce.installed
# sudo chmod -R 775 $TMPDIR/usr/local/tce.installed
sudo chown root:staff $TMPDIR/usr/local/etc/dropbear/banner
sudo chown root:staff $TMPDIR/usr/local/etc/init.d/dropbear

cd ..
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* .

# 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 | sed 's/\(.*$\)/\/\1/g' > ${PACKAGE}-doc.tcz.list"
sudo mv ../${PACKAGE}-doc.tcz* .

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

# Cleanup temp directory
sudo rm -r -f usr


























