#!/bin/sh
# Original script by andriscom found here:
# http://repo.tinycorelinux.net/3.x/tcz/src/lshw/
# 
# Modified Feb. 28, 2020 by Rich
# Added gtk-lshw, desktop file fixes, removed unused
# data files, various other enhancements.  Rich
#
# Mar. 26, 2020  Adapted for building gpicview.  Rich
#

echo "Don't forget to update this script with the change-log!
Press ENTER to start, Ctrl-C to abort"
read

DATE=`date '+%Y/%m/%d'`

GPICVIEW="gpicview"
GPICVIEWLOCALE="$GPICVIEW-locale"
VERSION="0.2.5"
STARTDIR="$PWD"
PACKAGEDIR="package"
GPICVIEWDIR="$PACKAGEDIR/$GPICVIEW"
GPICVIEWLOCALEDIR="$PACKAGEDIR/$GPICVIEWLOCALE"
DESTDIR="$STARTDIR/$GPICVIEWDIR"
PREFIX="/usr/local"

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

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

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

export DESTDIR PREFIX CFLAGS CXXFLAGS LDFLAGS
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

# Remove previous source and build files.
busybox rm -rf "$GPICVIEW"
busybox rm -rf "$GPICVIEW-$VERSION"
busybox rm -rf "$PACKAGEDIR"

# Set up some destination directories.
busybox mkdir -p "$DESTDIR$PREFIX/share/doc/$GPICVIEW"
busybox mkdir -p "$DESTDIR$PREFIX/share/pixmaps"
busybox mkdir -p "$STARTDIR/$GPICVIEWLOCALEDIR$PREFIX/share"

SOURCE="https://downloads.sourceforge.net/lxde/$GPICVIEW-$VERSION.tar.xz"

echo "Downloading essential packages..."
BUILD_DEPS="compiletc gtk2-dev intltool sstrip submitqc"
tce-load -w $BUILD_DEPS
tce-load -i $BUILD_DEPS

echo "Fetching $SOURCE."
[ ! -e "$GPICVIEW-$VERSION.tar.xz" ] && wget "$SOURCE"
echo "Unpacking $GPICVIEW-$VERSION.tar.xz."
tar xf "$GPICVIEW-$VERSION.tar.xz"

# Create a link without the version number to the source directory.
busybox ln -s $GPICVIEW-$VERSION $GPICVIEW

# Enter the source tree.
cd $GPICVIEW

# Configure gpicview.
./configure --prefix=$PREFIX --disable-static

# Make gpicview.
make

# Install gpicview to $DESTDIR.
make install


cd "$STARTDIR"


# Strip program.
if [ "$GDEBUG" == "No" ]
then
	sudo find $DESTDIR | xargs file | grep "executable" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs sstrip 2> /dev/null
fi

# Copy the copyright file.
cp "$STARTDIR/$GPICVIEW/COPYING" "$DESTDIR$PREFIX/share/doc/$GPICVIEW"

# Fix .desktop file. Add X-FullPathIcon= line
sed -i 's|Icon=gpicview|Icon=gpicview\nX-FullPathIcon=/usr/local/share/pixmaps/gpicview.png|' "$DESTDIR$PREFIX/share/applications/gpicview.desktop"

# Move the icon where Tinycore can find it. Remove the now empty icons directory.
mv $DESTDIR$PREFIX/share/icons/hicolor/48x48/apps/gpicview.png $DESTDIR$PREFIX/share/pixmaps/
rm -rf $DESTDIR$PREFIX/share/icons

# Separate the locale files for their own extension.
mv "$DESTDIR$PREFIX/share/locale" "$STARTDIR/$GPICVIEWLOCALEDIR$PREFIX/share"

# This is where the destination subdirectories are.
cd "$STARTDIR/$PACKAGEDIR"

# Create .tcz, .md5.txt, .dep, and .list files for gpicview.
mksquashfs $GPICVIEW $GPICVIEW.tcz
md5sum $GPICVIEW.tcz > $GPICVIEW.tcz.md5.txt
echo -ne "gtk2.tcz\n" > $GPICVIEW.tcz.dep
cd $GPICVIEW
# 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 > ../$GPICVIEW.tcz.list
cd ..

# Create .info file for gpicview.
SIZE="`du -h $GPICVIEW.tcz | cut -f1`"
echo "Title:		$GPICVIEW.tcz
Description:	A Simple and Fast GTK2 based Image Viewer
Version:	$VERSION 
Author:		Hon Jen Yee (PCMan) and others
Original-site:	http://lxde.sourceforge.net/gpicview/
Copying-policy:	GPLv2
Size:		$SIZE
Extension_by:	Rich
Tags:		image picture viewer
Comments:	Extremely lightweight and fast with low memory usage
		Very suitable for default image viewer of desktop system
		Simple and intuitive interface
		Minimal lib dependency: Only pure GTK+ is used
		Desktop independent: Doesn't require any specific desktop environment
		
		Built with:
		CFLAGS=$CFLAGS
		CXXFLAGS=$CXXFLAGS
		LDFLAGS=$LDFLAGS
		PREFIX=$PREFIX

Source:		$SOURCE
Change-log:	$DATE Original release Version: $VERSION
Current:	$DATE Original release Version: $VERSION
" > $GPICVIEW.tcz.info

# Create .tcz, .md5.txt, .dep, and .list files for gpicview-locale.
mksquashfs $GPICVIEWLOCALE $GPICVIEWLOCALE.tcz
md5sum $GPICVIEWLOCALE.tcz > $GPICVIEWLOCALE.tcz.md5.txt
echo -ne "$GPICVIEW.tcz\n" > $GPICVIEWLOCALE.tcz.dep
cd $GPICVIEWLOCALE
# 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 > ../$GPICVIEWLOCALE.tcz.list
cd ..

# Create .info file for gpicview-locale.
SIZE="`du -h $GPICVIEWLOCALE.tcz | cut -f1`"
echo "Title:		$GPICVIEWLOCALE.tcz
Description:	A Simple and Fast GTK2 based Image Viewer
Version:	$VERSION 
Author:		Hon Jen Yee (PCMan) and others
Original-site:	http://lxde.sourceforge.net/gpicview/
Copying-policy:	GPLv2
Size:		$SIZE
Extension_by:	Rich
Tags:		image picture viewer
Comments:	Language files
		
Source:		$SOURCE
Change-log:	$DATE Original release Version: $VERSION
Current:	$DATE Original release Version: $VERSION
" > $GPICVIEWLOCALE.tcz.info

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

# Create archives of the 2 packages.
busybox tar -czf "$GPICVIEW".tar.gz "$GPICVIEW".tc*
busybox tar -czf "$GPICVIEWLOCALE".tar.gz "$GPICVIEWLOCALE".tc*

# Tar the package archives, source package, and build script. Include processor type as part of archive name.
busybox tar -czf "$GPICVIEW-$PROCESSOR_TYPE".tar.gz *.tar.gz "../$GPICVIEW-$VERSION.tar.xz" "../$0"

# bcrypt the final file with the password  tinycore
yes tinycore | bcrypt "$GPICVIEW-$PROCESSOR_TYPE".tar.gz
busybox cp -f "$GPICVIEW-$PROCESSOR_TYPE".tar.gz.bfe ../
echo "Check $PACKAGEDIR/submitqc.txt for any errors."
echo "Send $GPICVIEW-$PROCESSOR_TYPE.tar.gz to: tcesubmit@gmail.com"

cd "$STARTDIR"