#!/bin/sh

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

PROGRAM="HideMouse"
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"

# 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 HideMouse.
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 HideMouse.
VERSION=`./$PROGRAM h | grep version | cut -d ' ' -f2-`
SIZE="`du -h $PROGRAM.tcz | cut -f1`"
echo "Title:		$PROGRAM.tcz
Description:	Hide mouse cursor and move it to the screens corner.
Version:	$VERSION
Author:		Richard A. Rost
Original-site:	None
Copying-policy:	GPLv2
Size:		$SIZE
Extension_by:	Rich
Tags:		move hide mouse cursor pointer
Comments:	Run  $PROGRAM h  for this help output:

		$PROGRAM $VERSION
		Copyright Richard A. Rost March 1, 2020
		$PROGRAM is for hiding/moving the mouse cursor.

		First run:  $PROGRAM &

		Then:  echo X > /tmp/$PROGRAM

		Where X is:
				h=Hide cursor  s=Show cursor  q=Quit
				1=Top Left  2=Top Right  3=Bottom Left  4=Bottom Right


		Note: If you need to stop the program, use the Quit option. If
		      you stop it with kill, /tmp/$PROGRAM will need to be
		      removed if you want to restart $PROGRAM. The cursor
		      will become visible if the program exits.

		Built with:
		CFLAGS=$CFLAGS
		LDFLAGS=$LDFLAGS

Change-log:	$DATE Original release $VERSION
Current:	$DATE Original release $VERSION
" > $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-v0.1.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"
