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

LIST="compiletc submitqc libfm-dev intltool  lxde-icon-theme"
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/$P/$SRC.tar.xz   "  $USER
mkdir $P
xz -d $P*xz && tar xvf $P*tar
cd $SRC
./configure --sysconfdir=/etc --with-gtk=2
make check  # takes seconds
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
chmod 644 $P-doc/usr/local/share/doc/$P/COPYING

# dev
#####
# remove it from repo
rm -rf $P/usr/local/include

# main
#####
rm -rf $P/usr/local/lib
mkdir -p $P/usr/local/share/pixmaps
ICON=/usr/local/share/icons/nuoveXT2/48x48/apps/system-file-manager.png
cp $ICON $P/usr/local/share/pixmaps/pcmanfm.png

APP=$P/usr/local/share/applications/$P.desktop
echo 'X-FullPathIcon=/usr/local/share/pixmaps/pcmanfm.png' >> $APP

# add config file template
echo '# DO NOT EDIT! This file will be overwritten by LXAppearance.
# Any customization should be done in ~/.gtkrc-2.0.mine instead.
gtk-theme-name="Raleigh"
gtk-icon-theme-name="nuoveXT2"
gtk-font-name="Sans 10"
gtk-cursor-theme-size=0
gtk-toolbar-style=GTK_TOOLBAR_BOTH
gtk-toolbar-icon-size=GTK_ICON_SIZE_LARGE_TOOLBAR
gtk-button-images=1
gtk-menu-images=1
gtk-enable-event-sounds=1
gtk-enable-input-feedback-sounds=1
gtk-xft-antialias=1
gtk-xft-hinting=1
gtk-xft-hintstyle="hintfull"
include .gtkrc-2.0.mine  ' > $P/usr/local/share/$P/.gtkrc-2.0

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

cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
# modified from bmarkus 
LOOP=/tmp/tcloop/pcmanfm/usr/local/share/pcmanfm/.gtkrc-2.0
USER=`cat /etc/sysconfig/tcuser`
GTK=/home/$USER/.gtkrc-2.0
if [ ! -f  $GTK ]  ;  then 
    cp $LOOP /home/$USER
    chown $USER:staff $GTK
    chmod 644 $GTK    
fi
EOF
chown -R tc:staff $P/usr/local/tce.installed
chmod -R 755 $P/usr/local/tce.installed

# TCZ them
###########
LIST="$P  $P-locale $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 'man-db.tcz ' > $P-doc.tcz.dep
echo 'pcmanfm.tcz ' > $P-locale.tcz.dep

echo 'libfm.tcz     
shared-mime-info.tcz    ' > $P.tcz.dep

echo 'Title:          pcmanfm.tcz
Description:    Graphical File Manager 
Version:        1.3.0
Author:         many see -doc/AUTHORS
Original-site:  https://downloads.sourceforge.net/pcmanfm
Copying-policy: GPL v2
Size:           132K          		
Extension_by:   aus9 at gmx dot com
Tags:           pcmanfm      
Comments:        Warning: all partitions may be automounted 
                but no umount in this app.  Use the mount tool 
                to un-mount please.

                Adjust .config/pcmanfm/default/pcmanfm.conf if
                automount does not suit you, etc

                also note you should have a gtk2 config file at
                .gtkrc-2.0

                
                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/10 v1.3.0 (aus9) ' > $P.tcz.info

echo 'Title:          pcmanfm-locale.tcz
Description:    locales for pcmanfm 
Version:        1.3.0
Author:         many see -doc/AUTHORS
Original-site:  https://downloads.sourceforge.net/pcmanfm
Copying-policy: GPL v2
Size:           472K          		
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/10 v1.3.0 (aus9)  ' > $P-locale.tcz.info

echo 'Title:          pcmanfm-doc.tcz
Description:    docs for pcmanfm 
Version:        1.3.0
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/10 v1.3.0 (aus9)  ' > $P-doc.tcz.info

submitqc --libs
