#!/bin/sh
# Original script by andriscom found here:
# http://repo.tinycorelinux.net/3.x/tcz/src/lshw/
# 
# Modified Apr. 1, 2020 by Rich
# Adapted to build exfat-utils.  Rich
#

echo "Don't forget to update this scripts change-log!!!!
Press ENTER to start, Ctrl-C to abort"
read

TOOLCHAIN="compiletc git sstrip submitqc"
BUILD_DEPS="automake fuse fuse-dev"

EXFAT="exfat"
STARTDIR="$PWD"
PACKAGEDIR="package"
EXFATDIR="$PACKAGEDIR/$EXFAT"

DESTDIR="$STARTDIR/$EXFATDIR"
PREFIX="/usr/local"

GDEBUG="No"
OPTIMIZE="-Os"
# Uncomment the next line to compile a version that can be run under gdb.
#GDEBUG="Debug"

if [ "$GDEBUG" == "Debug" ]
then
	OPTIMIZE="-O0 -ggdb"
fi


PROCESSOR_TYPE=`uname -m`
echo "$PROCESSOR_TYPE detected."

case "$PROCESSOR_TYPE" in
		i686)
		CFLAGS=" -flto -march=i486 -mtune=i686 $OPTIMIZE -pipe -Wall -Wextra -fno-plt"
#		CXXFLAGS="g++ -flto -march=i486 -mtune=i686 $OPTIMIZE -pipe -fno-exceptions -fno-rtti"
		LDFLAGS="-Wl,-T/usr/local/lib/ldscripts/elf_i386.xbn"
		;;

		x86_64)
		CFLAGS=" -flto -mtune=generic $OPTIMIZE -pipe -Wall -Wextra -fno-plt"
#		CXXFLAGS="g++ -mtune=generic $OPTIMIZE -pipe -fno-exceptions -fno-rtti"
		LDFLAGS="-Wl,-T/usr/local/lib/ldscripts/elf_x86_64.xbn"
		;;

		armv*)
		CFLAGS=" -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp $OPTIMIZE -pipe"
#		CXXFLAGS="g++ -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp $OPTIMIZE -pipe -fno-exceptions -fno-rtti"
		LDFLAGS="-Wl,-O1"
		;;

		*)
		echo "$PROCESSOR_TYPE: Unknown processor type. Please add an entry for it in this script."
		exit
		;;
esac

export DESTDIR PREFIX CFLAGS CXXFLAGS LDFLAGS

# Remove previous source and build files.
busybox rm -rf "$EXFAT"
busybox rm -rf "$PACKAGEDIR"

# Set up destination directories for copyright files.
busybox mkdir -p "$EXFATDIR$PREFIX/share/doc/$EXFAT-utils"
busybox mkdir -p "$EXFATDIR-fuse$PREFIX/share/doc/$EXFAT-fuse"

# Set up destination directories for the fuse extension.
busybox mkdir -p "$EXFATDIR-fuse$PREFIX/sbin"
busybox mkdir -p "$EXFATDIR-fuse$PREFIX/share/man/man8"
busybox mkdir -p "$EXFATDIR-fuse$PREFIX/tce.installed"

SOURCE="https://github.com/relan/$EXFAT"

# Download packages required for compiling
tce-load -w "$TOOLCHAIN $BUILD_DEPS"

# Install packages required for compiling
tce-load -i "$TOOLCHAIN $BUILD_DEPS"

echo "Cloning source package."
git clone "$SOURCE"

# Copy the copyright files.
cp "$EXFAT/COPYING" "$EXFATDIR$PREFIX/share/doc/$EXFAT-utils"
cp "$EXFAT/COPYING" "$EXFATDIR-fuse$PREFIX/share/doc/$EXFAT-fuse"

# Enter the source tree. Must be before the  VERSION=  statement.
cd $EXFAT

DATE=`date '+%Y/%m/%d'`
VERSION=`git describe --tags --long | cut -d - -f 1,2 | tr - .`

# Create a source tarball.
busybox tar -czf "../$EXFAT-$VERSION".tar.gz "../$EXFAT"


# Make exfat
autoreconf --install
./configure
make
make install

# Strip programs and libraries
cd "$DESTDIR"
if [ "$GDEBUG" == "No" ]
then
	sudo find . | xargs file | grep "executable" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
	sudo find . | xargs file | grep "shared object" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip -g 2> /dev/null
fi

# This is where the destination directory is.
cd "$STARTDIR/$PACKAGEDIR"

# Separate the fuse (mount commands) extension from the utils extension.
mv "$EXFAT$PREFIX/sbin/mount.exfat-fuse" "$EXFAT-fuse$PREFIX/sbin"
mv "$EXFAT$PREFIX/sbin/mount.exfat" "$EXFAT-fuse$PREFIX/sbin"
mv "$EXFAT$PREFIX/share/man/man8/mount.exfat-fuse.8" "$EXFAT-fuse$PREFIX/share/man/man8"

# Create tce.installed file for exfat-fuse.
echo "#!/bin/sh

ln -sf /usr/local/sbin/mount.exfat /sbin/
ln -sf /usr/local/sbin/mount.exfat-fuse  /sbin/
" > "$EXFAT-fuse$PREFIX/tce.installed/$EXFAT-fuse"
sudo chown -R root:staff "$EXFAT-fuse$PREFIX/tce.installed"
sudo chmod -R 775 "$EXFAT-fuse$PREFIX/tce.installed"

# Create .tcz, .md5.txt, and .list files for exfat-utils.
mksquashfs $EXFAT $EXFAT-utils.tcz
md5sum $EXFAT-utils.tcz > $EXFAT-utils.tcz.md5.txt
cd $EXFAT
# Find files including their path starting from the current directory,
# cut the leading dot, sort it, and save it to the .list file.
find . -not -type d | cut -c 2- | sort > ../$EXFAT-utils.tcz.list
cd ..

# Create .info file for exfat-utils.
SIZE="`du -h $EXFAT-utils.tcz | cut -f1`"
echo "Title:		$EXFAT-utils.tcz
Description:	exFAT filesystem support
Version:	$VERSION 
Author:		Andrew Nayenko
Original-site:	https://github.com/relan/exfat
Copying-policy:	GPLv2
Size:		$SIZE
Extension_by:	Rich
Tags:		exFAT filesystem support
Comments:	Utilities to create, check, label
		and dump exFAT filesystems.

		Built with:
		CFLAGS=$CFLAGS
		CXXFLAGS=$CXXFLAGS
		LDFLAGS=$LDFLAGS
		PREFIX=$PREFIX

		For man entry(s) see:
		/usr/local/share/man/man8/

Source:		$SOURCE
Change-log:	2020/04/02 Original release Version: $VERSION
Current:	$DATE Original release Version: $VERSION
" > $EXFAT-utils.tcz.info


# Create .tcz, .md5.txt, and .list files for exfat-fuse.
mksquashfs $EXFAT-fuse $EXFAT-fuse.tcz
md5sum $EXFAT-fuse.tcz > $EXFAT-fuse.tcz.md5.txt
echo -ne "fuse.tcz\n" > $EXFAT-fuse.tcz.dep
cd $EXFAT-fuse
# Find files including their path starting from the current directory,
# cut the leading dot, sort it, and save it to the .list file.
find . -not -type d | cut -c 2- | sort > ../$EXFAT-fuse.tcz.list
cd ..

# Create .info file for exfat-fuse.
SIZE="`du -h $EXFAT-fuse.tcz | cut -f1`"
echo "Title:		$EXFAT-fuse.tcz
Description:	exFAT filesystem support
Version:	$VERSION 
Author:		Andrew Nayenko
Original-site:	https://github.com/relan/exfat
Copying-policy:	GPLv2
Size:		$SIZE
Extension_by:	Rich
Tags:		exFAT filesystem support
Comments:	Read and write exFAT driver for
		mounting exFAT filesystems.

		Built with:
		CFLAGS=$CFLAGS
		CXXFLAGS=$CXXFLAGS
		LDFLAGS=$LDFLAGS
		PREFIX=$PREFIX

		For man entry(s) see:
		/usr/local/share/man/man8/

Source:		$SOURCE
Change-log:	2020/04/02 Original release Version: $VERSION
Current:	$DATE Original release Version: $VERSION
" > $EXFAT-fuse.tcz.info

# Check the newly created extension files for errors.
submitqc --libs 2>&1 > submitqc.txt

# Create archives of the 2 packages.
busybox tar -czf "$EXFAT-fuse".tar.gz "$EXFAT-fuse".tc*
busybox tar -czf "$EXFAT-utils".tar.gz "$EXFAT-utils".tc*

# Tar the 2 package archives, source package, and build script. Include processor type as part of archive name.
busybox tar -czf "$EXFAT-$PROCESSOR_TYPE".tar.gz *.tar.gz "../$EXFAT-$VERSION".tar.gz "../$0"

# bcrypt the final file with the password  tinycore
yes tinycore | bcrypt "$EXFAT-$PROCESSOR_TYPE".tar.gz
busybox cp -f "$EXFAT-$PROCESSOR_TYPE".tar.gz.bfe ../
echo "Check $PACKAGEDIR/submitqc.txt for any errors."
echo "Send $EXFAT-$PROCESSOR_TYPE.tar.gz to: tcesubmit@gmail.com"

cd "$STARTDIR"
