#!/bin/sh

alias cp='busybox cp -a'
alias cut='busybox cut'
alias date='busybox date'
alias du='busybox du'
alias mkdir='busybox mkdir -p'
alias rm='busybox rm -rf'
alias sed='busybox sed'
alias tar='busybox tar'

PROGRAM="flit"

STARTDIR="$PWD"
PACKAGEDIR="$STARTDIR/package"
PREFIX="/usr/local"
DESTDIR="$PACKAGEDIR/$PREFIX"
SRCDIR="src"

OPTIMIZE="-Os"
SYMBOLS="-g"

TOOLCHAIN="compiletc sstrip submitqc"
BUILD_DEPS="fltk-1.4-dev"
PACKAGE_DEPS="fltk-1.4"

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

if [ "$GDEBUG" == "Debug" ]
then
# -flto gets removed because it interferes with gdb being able to display
# code listings.
	OPTIMIZE="-g"
fi


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

case "$PROCESSOR_TYPE" in
        i686)
        CFLAGS="-fuse-linker-plugin -march=i486 -mtune=i686 $OPTIMIZE $SYMBOLS -pipe -Wall -Wextra -fno-plt"
        CXXFLAGS="-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="-fuse-linker-plugin -mtune=generic $OPTIMIZE $SYMBOLS -pipe -Wall -Wextra -fno-plt"
        CXXFLAGS="-fuse-linker-plugin -mtune=generic $OPTIMIZE $SYMBOLS -pipe -Wall -Wextra -fno-exceptions -fno-rtti"
        LDFLAGS="-L/usr/X11/lib -lX11 -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, 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"

cd "$STARTDIR/$SRCDIR"

# Build "$PROGRAM"
#gcc  -march=i486 -mtune=i686 "$OPTIMIZE" -pipe -Wall -Wextra -c "$PROGRAM".c "$SYMBOLS" 
#gcc $CXXFLAGS -std=c++03 -Wno-unused-parameter -Wno-missing-field-initializers -c "$PROGRAM".cpp
g++ -mtune=generic "$OPTIMIZE" -pipe -Wall -Wextra -fno-exceptions -fno-rtti -std=c++03 -Wno-unused-parameter -Wno-missing-field-initializers -c flit.cpp


# Link "$PROGRAM"
#gcc -I. -L. "$PROGRAM".o -o "$PROGRAM" "$SYMBOLS" "$OPTIMIZE" -lX11 -lXfixes -T/usr/local/lib/ldscripts/elf_i386.xbn
#gcc $LDFLAGS "$PROGRAM".o -o "$PROGRAM" -L/usr/lib -lfltk -lfltk_images -lfltk_forms -lpng -lstdc++
g++ -Wl,-T/usr/local/lib/ldscripts/elf_x86_64.xbn flit.o -o flit -L/usr/local/lib -lfltk -lfltk_images -lfltk_forms -lpng -lX11 -lstdc++

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

#exit

cd "$STARTDIR"

# Remove output from previous build.
rm "$PROCESSOR_TYPE"
rm "$PACKAGEDIR"
rm *.tcz*
rm *.gz
rm *.bfe

# Create the destination directories for packaging.
mkdir "$PROCESSOR_TYPE"
mkdir "$DESTDIR/bin"
mkdir "$DESTDIR/share/applications"
mkdir "$DESTDIR/share/doc/$PROGRAM"
mkdir "$DESTDIR/share/pixmaps"

# Copy program files to their final destination.
cp "$STARTDIR/$SRCDIR/$PROGRAM" "$DESTDIR/bin"
cp "$STARTDIR/$SRCDIR/$PROGRAM.desktop" "$DESTDIR/share/applications"
cp "$STARTDIR/$SRCDIR/$PROGRAM"_"help.htm" "$DESTDIR/share/doc/$PROGRAM"
cp "$STARTDIR/$SRCDIR/$PROGRAM.png" "$DESTDIR/share/pixmaps"

# Update version number and date in the help file.
DATE=`date '+%b %d, %Y'`
VERSION="`grep "#define APP_VER" $STARTDIR/$SRCDIR/flit.cpp | cut -d' ' -f3 | tr -d '"'`"
echo "Version is " ${VERSION}
sed -i "s|.*<p>Version.*|<p>Version $VERSION, $DATE</p>|" "$DESTDIR/share/doc/$PROGRAM/$PROGRAM"_"help.htm"

# Create .tcz, .md5,txt, and .list files.
mksquashfs $PACKAGEDIR $PROGRAM.tcz -noappend -quiet -no-progress
md5sum $PROGRAM.tcz > $PROGRAM.tcz.md5.txt
cd "$PACKAGEDIR"
# 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 "$STARTDIR"

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

# Create .info file
DATE=`date '+%Y/%m/%d'`
SIZE="`du -h $PROGRAM.tcz | cut -f1`"
echo "Title:		$PROGRAM.tcz
Description:	Clock, Sound, WiFi, and Battery system tray.
Version:	$VERSION
Author:		Michael A. Losh
Original-site:	http://tinycorelinux.com
Copying-policy:	GPLv3
Size:		$SIZE
Extension_by:	Michael A. Losh
Tags:		System Tray, Battery, Sound, WiFi, Clock
Comments:	Small applets as found in many monder computer
            desktop software.  Choose any combination of
            clock, sound control, battery/power monitor,
            and WiFi networking control.
            Dockable in some WM trays,
            Supports ALSA or OSS, 
            Uses ACPI for battery/power status, 
            scalable screen sizes 
-
            To start flit on X start, add to .xsession:
            [ $(which flit) ] && flit &

Change-log:     2009/09/13 Original 
                2009/09/23 Updated to v0.9.8
                2009/10/04 Updated to v1.0
                2010/05/10 Updated to v1.2.0
                2010/06/06 Updated to v1.2.1
                2011/07/23 Updated to v1.3.1
                2019/03/08 Updated to v1.3.2 (Rich)
                2011/07/23 Updated to v1.3.1
                2019/03/08 Updated to v1.3.2 (Rich)
                2023/06/01 v1.4.x - WiFi updates 
                2025/03/16 v1.5.0 - FLTK 1.4, scaling flexibility, updates to power monitor                
" > $PROGRAM.tcz.info

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

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

# Create archive of the source package.
tar -czf "$PROGRAM"-source-"$VERSION".tar.gz --exclude "*.o" ../"$PROGRAM"/"$SRCDIR" ../"$PROGRAM"/compileit

# Tar the package archive and build script. Include processor type as part of archive name.
tar -czf "$PROGRAM-$PROCESSOR_TYPE".tar.gz --exclude "$PROGRAM-$PROCESSOR_TYPE*" *.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 "$SRCDIR/$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"
echo
