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

# build order is libfm-extra-dev menu-cache libfm then pcmanfm
export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe -fno-exceptions -fno-rtti"

P=libfm
V=1.3.2
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`

LIST="compiletc submitqc menu-cache-dev libexif-dev gtk2-dev adwaita-icon-theme"
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

# build 1 wants /etc/ not /usr/local/etc
cd /tmp
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://downloads.sourceforge.net/pcmanfm/$SRC.tar.xz   "  $USER
xz -d *xz && tar xvf $SRC.tar
cd $SRC
# compile requests etc for configs
./configure --prefix=/usr/local --sysconfdir=/etc --enable-static=no \
--with-gtk=2 --disable-old-actions 
##################################  
# 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):     no
# 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
####################################################  
make check  # about 1M
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

# no doc
#####
rm -rf $P/usr/local/share/man 

# 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 

##################
# dev tce.installed to avoid libfm-extra-dev duplication
###################
mkdir -p $P-dev/usr/local/tce.installed
cat >> $P-dev/usr/local/tce.installed/$P-dev <<'EOF'
#!/bin/sh
# Due to libfm-extra duplications removes all libfm* files 
# Link to libfm and not libfm-extra, libfm is loaded 
# before libfm-dev so some duplication 
P=libfm
INC=/usr/local/include
LIB=/usr/local/lib
DIR1=/tmp/tcloop/$P-dev/usr/local/lib
DIR2=/tmp/tcloop/$P-dev/usr/local/lib/pkgconfig
DIR3=/tmp/tcloop/$P-dev/usr/local/include
DIR4=/tmp/tcloop/$P/usr/local/lib

rm -rf $INC/$P*
rm -rf $LIB/pkgconfig/$P*
rm -rf $LIB/$P*la
ln -s $DIR1/*la $LIB/
ln -s $DIR2/*pc $LIB/pkgconfig
ln -s $DIR3/$P-1.0 $INC/
ln -s $DIR3/$P-1.0 $INC/$P
rm -rf $LIB/$P*so*
ln -s $DIR4/$P*so* $LIB/
EOF
chown -R root:staff $P-dev/usr/local/tce.installed
chmod -R 755 $P-dev/usr/local/tce.installed

# main    
#######
# fix desktops
#############  
APPS=$P/usr/local/share/applications
sed 's|X-LXDE-Settings;||' -i $APPS/$P*desktop
sed 's|OnlyShowIn=LXDE|NoDisplay=true|' -i $APPS/$P*desktop
# sed is adding remmants...failing but not there on tcz creation
rm -rf $P/usr/local/share/applications/sed*

rm -rf $APPS/lxshortcut.desktop
rm -rf $P/usr/local/bin/lxshortcut

# I need to change libfm.conf to affect MAINLY trash network
EXL=$P/etc/xdg/$P/$P.conf
rm -rf $EXL

echo '# Change in pcmanfm -> Edit -> Preferences 
# Any changes you make will edit ~/.config/libfm/libfm.conf

[config]
single_click=1
use_trash=0
confirm_del=1
confirm_trash=0
quick_exec=1
thumbnail_local=1
thumbnail_max=2048

[ui]
big_icon_size=48
small_icon_size=24
thumbnail_size=128
pane_icon_size=24
show_thumbnail=1

[places]
places_home=1
places_desktop=1
places_root=1
places_computer=0
places_trash=0
places_applications=0
places_network=0
places_unmounted=0' > $EXL

# tce.installed
################
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
# avoid libfm-extra-dev duplication 
P=libfm
LIB=/usr/local/lib
LOOP=/tmp/tcloop/$P/usr/local/lib
rm -rf $LIB/$P*so*
ln -s $LOOP/$P*so* $LIB/
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 755 $P/usr/local/tce.installed

mkdir -p $P/usr/local/share/doc/$P
echo 'GPL v2' > $P/usr/local/share/doc/$P/COPYING

# TCZ them
###########
LIST="$P $P-locale $P-dev "
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 'Title:          libfm-dev.tcz
Description:    devs for libfm 
Version:        1.3.2
Author:         https://github.com/lxde/libfm/blob/master/AUTHORS
Original-site:  tps://downloads.sourceforge.net/pcmanfm
Copying-policy: GPL v2
Size:           88K
Extension_by:   aus9
Tags:           pcmanfm     
Comments:       Dev files
                
Change-log:     2014/01/18 First version, 1.1.2.2 (bmarkus)
                2015/08/20 v1.2.3 (gordon64)
                2019/02/11 v1.2.5 (aus9) for 10x
                2020/07/19 v1.3.1 for 11x modify install script
Current:        2023/03/26 -> 1.3.2 on 14x   ' > $P-dev.tcz.info

echo 'Title:          libfm-locale.tcz
Description:    locales for libfm 
Version:        1.3.2
Author:         https://github.com/lxde/libfm/blob/master/AUTHORS
Original-site:  https://downloads.sourceforge.net/pcmanfm
Copying-policy: GPL v2
Size:           596K
Extension_by:   aus9
Tags:           locale   pcmanfm   
Comments:       locales untested by me
  
Change-log:     2014/01/18 First version, 1.1.2.2 (bmarkus)
                2015/08/20 v1.2.3 (gordon64)
                2019/02/11 v1.2.5 (aus9) for 10x
                2020/07/19 v1.3.1 for 11x modify install script
Current:        2023/03/26 -> 1.3.2 on 14x   ' > $P-locale.tcz.info

echo 'Title:          libfm.tcz
Description:    Library File Manager for pcmanfm 
Version:        1.3.2
Author:         https://github.com/lxde/libfm/blob/master/AUTHORS
Original-site:  https://downloads.sourceforge.net/pcmanfm
Copying-policy: GPL v2
Size:           368K
Extension_by:   aus9
Tags:           pcmanfm      
Comments:       Config will auto-create at ~/.config/libfm/libfm.conf
                Original /etc/xdg/libfm/libfm.conf

                gvfs and trash can support disabled. pcmanfm has  
                the setting but do not try to use trash please.

Change-log:     2014/01/18 First version, 1.1.2.2 (bmarkus)
                2015/08/20 v1.2.3 (gordon64)
                2019/02/11 v1.2.5 (aus9) for 10x
                2020/07/19 v1.3.1 for 11x modify install script
Current:        2023/03/26 -> 1.3.2 on 14x    ' > $P.tcz.info

##################################
readelf -d $P/usr/local/lib/*.so | grep 'NEEDED'
###################################  delete any duplicates
# 0x0000000000000001 (NEEDED) Shared library: [libgio-2.0.so.0]glib2 -> gtk2
# 0x0000000000000001 (NEEDED) Shared library: [libgthread-2.0.so.0]  "
# 0x0000000000000001 (NEEDED) Shared library: [libgobject-2.0.so.0]  "
# 0x0000000000000001 (NEEDED) Shared library: [libglib-2.0.so.0]     "
# 0x0000000000000001 (NEEDED) Shared library: [libm.so.6] TCB
# 0x0000000000000001 (NEEDED) Shared library: [libc.so.6] TCB
# 0x0000000000000001 (NEEDED) Shared library: [libgtk-x11-2.0.so.0] gtk2
# 0x0000000000000001 (NEEDED) Shared library: [libgdk-x11-2.0.so.0]  "
# 0x0000000000000001 (NEEDED) Shared library: [libpangocairo-1.0.so.0]pango      gtk2
# 0x0000000000000001 (NEEDED) Shared library: [libatk-1.0.so.0]   atk              "
# 0x0000000000000001 (NEEDED) Shared library: [libgdk_pixbuf-2.0.so.0]gdk-pixbuf2   "  
# 0x0000000000000001 (NEEDED) Shared library: [libpangoft2-1.0.so.0] pango          "   
# 0x0000000000000001 (NEEDED) Shared library: [libfontconfig.so.1] fontconfig       "
# 0x0000000000000001 (NEEDED) Shared library: [libfreetype.so.6] freetype           "
# 0x0000000000000001 (NEEDED) Shared library: [libpango-1.0.so.0]pango            "
# 0x0000000000000001 (NEEDED) Shared library: [libharfbuzz.so.0] harfbuzz         "
# 0x0000000000000001 (NEEDED) Shared library: [libcairo.so.2] cairo               "
# 0x0000000000000001 (NEEDED) Shared library: [libmenu-cache.so.3] menu-cache  
# 0x0000000000000001 (NEEDED) Shared library: [libfm.so.4]   THIS tce        
# 0x0000000000000001 (NEEDED) Shared library: [libexif.so.12]libexif
##################################################
readelf -d $P/usr/local/bin/* | grep 'NEEDED'
##################################
# no extra to above

echo 'libfm.tcz 
menu-cache-dev.tcz 
gtk2-dev.tcz 
libexif-dev.tcz
adwaita-icon-theme.tcz '> $P-dev.tcz.dep

echo 'libfm.tcz ' > $P-locale.tcz.dep

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

submitqc --libs

# on icewm   libfm-pref-apps   GUI opens ok
