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

# some parts based on slackbuild and LFS
# https://slackbuilds.org/slackbuilds/14.0/system/rox-filer/rox-filer.SlackBuild
# http://www.linuxfromscratch.org/blfs/view/cvs/xsoft/rox-filer.html

export CFLAGS="-mtune=generic -Os -pipe"
export CXX="g++ -flto -fuse-linker-plugin -mtune=generic -Os -pipe \
-fno-rtti -fno-exceptions"
export LDFLAGS="-ldl -lm" 

P=rox-filer
V=2.11
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`

LIST="compiletc submitqc gtk2-dev attr-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/rox/$P-$V.tar.bz2 " $USER 
tar jxvf $P*bz2
cd $SRC/ROX-Filer/
mkdir build
cd build
../src/configure
make -j5

# creates Apprun, R-F stripped and R-F debug unstripped
# clean up and it makes no sense to offer a dev with this type of build
cd ..
rm -rf build src Messages ROX-Filer.dbg
cd /tmp

# create skeletons
#########
mkdir -p \
$P/usr/local/bin \
$P/usr/local/share/applications \
$P/usr/local/share/pixmaps \
$P/usr/local/share/doc/$P \
$P/usr/local/tce.installed \
$P-doc/usr/local/share/ROX-Filer \
$P-doc/usr/local/share/man/man1 

# main misc
################
mv $SRC/ROX-Filer $P/usr/local/share/
cp $P/usr/local/share/ROX-Filer/Help/COPYING $P/usr/local/share/doc/$P/COPYING
PBASE=$P/usr/local/share/ROX-Filer
mkdir -p $PBASE/real-files
mv $PBASE/AppRun $PBASE/real-files/
mv $PBASE/ROX-Filer $PBASE/real-files/
SMIME=$SRC/Choices/MIME-types
PMIME=$P/usr/local/etc/rox/xdg/rox.sourceforge.net/MIME-types
cp $SMIME/application_postscript $PMIME/application_postscript.new
cp $SMIME/text $PMIME/text.new
cp $SMIME/text_html $PMIME/text_html.new

# doc TCZ
##########
# clicking on help in rox expects special pathway to Help
                                 
mv $P/usr/local/share/ROX-Filer/Help $P-doc/usr/local/share/ROX-Filer/
cp $SRC/rox.1 $P-doc/usr/local/share/man/man1/
cd $P-doc/usr/local/share/man/man1/
ln -s rox.1 rox-filer.1
cd /tmp

# bin 
#####
cat > $P/usr/local/bin/rox << EOF
#!/bin/sh
exec /usr/local/share/ROX-Filer/AppRun "\$@"
EOF
chmod 755 $P/usr/local/bin/rox

# desktop
##########
# create icon
cp $P/usr/local/share/ROX-Filer/.DirIcon $P/usr/local/share/pixmaps/rox.png

cat >> $P/usr/local/share/applications/$P.desktop <<'EOF'
[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=roxFM
Icon=rox
Exec=rox
Categories=GTK;Application;System;
StartupNotify=true
Terminal=false
X-FullPathIcon=/usr/local/share/pixmaps/rox.png
EOF

# tce.installed
#################
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
REAL=/tmp/tcloop/rox-filer/usr/local/share/ROX-Filer/real-files
cp $REAL/AppRun /usr/local/share/ROX-Filer/
cp $REAL/ROX-Filer /usr/local/share/ROX-Filer/
EOF
chown -R tc:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# dependency
##############
echo 'gtk2.tcz
rox-filer-doc.tcz' > $P.tcz.dep

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

# 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
	cd /tmp
done

# create info files let submitqc add size

echo 'Title:          rox-filer.tcz  
Description:    file manager
Version:        2.11
Author:         Thomas Leonard 
Original-site:  https://sourceforge.net/projects/rox
Copying-policy: GPL v2.1
Size:		
Extension_by:   aus9 at gmx dot com
Tags:           rox-filer
Comments:       gtk2 file manager 
                You can safely ignore these errors, if found
                gtkrc-2.0:16: error: scanner: unknown error
                Could not find the icon 'text-plain'

                OPTIONAL: remove the doc from your dep file if un-needed

                Compiled for Corepure64 8.x
Change-log:     2017/09/06  First version 
Current:        2017/09/06
' > $P.tcz.info

echo 'Title:          rox-filer-doc.tcz
Description:    docs 
Version:        2.11
Author:         Thomas Leonard
Original-site:  https://sourceforge.net/projects/rox
Copying-policy: GPL v2.1
Size:		
Extension_by:   aus9 at gmx dot com
Tags:           rox-filer 
Comments:       some docs and manpage
                Help is in a non-standard pathway

                Compiled for Corepure64 8.x
Change-log:     2017/09/06  First version 
Current:        2017/09/06 
' > $P-doc.tcz.info

submitqc  --libs