#!/bin/sh
# test we are root
if [ "$(id -u)" != "0" ]; then
   echo "run as root now exitting"
   exit 1
fi

CFLAGS="-mtune=generic -Os -pipe"
CXX="g++ -flto -fuse-linker-plugin -mtune=generic -Os -pipe -fno-exceptions -fno-rtti"

P=feh
V=2.27.1
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`

# make install-strip below fails so strip manually

# build deps
LIST="compiletc submitqc libX11-dev imlib2-dev curl-dev libexif-dev Xorg-7.7-dev"
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://feh.finalrewind.org/feh-2.27.1.tar.bz2"  $USER

mkdir $P
tar jxvf $SRC*bz2
cd $SRC
make -j5
make install DESTDIR=/tmp/$P
cd /tmp

# doc
##########
mkdir -p $P-doc/usr/local/share
mv $P/usr/local/share/doc $P-doc/usr/local/share/
mv $P/usr/local/share/man $P-doc/usr/local/share/

# main + remove svg files
######
strip --strip-unneeded $P/usr/local/bin/$P
mkdir -p $P/usr/local/share/pixmaps
cp $P/usr/local/share/icons/hicolor/48x48/apps/$P.png $P/usr/local/share/pixmaps
echo 'X-FullPathIcon=/usr/local/share/pixmaps/feh.png' >> $P/usr/local/share/applications/$P.desktop
rm -rf $P/usr/local/share/feh/images/feh.svg
rm -rf $P/usr/local/share/icons/hicolor/scalable

# tce.install
#############
mkdir -p $P/usr/local/tce.installed

cat << EOF >> $P/usr/local/tce.installed/$P 
#!/bin/sh
gtk-update-icon-cache -q -f -t /usr/local/share/icons/hicolor
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# TCZ them
###########
LIST2="$P $P-doc "
for Z in $LIST2
do
	mksquashfs $Z $Z.tcz
	md5sum $Z.tcz > $Z.tcz.md5.txt
	cd $Z
	find usr -not -type d > /tmp/$Z.tcz.list
        sed 's|usr|/usr|g' -i /tmp/$Z.tcz.list
	cd /tmp
done

ls -alh

echo 'man-db.tcz' > $P-doc.tcz.dep

echo 'imlib2.tcz 
libexif.tcz
libXinerama.tcz 
curl.tcz' > $P.tcz.dep

echo 'Title:          feh.tcz
Description:    image viewer 
Version:        2.27.1
Author:         many see doc AUTHORS
Original-site:  http://feh.finalrewind.org/
Copying-policy: MIT
Size:           148K          		
Extension_by:   aus9
Tags:           image viewer
Comments:       x11 image viewer aimed mostly at terminal users
                ----
Change-log:     2015/09/01 first version
                2017/06/19 updated 2.13.1 -> 2.9.0 (juanito)
Current:        2018/09/06 v 2.9.0 -> 2.27.1 (aus9)  ' > $P.tcz.info

echo 'Title:          feh-doc.tcz
Description:    documents 
Version:        2.27.1
Author:         many see AUTHORS
Original-site:  http://feh.finalrewind.org/
Copying-policy: MIT
Size:           80K      		
Extension_by:   aus9
Tags:           image viewer
Comments:       mainly man page

                online man page is here
                https://man.finalrewind.org/1/feh/

Change-log:     2015/09/01 first version
                2017/06/19 updated 2.13.1 -> 2.9.0 (juanito)
Current:        2018/09/06 v 2.9.0 -> 2.27.1 (aus9) ' > $P-doc.tcz.info

submitqc --libs
