#!/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=mc
V=4.8.31
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc slang-dev libssh2-dev libX11-dev doxygen perl5 gettext-dev setfont \
glib2-dev gpm "
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

ln -s /usr/local/include/ncursesw/curses.h /usr/local/include/
ln -s /usr/local/include/ncursesw/term.h /usr/local/include/

cd /tmp
su -c "/usr/local/bin/wget -nc --no-check-certificate \
http://ftp.midnight-commander.org/$SRC.tar.xz  \
http://tinycorelinux.net/5.x/x86_64/tcz/src/$P/$P.png" $USER 
chown root:root $P.png # already 644 perm

tar xvf $SRC*xz
cd $SRC
# force some to build to /usr/local/share
# console test --with-screen=ncurses....is worse slang

./configure --prefix=/usr/local --localedir=/usr/local/share/locale \
--mandir=/usr/local/share/man --docdir=/usr/local/share/doc/$P \
--with-gpm-mouse --with-internal-edit --without-pcre2 --with-x \
--with-search-engine=glib 

# Configuration: not used configure 
# Source code location:           .
# Compiler:                       gcc
# Compiler flags:-fdiagnostics-show-option -Wbad-function-cast -Wcomment -Wdeclaration-after-statement -Wfloat-conversion
#                 -Wfloat-equal -Wformat -Wformat-security -Wformat-signedness -Wimplicit -Wimplicit-fallthrough
#                 -Wignored-qualifiers -Wlogical-not-parentheses -Wmaybe-uninitialized -Wmissing-braces -Wmissing-declarations
#                 -Wmissing-field-initializers -Wmissing-format-attribute -Wmissing-parameter-type -Wmissing-prototypes 
#                 -Wnested-externs -Wno-long-long -Wno-unreachable-code -Wparentheses -Wpointer-arith -Wpointer-sign 
#                 -Wredundant-decls -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wstrict-prototypes -Wswitch 
#                 -Wswitch-default -Wtype-limits -Wundef -Wuninitialized -Wunreachable-code -Wunused-but-set-variable 
#                 -Wunused-function -Wunused-label -Wunused-parameter -Wunused-result -Wunused-value -Wunused-variable 
#                 -Wwrite-strings  -mtune=generic -Os -pipe
# Assertions:                     yes
# Unit tests:                     no ('Check' testing framework not found)
# File system:                    Midnight Commander Virtual Filesystem
#                                 cpio, extfs, shell, ftp, sfs, sftp, tar
# Screen library:                 S-Lang
# Mouse support:                  gpm and xterm  
# X11 events support:             yes
# With subshell support:          yes
# With background operations:     yes
# With ext2fs attributes support: yes
# Internal editor:                yes
# Diff viewer:                    yes
# Support for charset:            yes
# Search type:                    glib-regexp
make -j5 # 1M
make install-strip DESTDIR=/tmp/$P
cd /tmp

# doc
######
mkdir -p $P-doc/usr/local/share/doc/$P
mkdir -p $P-doc/usr/local/share/$P/help

mv $P/usr/local/share/man $P-doc/usr/local/share
cp $SRC/AUTHORS $P-doc/usr/local/share/doc/$P
TARGET=/tmp/$P-doc/usr/local/share/$P/help
cd $P/usr/local/share/$P/help
mv *es *hu *it *pl *ru *sr $TARGET
cd /tmp 
mv $P/usr/local/share/$P/examples $P-doc/usr/local/share/$P
# simpler to move all hints then move EN back
mv $P/usr/local/share/$P/hints $P-doc/usr/local/share/$P
mkdir -p $P/usr/local/share/$P/hints
mv $P-doc/usr/local/share/$P/hints/mc.hint $P/usr/local/share/$P/hints/
mv $P-doc/usr/local/share/$P/hints/mc.hint.en_GB $P/usr/local/share/$P/hints/

# locale 
######
mkdir -p $P-locale/usr/local/share
mv $P/usr/local/share/locale $P-locale/usr/local/share

# main 
# desktop - icon
################
mkdir -p $P/usr/local/share/pixmaps
mv $P.png $P/usr/local/share/pixmaps

mkdir -p $P/usr/local/share/applications
echo '[Desktop Entry]
Name=mc
Exec=xterm -e mc
Icon=mc
Terminal=false
X-FullPathIcon=/usr/local/share/pixmaps/mc.png
Type=Application
Categories=Shells;System ' > $P/usr/local/share/applications/$P.desktop

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

# making read-write completed by install script
DIR1=$P/usr/local/libexec/mc/ext.d
mv $DIR1 $P/usr/local/share/mc

# tce.install
#############
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
DIR1=/usr/local/libexec/mc/ext.d
DIR2=/tmp/tcloop/mc/usr/local/share/mc/ext.d
# members may have backups so check and copy if not found
mkdir -p $DIR1
[ -f $DIR1/archive.sh ] || cp $DIR2/archive.sh $DIR1
[ -f $DIR1/doc.sh ]     || cp $DIR2/doc.sh $DIR1
[ -f $DIR1/image.sh ]   || cp $DIR2/image.sh $DIR1
[ -f $DIR1/misc.sh ]    || cp $DIR2/misc.sh $DIR1
[ -f $DIR1/package.sh ] || cp $DIR2/package.sh $DIR1
[ -f $DIR1/sound.sh ]   || cp $DIR2/sound.sh $DIR1
[ -f $DIR1/text.sh ]    || cp $DIR2/text.sh $DIR1
[ -f $DIR1/video.sh ]   || cp $DIR2/video.sh $DIR1
[ -f $DIR1/web.sh ]     || cp $DIR2/web.sh $DIR1
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# fix some file perms found by submitqc
DIR3=$P/usr/local/libexec/$P
chmod 644 $DIR3/*csh
chmod 644 $DIR3/*sh

# TCZ them
###########
LIST2="$P $P-doc $P-locale "
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:           mc-doc.tcz
Description:     man pages & non-EN help
Version:         4.8.31
Author:          see AUTHORS 
Original-site:   http://midnight-commander.org/
Copying-policy:  GPL v3
Size:            968K
Extension_by:    aus9 @ linuxquestions.org
Tags:            terminal file manager
Comments:        man pages
                 + 6 non-English help files, has dep of mc
                 pathway may work in mc not tested by me
                 + examples + non-EN hints
                                  
Change-log:     2014/01/18 v 4.8.11 (bmarkus)
                2014/06/16 v -> 4.8.12
Current:        2024/04/05 v -> 4.8.31 on 15x (aus9) GPLv3 & files as above     ' > $P-doc.tcz.info
echo 'man-db.tcz
mc.tcz' > $P-doc.tcz.dep

# main has /usr/local/etc/mc/mc.menu.sr left in

echo 'Title:           mc-locale.tcz
Description:     locales
Version:         4.8.31
Author:          see AUTHORS 
Original-site:   http://midnight-commander.org/
Copying-policy:  GPL v3
Size:            1.3M
Extension_by:    aus9 @ linuxquestions.org
Tags:            terminal file manager
Comments:        untested by me
                 doc contains some non-EN man pages 

Change-log:     2014/01/18 v 4.8.11 (bmarkus)
                2014/06/16 v -> 4.8.12
Current:        2024/04/05 v -> 4.8.31 on 15x (aus9) ' > $P-locale.tcz.info
echo 'mc.tcz' > $P-locale.tcz.dep

echo 'Title:           mc.tcz
Description:     Console or Virtual Terminal File Manager
Version:         4.8.31
Author:          see TCE doc 
Original-site:   http://midnight-commander.org/
Copying-policy:  GPL v3
Size:            868K
Extension_by:    aus9 @ linuxquestions.org
Tags:            terminal file manager
Comments:        (UPGRADERS pls note) menu, wbar or desktop icon changes from aterm to xterm 
                 If $ xterm does not launch your terminal and you prefer it over aterm..... 
                 1) Remove aterm from your boot list, add your preferred terminal to bootlist
                    Hint lxterminal has a full xterm function
                 2) Create a sym link as below
                    $ sudo ln -s /usr/local/bin/<name-of-terminal> /usr/local/bin/xterm
                    Add new file to your backup or do it manually each time
                 3) For those using GUI Apps, and xterm fails for your term, load aterm

                 My AMD APU did not allow me to use mc in console mode. This TCE worked in 
                 console mode correctly on a intel gfx so YMMV. If console does not work 
                 please try vifm which works for me both console and VT 

                 If your terminal does not allow arrow up/down navigation. Choose A or B
                 (A) $ export TERM=xterm && mc 
                     aterm, lxterminal, sakura, tilda and urxvt arrow down/up work by default
                     Other terms not tested by me
                 (B) For lazy people like me, run as non-root
                     $ echo export export TERM=xterm >> $HOME/.ashrc
                       This allows allows arrow navigation in all tabs compared to (A)
                 
                 TCE=vt-color-test will prove you will not lose 256 color
                                
                 Optional deps:  perl5 for misc. functions
                 No support for trash can
                 On first launch configs at ~/.config/mc
                 mc allows live config changes, mc-> Options -> Configuration 

                 mc now has read-write file association files under extension dir=
                 /usr/local/libexec/mc/ext.d
                 Any you change and need over reboot, add to your backup please.
                 eg usr/local/libexec/mc/ext.d/doc.sh (we drop leading /)                 
                 Also--examining doc.sh for example reveals that we could use
                 xpdf-tools or poppler23-bin internal view of PDF (pdftotext)
                 and so on. 

                 Please read -doc and -locale TCEs 
                 mouse is supported this build                 
      
Change-log:     2014/01/18 v 4.8.11 (bmarkus)
                2014/06/16 v 4.8.12
Current:        2024/04/05 v 4.8.31 on 15x + mouse doc/locale, rw file associations, remove aterm dep (aus9)
' > $P.tcz.info

readelf -d $P/usr/local/bin/mc | grep 'NEEDED'
######### ulocbin no extras 4 mcview mcedit mcdiff 
#libslang.so.2] slang
#libgpm.so.2] gpm
#[libssh2.so.1]libssh2
#[libssl.so.3]libcrypto.so.3]openssl -> libssh2
#[libgmodule-2.0.so.0][libglib-2.0.so.0]glib2

echo 'slang.tcz
gpm.tcz
libssh2.tcz
glib2.tcz  ' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync
# wbar test OK after I stopped pcmanfm controlling bg

