#!/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
#

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

LSHW="lshw"
GTKLSHW="gtk-lshw"
STARTDIR="$PWD"
PACKAGEDIR="package"
LSHWDIR="$PACKAGEDIR/$LSHW"
GTKLSHWDIR="$PACKAGEDIR/$GTKLSHW"

DESTDIR="$STARTDIR/$LSHWDIR"
PREFIX="/usr/local"
ZLIB="1"

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="gcc -flto -fuse-linker-plugin -march=i486 -mtune=i686 $OPTIMIZE -pipe"
		CXXFLAGS="g++ -flto -fuse-linker-plugin -march=i486 -mtune=i686 $OPTIMIZE -pipe -fno-exceptions -fno-rtti"
		LDFLAGS="-Wl,-O1"
		;;

		x86_64)
		CFLAGS="gcc -flto -fuse-linker-plugin -mtune=generic $OPTIMIZE -pipe"
		CXXFLAGS="g++ -flto -fuse-linker-plugin -mtune=generic $OPTIMIZE -pipe -fno-exceptions -fno-rtti"
		LDFLAGS="-Wl,-O1"
		;;

		armv*)
		CFLAGS="gcc -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 ZLIB

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

# Set up some destination directories.
busybox mkdir -p "$LSHW"
busybox mkdir -p "$LSHWDIR$PREFIX/share/doc/$LSHW"
busybox mkdir -p "$GTKLSHWDIR$PREFIX/share/doc/$GTKLSHW"

SOURCE="https://github.com/lyonel/$LSHW"

echo "Downloading essential packages..."
tce-load -w compiletc git gtk2-dev libxslt librsvg submitqc
tce-load -i compiletc git gtk2-dev libxslt librsvg submitqc

echo "Cloning source package."
git clone "$SOURCE"

# Copy the copyright files.
cp "$LSHW/COPYING" "$LSHWDIR$PREFIX/share/doc/$LSHW"
cp "$LSHW/COPYING" "$GTKLSHWDIR$PREFIX/share/doc/$GTKLSHW"

# Enter the source tree. Must be before the  VERSION=  statement.
cd $LSHW

DATE=`date '+%Y/%m/%d'`
VERSION=`git describe --tags --long | cut -d - -f 1,2 | tr - .`

# Create a source tarball.
busybox tar -czf "../$LSHW-$VERSION".tar.gz "../$LSHW"

# Comment out 2 lines from the Makefile because they cause errors.
sed -i 's/.*docbook2man \$</#docbook2man \$</' src/Makefile
sed -i 's/\$(PACKAGENAME).1: \$(PACKAGENAME).sgml/#\$(PACKAGENAME).1: \$(PACKAGENAME.sgml)/' src/Makefile

# Change hardcoded  /usr/share/lshw/  paths in the sourcecode to  /usr/local/share/lshw/
for i in `grep -riIl "/usr/share/lshw" --include="*.cc"`
do
	sed -i 's|/usr/share/lshw/|/usr/local/share/lshw/|' "$i"
done

# Make lshw
make
make install

# Strip programs and libraries
cd "$DESTDIR"
if [ "$GDEBUG" == "No" ]
then
	sudo find . | xargs file | grep "executable" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
	sudo find . | xargs file | grep "shared object" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip -g 2> /dev/null
fi

# These don't appear to be used by lshw or gtk-lshw. 1.5 Meg total size.
busybox rm -f $DESTDIR/$PREFIX/share/$LSHW/manuf.txt*
busybox rm -f $DESTDIR/$PREFIX/share/$LSHW/oui.txt*

# Go back to the top of the source directory.
cd "$STARTDIR/$LSHW"

# Adjust the destination directory for the GTK front end.
export DESTDIR="$STARTDIR/$GTKLSHWDIR"

# Make gtk-lshw
make gui
make install-gui

# Strip programs and libraries
cd "$DESTDIR"
if [ "$GDEBUG" == "No" ]
then
	sudo find . | xargs file | grep "executable" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
	sudo find . | xargs file | grep "shared object" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip -g 2> /dev/null
fi


# Create scaled logo.png icon for wbar.
rsvg-convert -z .064 "$DESTDIR/$PREFIX/share/$LSHW/artwork/logo.svg" -o "$DESTDIR/$PREFIX/share/$LSHW/artwork/$GTKLSHW.png"

# Fix .desktop file. Change /usr/ to /usr/local/ , exec=/usr to exec=sudo /usr , bin to sbin , logo.svg to gtk-lshw.png , and add Icon= line
sed -i 's|Exec=/usr/bin/gtk-lshw|Exec=sudo /usr/local/sbin/gtk-lshw|' "$STARTDIR/$LSHW/src/gui/integration/gtk-lshw.desktop"
sed -i 's|Icon=/usr/share/lshw/artwork/logo.svg|X-FullPathIcon=/usr/local/share/lshw/artwork/gtk-lshw.png|' "$STARTDIR/$LSHW/src/gui/integration/gtk-lshw.desktop"
echo "Icon=$GTKLSHW" >> "$STARTDIR/$LSHW/src/gui/integration/gtk-lshw.desktop"

# Create destination for .desktop file and copy it.
mkdir "$STARTDIR/$GTKLSHWDIR/$PREFIX/share/applications"
cp "$STARTDIR/$LSHW/src/gui/integration/gtk-lshw.desktop" "$STARTDIR/$GTKLSHWDIR/$PREFIX/share/applications"

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

# Create .tcz, .md5.txt, and .list files for lshw.
mksquashfs $LSHW $LSHW.tcz
md5sum $LSHW.tcz > $LSHW.tcz.md5.txt
cd $LSHW
# 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 > ../$LSHW.tcz.list
cd ..

# Create .info file for lshw.
SIZE="`du -h $LSHW.tcz | cut -f1`"
echo "Title:		$LSHW.tcz
Description:	lshw (Hardware Lister)
Version:	$VERSION 
Author:		Lyonel Vincent
Original-site:	http://ezix.org/project/wiki/HardwareLiSter
Copying-policy:	GPLv2
Size:		$SIZE
Extension_by:	andriscom
Tags:		hardware scanner lister
Comments:	lshw (Hardware Lister) is a small tool to provide detailed
		information on the hardware configuration of the machine.
		It can report exact memory configuration, firmware version,
		mainboard configuration, CPU version and speed, cache
		configuration, bus speed, etc.
		Information can be output in plain text, XML or HTML.
		
		This program should be run as root (sudo) to ensure accurate
		and complete output
		
		For a GUI frontend, install $GTKLSHW.tcz

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

		For man entry see:
		/usr/local/share/man/man1/lshw.1

Source:		$SOURCE
Change-log:	2010/10/16 Original release Version: B.02.15
		2020/02/29 Updated to Version: B.02.18.105 (Rich)
		$DATE Changed path for .ids files to /usr/local/share/lshw/ (Rich)
Current:	$DATE Updated to Version: $VERSION (Rich)
" > $LSHW.tcz.info



# Create .tcz, .md5.txt, .dep, and .list files for gtk-lshw.
mksquashfs $GTKLSHW $GTKLSHW.tcz
md5sum $GTKLSHW.tcz > $GTKLSHW.tcz.md5.txt
echo -ne "$LSHW.tcz\ngtk2.tcz\n" > $GTKLSHW.tcz.dep
cd $GTKLSHW
# 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 > ../$GTKLSHW.tcz.list
cd ..

# Create .info file for gtk-lshw.
SIZE="`du -h $GTKLSHW.tcz | cut -f1`"
echo "Title:		$GTKLSHW.tcz
Description:	GTK2 GUI for lshw (Hardware Lister)
Version:	$VERSION 
Author:		Lyonel Vincent
Original-site:	http://ezix.org/project/wiki/HardwareLiSter
Copying-policy:	GPLv2
Size:		$SIZE
Extension_by:	Rich
Tags:		GTK2 GUI hardware scanner lister
Comments:	lshw (Hardware Lister) is a small tool to provide detailed
		information on the hardware configuration of the machine.
		It can report exact memory configuration, firmware version,
		mainboard configuration, CPU version and speed, cache
		configuration, bus speed, etc.
		Information can be output in plain text, XML or HTML.
		
		This program should be run as root (sudo) to ensure accurate
		and complete output

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

		For man entry see:
		/usr/local/share/man/man1/lshw.1

Source:		$SOURCE
Change-log:	2020/02/29 Original release Version: B.02.18.105
		$DATE Changed path for .ids files to /usr/local/share/lshw/ (Rich)
Current:	$DATE Updated to Version: $VERSION (Rich)
" > $GTKLSHW.tcz.info

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

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

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

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

cd "$STARTDIR"
