#!/bin/sh

alias cp='busybox cp -a'
alias mkdir='busybox mkdir -p'
alias rm='busybox rm -rf'

PROGRAM="AutoCursor"
OPTIMIZE="-Os"
SYMBOLS="-g"

TOOLCHAIN="compiletc sstrip submitqc"
BUILD_DEPS="libXfixes-dev"
PACKAGE_DEPS="libXfixes"

GDEBUG="No"
# 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 -fuse-linker-plugin -march=i486 -mtune=i686 $OPTIMIZE $SYMBOLS -pipe -Wall -Wextra -fno-plt"
        CXXFLAGS="-flto -fuse-linker-plugin -march=i486 -mtune=i686 $OPTIMIZE $SYMBOLS -pipe -Wall -Wextra -fno-exceptions -fno-rtti"
        LDFLAGS="-Wl,-T/usr/local/lib/ldscripts/elf_i386.xbn"
        ;;

        x86_64)
        CFLAGS="-flto -fuse-linker-plugin -mtune=generic $OPTIMIZE $SYMBOLS -pipe -Wall -Wextra -fno-plt"
        CXXFLAGS="-flto -fuse-linker-plugin -mtune=generic $OPTIMIZE $SYMBOLS -pipe -Wall -Wextra -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 $SYMBOLS -pipe -Wall -Wextra"
        CXXFLAGS="-march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp $OPTIMIZE $SYMBOLS -pipe -Wall -Wextra -fno-exceptions -fno-rtti"
        LDFLAGS="-Wl,-O1"
        ;;

        aarch64)
        CFLAGS="-march=armv8-a+crc -mtune=cortex-a72 $OPTIMIZE $SYMBOLS -pipe -Wall -Wextra"
        CXXFLAGS="-march=armv8-a+crc -mtune=cortex-a72 $OPTIMIZE $SYMBOLS -pipe -Wall -Wextra -fno-exceptions -fno-rtti"
        LDFLAGS="-Wl,-O1"
        ;;

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

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

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

# Remove output from previous build.
rm pkg
rm "$PROGRAM".tcz
rm "$PROGRAM".tcz.dep
rm "$PROGRAM".tcz.info
rm "$PROGRAM".tcz.list
rm "$PROGRAM".tcz.md5.txt
rm "$PROGRAM".tcz.zsync

# Create the destination directories for packaging.
mkdir pkg/usr/local/bin
mkdir pkg/usr/local/share/doc/"$PROGRAM"

# Build "$PROGRAM"
#gcc  -march=i486 -mtune=i686 "$OPTIMIZE" -pipe -Wall -Wextra -c "$PROGRAM".c "$SYMBOLS" 
gcc  $CFLAGS -c "$PROGRAM".c 

# Link "$PROGRAM"
#gcc -I. -L. "$PROGRAM".o -o "$PROGRAM" "$SYMBOLS" "$OPTIMIZE" -lX11 -lXfixes -T/usr/local/lib/ldscripts/elf_i386.xbn
gcc $LDFLAGS -I. -L. "$PROGRAM".o -o "$PROGRAM" -lX11 -lXfixes

size "$PROGRAM".o "$PROGRAM"
if [ "$GDEBUG" == "No" ]
then
	sstrip "$PROGRAM"
fi
ls -l "$PROGRAM"

#exit

# Copy program to its final destination.
cp "$PROGRAM" pkg/usr/local/bin
cp "$PROGRAM"-License.txt pkg/usr/local/share/doc/"$PROGRAM"

# Create .tcz, .md5,txt, and .list files for AutoCursor.
mksquashfs pkg $PROGRAM.tcz
md5sum $PROGRAM.tcz > $PROGRAM.tcz.md5.txt
cd pkg
# 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 > ../$PROGRAM.tcz.list
cd ..

# Create dependency file.
for EXT in $PACKAGE_DEPS
do
	echo "$EXT".tcz >> "$PROGRAM".tcz.dep
done

# Create .info file for AutoCursor.
DATE=`date '+%Y/%m/%d'`
VER=`./$PROGRAM | grep version | cut -d ' ' -f2-`
VERSION="v`./$PROGRAM | grep version | cut -d ' ' -f3`"
SIZE="`du -h $PROGRAM.tcz | cut -f1`"
echo "Title:		$PROGRAM.tcz
Description:	Controls cursor visibility based on device events.
Version:	$VERSION
Author:		Richard A. Rost
Original-site:	None
Copying-policy:	GPLv2
Size:		$SIZE
Extension_by:	Rich
Tags:		auto hide mouse cursor pointer
Comments:	Run  $PROGRAM  for this help output:

		$PROGRAM $VER
		Copyright Richard A. Rost Jan 25,2021

		AutoCursor controls cursor visibility based on device
		events, such as mouse, keyboard, touch screen, etc.

		Usage: AutoCursor [-sN] [-hN] [-i] [-e] [-S file] [-A] [-L file]

			-s N    Show cursor when event N occurs.
			-h N    Hide cursor when event N occurs.
			-i      Initial state of cursor is hidden.
			-e      Display list of available events with descriptions.
			-S file Save event settings to file.
			-A      Append instead of overwrite when -S is used.
			-L file Load event settings from file.

		Example: sudo AutoCursor -i -s5 -h1 -h3 -s2 -S ~/.cursor.conf &

		Starts with cursor hidden.
		Shows cursor when events 5 or 2 occur.
		Hides cursor when events 1 or 3 occur.
		Saves the event settings (but not -i) to a file.


		To start automatically next time you boot:
		"echo \"sudo AutoCursor -i -L \~/.cursor.conf \&\" \> \~/.X.d/AutoCursor"

		Event numbers can change between boots by adding or removing a
		mouse for example. So settings get saved by description instead.
		If a mouse was included in your saved settings and is not present
		when you boot, we just move on to the next setting in the file.

		Built with:
		CFLAGS=$CFLAGS
		LDFLAGS=$LDFLAGS

Change-log:	2021/01/30 Original release v0.10
		2021/01/31 v0.20 Fixed search in LoadSettings().
		2021/01/31 v0.30 Added append option. Fixed file permissions.
Current:	$DATE $VERSION Cosmetic fixes to usage message.
" > $PROGRAM.tcz.info

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

# Create archive of the package.
busybox tar -czf "$PROGRAM".tar.gz "$PROGRAM".tc*

# Create archive of the source package.
busybox tar -czf "$PROGRAM"-source-"$VERSION".tar.gz ../"$PROGRAM"/"$PROGRAM".c ../"$PROGRAM"/"$PROGRAM"-License.txt ../"$PROGRAM"/Compile"$PROGRAM"

# Tar the package archive and build script. Include processor type as part of archive name.
busybox tar -czf "$PROGRAM-$PROCESSOR_TYPE".tar.gz *.tar.gz

# bcrypt the final file with the password  tinycore
yes tinycore | bcrypt "$PROGRAM-$PROCESSOR_TYPE".tar.gz

echo "Check $PACKAGEDIR/submitqc.txt for any errors."
echo "Send $PROGRAM-$PROCESSOR_TYPE.tar.gz.bfe to: tcesubmit@gmail.com"
