#!/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=libfm
V=1.3.1
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`

LIST="compiletc submitqc menu-cache-dev gtk2-dev pango-dev cairo-dev libexif-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://downloads.sourceforge.net/pcmanfm/$SRC.tar.xz   "  $USER
mkdir $P
xz -d $P*xz && tar xvf $P*tar
cd $SRC
./configure --enable-static=no  --sysconfdir=/etc --disable-gtk-doc
##################################
# had seen errors in trying usr loc etc so stayed with etc
# prefix:            /usr/local
# sysconfdir:   /etc
# Enable compiler flags and other support for debugging:  no
# Build udisks support (Linux only, experimental):              no
# Build with libexif for faster thumbnail loading:                   yes
# Build demo program src/demo/libfm-demo:                     no
# Build with custom actions support (requires Vala):         yes
# Large file support:                                                              yes
# GIO module for preferred apps (for glib < 2.28 only):     not required
# Build libfm-gtk for Gtk+ version:                                          2.0
# Build API doc with gtk-doc (recommended for make dist): no
####################################################
# ignored my disable static
make check  # 30 secs
make install-strip DESTDIR=/tmp/$P
cd /tmp

# locale
#########
mkdir -p $P-locale/usr/local/share
mv $P/usr/local/share/locale $P-locale/usr/local/share

# doc
#####
mkdir -p $P-doc/usr/local/share/doc/$P
mv $P/usr/local/share/man $P-doc/usr/local/share/
cp $SRC/AUTHORS $P-doc/usr/local/share/doc/$P
cp $SRC/COPYING $P-doc/usr/local/share/doc/$P
####################################################
# as libfm-extra has some duplication destroy all and create
# structure based only on libfm (full)
####################################################

# dev
#####
mkdir -p $P-dev/usr/local/lib
mv $P/usr/local/include $P-dev/usr/local/
rm -rf $P-dev/usr/local/include/libfm

mv $P/usr/local/lib/pkgconfig $P-dev/usr/local/lib/
mv $P/usr/local/lib/*a $P-dev/usr/local/lib/
chmod 644 $P-dev/usr/local/lib/*a 

# main
#####
# main loads before dev so 

mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
INC=/usr/local/include
LIB=/usr/local/lib
# due to libfm-extra syn links
# removes all dev and lib files 
rm -rf $INC/libfm
rm -rf $INC/libfm-1.0
rm -rf $LIB/pkgconfig/libfm-extra.pc
rm -rf $LIB/libfm*

# now create dirs and populate them
LOOP=/tmp/tcloop/libfm/usr/local/lib
[ -d /usr/local/lib/libfm ] || mkdir -p /usr/local/lib/libfm
ln -s $LOOP/libfm/modules  /usr/local/lib/libfm/
ln -s $LOOP/*so* /usr/local/lib/
EOF
chown -R tc:staff $P/usr/local/tce.installed
chmod -R 755 $P/usr/local/tce.installed

# tce.install  dev
#############
mkdir -p $P-dev/usr/local/tce.installed
cat >> $P-dev/usr/local/tce.installed/$P-dev <<'EOF'
#!/bin/sh
# libfm destroyed libfm-extra and created some links
INC=/usr/local/include
LIB=/usr/local/lib
LOOP=/tmp/tcloop/libfm-dev/usr/local

ln -s $LOOP/pkgconfig/* $LIB/pkgconfig/
ln -s $LOOP/include/libfm-1.0 $INC/
ln -s $LOOP/include/libfm-1.0 $INC/libfm
ln -s $LOOP/lib/*la $LIB/
EOF
chown -R tc:staff $P-dev/usr/local/tce.installed
chmod -R 755 $P-dev/usr/local/tce.installed

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

ls -hal

echo 'libfm.tcz
menu-cache-dev.tcz 
gtk2-dev.tcz 
pango-dev.tcz 
cairo-dev.tcz 
libexif-dev.tcz   '> $P-dev.tcz.dep

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

echo 'gtk2.tcz
menu-cache.tcz
libexif.tcz ' > $P.tcz.dep

echo 'Title:          libfm.tcz
Description:    Library File Manager for pcmanfm 
Version:        1.2.5
Author:         many see -doc/AUTHORS
Original-site:  https://downloads.sourceforge.net/pcmanfm
Copying-policy: GPL v2
Size:           416K          		
Extension_by:   aus9
Tags:           pcmanfm      
Comments:       libfm required an interim build of libfm-extra
                as a building dep before this package could be built.
                
                compiled for 10x
Change-log:     2014/01/18 First version, 1.1.2.2 (bmarkus)
                2015/08/20 v1.2.3 (gordon64)
Current:        2019/02/11 v1.2.5 (aus9) ' > $P.tcz.info

echo 'Title:          libfm-locale.tcz
Description:    locales for libfm 
Version:        1.2.5
Author:         many see -doc/AUTHORS
Original-site:  https://downloads.sourceforge.net/pcmanfm
Copying-policy: GPL v2
Size:           580K          		
Extension_by:   aus9
Tags:           locale   pcmanfm   
Comments:       locales untested by me
                
                compiled for 10x
Change-log:     2014/01/18 First version, 1.1.2.2 (bmarkus)
                2015/08/20 v1.2.3 (gordon64)
Current:        2019/02/11 v1.2.5 (aus9)  ' > $P-locale.tcz.info

echo 'Title:          libfm-dev.tcz
Description:    devs for libfm 
Version:        1.2.5
Author:         many see -doc/AUTHORS
Original-site:  https://downloads.sourceforge.net/pcmanfm
Copying-policy: GPL v2
Size:           88K          		
Extension_by:   aus9
Tags:           pcmanfm     
Comments:       Development files
                
                compiled for 10x
Change-log:     2014/01/18 First version, 1.1.2.2 (bmarkus)
                2015/08/20 v1.2.3 (gordon64)
Current:        2019/02/11 v1.2.5 (aus9)  ' > $P-dev.tcz.info

echo 'Title:          libfm-doc.tcz
Description:    docs for libfm 
Version:        1.2.5
Author:         many see -doc/AUTHORS
Original-site:  https://downloads.sourceforge.net/pcmanfm
Copying-policy: GPL v2
Size:           12K          		
Extension_by:   aus9
Tags:           pcmanfm     
Comments:       Mainly man pages
                
                compiled for 10x
Change-log:     2014/01/18 First version, 1.1.2.2 (bmarkus)
                2015/08/20 v1.2.3 (gordon64)
Current:        2019/02/11 v1.2.5 (aus9)  ' > $P-doc.tcz.info

submitqc --libs
