#!/bin/sh

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

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

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

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 $DEFINES -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 $DEFINES -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 $DEFINES -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 $DEFINES -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, filter out success messages.
tce-load -w "$TOOLCHAIN $BUILD_DEPS" 2>&1 | grep -v "already downloaded"

# Install packages required for compiling, filter out success messages.
tce-load -i "$TOOLCHAIN $BUILD_DEPS" 2>&1 | grep -v "already installed"

# 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

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

#exit

# Remove output from previous build.
rm "$PROCESSOR_TYPE"
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 "$PROCESSOR_TYPE"
mkdir pkg/usr/local/bin
mkdir pkg/usr/local/share/doc/"$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 Resizer.
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 Resizer.
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:	Control starting window size/pos of program.
Version:	$VERSION
Author:		Richard A. Rost
Original-site:	None
Copying-policy:	GPLv2
Size:		$SIZE
Extension_by:	Rich
Tags:		window move resize
Comments:	Run  $PROGRAM  for this help screen:

		$PROGRAM $VER
		Copyright Richard A. Rost July 2,2021

		Resizer starts a program and then resizes and moves its
		window based on the options passed in.

		Usage:

		  Resizer -p "program args" [-x N] [-y N] [-w N] [-h N] [-f] [-d N] [-b] [-D]

			-p program plus arguments in quotes
			-x X position  -y Y position  -w Width  -h Height
			-f Fullscreen
			-d Delay N seconds, then move/resize window
			-b No window manager present
			-D Display ID and geometry of window under mouse, ^C exits

			Use -d when a program:
				Puts up a splash dialog before its window
				Displays its window and later moves it

			Your screen size is 1920 x 1080


		Built with:
		CFLAGS=$CFLAGS
		LDFLAGS=$LDFLAGS

		Examples:
			Start Midnight Commander in the /etc directory.
			Resizer -p \"xterm -e mc /etc\" -x320 -y320 -w800 -h600

			Start Tinycores default editor extra wide.
			Resizer -p \"editor ~/.profile\" -x 100 -w 1000

			If starting from a terminal, disable stdout and stderr.
			Resizer -p \"claws-mail-start 2>&1 > /dev/null\" -x 0 -y 0

		Notes:
			Programs that insist on displaying a startup dialog before
			displaying the main window need  -d. Wireshark for example.

			Programs that display their main window and then later decide
			to adjust their size/position need  -d. Firefox for example.

			Programs that remember their window settings or have a geometry
			option don't really need Resizer, though you can still use it.

			Resizer starts your command in the background automatically.
			Do NOT append an & to your command. Do NOT background Resizer.

			If you are running X without a window manager (kiosk) then
			use the  -b  option to make Resizer aware of that.

Change-log:	2021/07/08 Original release v0.10
Current:	$DATE $VERSION Added -f and -D options.
" > $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

# Save copies in the  $PROCESSOR_TYPE  directory in case we want
# to compile for another architecture.
cp "$PROGRAM" "$PROCESSOR_TYPE"
cp "$PROGRAM".tc* "$PROCESSOR_TYPE"
cp *.tar.gz* "$PROCESSOR_TYPE"
cp submitqc.txt "$PROCESSOR_TYPE"


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