#!/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"
export LDFLAGS="-Wl,-O1"

P=pcmanfm
V=1.3.1
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`

LIST="compiletc submitqc libfm-dev adwaita-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 --prefix=/usr/local --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

# no dev
#####
rm -rf $P/usr/local/include
rm -rf $P/usr/local/lib

# main
#####
mkdir -p $P/usr/local/share/pixmaps
ICON=/tmp/tcloop/adwaita-icon-theme/usr/local/share/icons/Adwaita/48x48/legacy/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

# gvfs not compiled into libfm so remove its GUI
rm -rf $P/usr/local/share/$P/ui/connect.ui

# create template
##################
mkdir -p $P/usr/local/share/$P
echo 'file:///usr/local/share/applications executable-icons
file:///home/tc/Downloads Downloads
file:///home/tc/.config .config
file:///home/tc/.local .local ' > $P/usr/local/share/$P/.gtk-bookmarks 

# tce.install
#############
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
USER=`cat /etc/sysconfig/tcuser`
LOOP=/tmp/tcloop/pcmanfm/usr/local/share/pcmanfm/.gtk-bookmarks 
HOME=/home/$USER
[ -f $HOME/.gtk-bookmarks ] ||  cp $LOOP $HOME 
chown $USER:staff $HOME/.gtk-bookmarks
chmod 644 $HOME/.gtk-bookmarks
EOF
chown -R root: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 'Title:          pcmanfm-doc.tcz
Description:    docs for pcmanfm 
Version:        1.3.1
Author:         see AUTHORS
Original-site:  https://downloads.sourceforge.net/pcmanfm
Copying-policy: GPL v2
Size:           12K
Extension_by:   aus9
Tags:           file manager     
Comments:       Mainly man pages
                
Change-log:     2014/01/18 First version, 1.1.2.2 (bmarkus)
                2015/08/20 v1.2.3 (gordon64)
                2019/02/10 v1.3.0  for 10x(aus9)
Current:        2020/07/19 v 1.3.1 for 11x 
  ' > $P-doc.tcz.info  

echo 'Title:          pcmanfm-locale.tcz
Description:    locales for pcmanfm 
Version:        1.3.1
Author:         many see doc TCE for AUTHORS
Original-site:  https://downloads.sourceforge.net/pcmanfm
Copying-policy: GPL v2
Size:           472K
Extension_by:   aus9
Tags:           locale file manager  
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/10 v1.3.0  for 10x(aus9)
Current:        2020/07/19 v 1.3.1 for 11x 
  ' > $P-locale.tcz.info

echo 'Title:          pcmanfm.tcz
Description:    Graphical File Manager 
Version:        1.3.1
Author:         many see doc TCE for AUTHORS
Original-site:  https://downloads.sourceforge.net/pcmanfm
Copying-policy: GPL v2
Size:           120K
Extension_by:   aus9 
Tags:           file manager gtk2      
Comments:       Warning: all partitions may be automounted  read only
                but no umount in this app. Use the mount tool to un-mount.

                Config will auto-create at ~/.config/pcmanfm/default/pcmanfm.conf
                Original /etc/xdg/pcmanfm/pcmanfm.conf

                Change by pulldown Edit -> Preferences.
                Where you can change off Single click if you prefer, or others etc
                If pcmanfm run in desktop mode, other preferences can be activated
                see   https://tinyurl.com/y3ouvb94
                            
                Bookmarks at ~/.gtk-bookmarks 
                
                gvfs and trash can support disabled. pcmanfm has trashcan/network 
                settings but do not try to use please.

                Read libfm and update-desktops info please!
     
Change-log:     2014/01/18 First version, 1.1.2.2 (bmarkus)
                2015/08/20 v1.2.3 (gordon64)
                2019/02/10 v1.3.0  for 10x(aus9)
                2020/07/19 v 1.3.1 for 11x change install script
Current:        2020/08/04 add update-desktops to dep and info           
 ' > $P.tcz.info

echo 'man-db.tcz ' > $P-doc.tcz.dep
echo 'pcmanfm.tcz ' > $P-locale.tcz.dep
echo 'libfm.tcz
menu-cache.tcz
libexif.tcz
gtk2.tcz 
update-desktops.tcz ' > $P.tcz.dep

submitqc --libs
