#!/bin/sh

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

PROGRAM="PicFormat"
OPTIMIZE="-Os"
SYMBOLS="-g"
#  Add  -DX_DISPLAY_MISSING  or  load  libX11-dev.tcz
DEFINES="-DX_DISPLAY_MISSING"

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

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" `pkg-config --cflags --libs imlib2`

size "$PROGRAM".o "$PROGRAM"
ls -l "$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"s.sh pkg/usr/local/bin
cp "$PROGRAM"-License.txt pkg/usr/local/share/doc/"$PROGRAM"

# Create .tcz, .md5,txt, and .list files for PicFormat.
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 PicFormat.
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:	Simple image manipulations from the command line.
Version:	$VERSION
Author:		Richard A. Rost
Original-site:	None
Copying-policy:	GPLv2
Size:		$SIZE
Extension_by:	Rich
Tags:		image resize rotate change format
Comments:	Run  $PROGRAM  for this help screen:

		$PROGRAM $VER
		Copyright Richard A. Rost Mar 17,2021

		PicFormat resizes, rotates, and changes formats (i.e. .jpg to .png)
		of images.

		Usage: PicFormat [-i FILE] [-o FILE] [-w N] [-h N] [-p N] [-r N]

			-i FILE Input.
			-o FILE Output.
			-w N    Output width.
			-h N    Output height.
			-p N    Percent, 110=10% bigger, 50=1/2 size.
			-r N    Rotate image CW 90, 180, or 270 degrees.

		Notes
			N is an unsigned integer.
			Only -w OR -h OR -p is needed if resizing.
			If rotating 90 or 270, -w and -h are swapped.

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

		Optional dependency:
		
			binutils.tcz or elfutils.tcz to run PicFormats.sh

		PicFormats.sh scans the /usr/local/lib/imlib2/loaders
		directory. It prints which loaders are available and
		whether they can load or save their respective formats.

Change-log:	2021/03/27 Original release v0.10
Current:	$DATE $VERSION Added -p resize option.
" > $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"s.sh ../"$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"
