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

export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe -fno-exceptions -fno-rtti"

P=vifm
V=0.13
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc perl5 file-dev libX11-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://prdownloads.sourceforge.net/$P/$SRC.tar.bz2 \
https://i.postimg.cc/3NnK6ZzN/$P.png  " $USER
chown root:root $P.png 
tar jxvf $SRC*bz2
cd $SRC

# header fixes
#################
ln -s /usr/local/include/ncursesw/curses.h /usr/local/include/
ln -s /usr/local/include/ncursesw/term.h /usr/local/include/
sed 's|vifm_cv_curses=/usr|vifm_cv_curses=/usr/local|' -i configure
sed 's|#include "curses.h"||' -i src/compat/curses.c
sed 's|curses.h|/usr/local/include/ncursesw/curses.h|' -i src/compat/curses.c
sed 's|curses.h|/usr/local/include/ncursesw/curses.h|' -i src/compat/curses.h

# configure: WARNING: "Could not find glib-2.0 gtk+-2.0 via pkg-config.  No GTK+ support."
# without groff help file not found borks make install

./configure prefix=/usr/local  --with-libmagic --with-X11 \
--with-curses-name=ncursesw --with-curses=/usr/local/lib
make -j5 # 15 seconds 4 make 30 seconds for configure
make install-strip DESTDIR=/tmp/$P 
cd /tmp

# doc quicker to move the lot fix later
#####
mkdir -p $P-doc/usr/local/share/
mv $P/usr/local/share/doc $P-doc/usr/local/share/
mv $P/usr/local/share/man $P-doc/usr/local/share/
mv $P/usr/local/share/$P/$P-help.txt $P-doc/usr/local/share/doc/$P/

# main
###### we do not use fish
rm -rf $P/usr/local/share/fish

mkdir -p $P/usr/local/share/doc/$P
echo 'GPL v2 for main 
see COPYING.3party for some headers please'  > $P/usr/local/share/doc/$P/COPYING
cp $SRC/COPYING.3party $P/usr/local/share/doc/$P

# desktop
#########
# pixmap too big
rm -rf $P/usr/local/share/pixmaps/$P.png
mv $P.png $P/usr/local/share/pixmaps/
DESK=$P/usr/local/share/applications/$P.desktop
echo 'X-FullPathIcon=/usr/local/share/pixmaps/vifm.png' >> $DESK 

# system wide config changes affect local copy
# we have no sxiv so use feh for some images
FILE=$P/usr/local/share/$P/vifmrc
sed 's|sxiv|feh|g' -i $FILE

# initially I attempted an install script but weirdly until I launched vifm, no config dir was created
# and it was ignoring my install script for copy and read write so leave it to the members

# 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
        sed 's|usr|/usr|g' -i /tmp/$Z.tcz.list
	cd /tmp
done

ls -hal

echo 'Title:           vifm-doc.tcz
Description:     important help files
Version:         0.13
Author:          see AUTHORS
Original-site:   https://vifm.info
Copying-policy:  GPL v2 for main TCE
                 some headers see COPYING.3party 
Size:            436K
Extension_by:    aus9 @ linuxquestions.org
Tags:            console terminal file manager  
Comments:        Please read links or doc files especially if you
                 have never used vi(m) before
                 Note vifm-help.txt is a General commands manual
                 --- has lots of commands  

                 https://wiki.vifm.info/index.php?title=Quickstart_Tutorial
                 https://vifm.info/cheatsheets.shtml

Change-log:     2024/03/28 v 0.13 on 15x 
Current:        2024/03/28                    ' > $P-doc.tcz.info
echo 'man-db.tcz' > $P-doc.tcz.dep

echo 'Title:           vifm.tcz
Description:     console & terminal file manager 
Version:         0.13
Author:          see doc TCE for AUTHORS
Original-site:   https://vifm.info
Copying-policy:  GPL v2 for main TCE
                 some headers see COPYING.3party 
Size:            784K
Extension_by:    aus9 @ linuxquestions.org
Tags:            console terminal file manager  
Comments:        Highly recommend: 
                 https://wiki.vifm.info/index.php?title=Quickstart_Tutorial

                 Normal builtin keys link below
                 https://vifm.info/cheatsheets.shtml
                 Copying to buffer/Selecting dir or file is called 
                 yanking hence yy (yanks), in command mode 
                 its the same as :yank  
                 Normally followed by a delete or paste input 

                 and doc TCE, especially vifm-help.txt

                 Run 2 panes by  $ vifm /dir1 /dir2 # like mc
                 On restart it may remember your last dirs so 
                 :cd /dir3 /dir4 # will change to new dirs 

                 In a virtual terminal, function keys like 
                 F3,4,5,6,8 work as expected except
                 F3 opens vi for inline view (unless configs changed)
                 F4 opens vi for edits and both will need input of 
                 quit view by :q  or save and quit edit by :wq 

                 In Console, Function keys fail (for me) -> use vi commands only
                 I use arrow keys to highlight selection dir or file, and eg
                 :copy (will copy dir or file to other directory )
                 :copy (force over write if exists at other dir)
               
                 You may like to edit the config file?
                 It is not created until you launch vifm. Next
                 $ rm -rf $HOME/.config/vifm/vifmrc 
                 $ cp /tmp/tcloop/vifm/usr/local/share/vifm/vifmrc $HOME/.config/vifm
                 
                 You can use external GUI TCES like leafpad too
                 To completely exit file manager while in normal mode...input  ZZ
                 If you are not in normal mode (command) input Esc(ape key) 
                 :q should also work

                 In normal mode input : puts you into command mode, normally followed by letter(s)

Change-log:     2024/03/28 v 0.13 on 15x 
Current:        2024/03/28                    ' > $P.tcz.info

readelf -d $P/usr/local/bin/vifm | grep 'NEEDED' # ignore TCBs
#libncursesw.so.6]ncursesw
#libmagic.so.1]file

echo 'ncursesw.tcz
file.tcz  ' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync
