#!/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.33
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc automake doxygen gettext-dev glib2-dev \
gpm libssh2-dev libtool-dev perl5 setfont slang-dev   "
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
/usr/local/bin/wget -nc --no-check-certificate \
https://github.com/MidnightCommander/$P/archive/refs/tags/$V.tar.gz 
tar xvf $V*gz
cd $SRC
./autogen.sh
./autogen.sh autoupdate
./configure --prefix=/usr/local --with-gpm-mouse --with-internal-edit --with-screen=slang --without-x --disable-doxygen-doc \
--with-ncurses-includes=/usr/local/include/ncursesw --with-ncurses-libs=/usr/local/lib --with-search-engine=pcre2

# Compiler flags:                  -fdiagnostics-show-option -Wall -Wextra -Wattributes -Wimplicit-function-declaration -Wimplicit-int 
# -Wno-declaration-after-statement -Wno-long-long -Wno-vla -Wincompatible-pointer-types -Wint-conversion -Wbad-function-cast -Wfloat-conversion 
# -Wfloat-equal -Wformat-signedness -Wimplicit-fallthrough -Wmissing-declarations -Wmissing-format-attribute -Wmissing-prototypes 
# -Wmissing-variable-declarations -Wnested-externs -Wpointer-arith -Wredundant-decls -Wshadow -Wstrict-prototypes -Wswitch-default -Wundef 
# -Wunreachable-code -Wunused-result -Wwrite-strings -Wno-cast-function-type -Wno-assign-enum  -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:             no
#  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:                    pcre2
make -j6 # 30s
make install-strip DESTDIR=/tmp/$P
cd /tmp

# doc
######
mkdir -p $P-doc/usr/local/share/$P/help 
mv $P/usr/local/share/man $P-doc/usr/local/share
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 
######
mkdir -p $P/usr/local/share/$P/files $P/usr/local/share/doc/$P
echo 'GPL v3' > $P/usr/local/share/doc/$P/COPYING

DIR=$P/usr/local/libexec/mc/ext.d
mv $DIR $P/usr/local/share/mc/files/

# tce.install
#############
mkdir -p $P/usr/local/tce.installed
echo '#!/bin/sh
DIR1=/usr/local/libexec/mc/ext.d
DIR2=/usr/local/share/mc/files/ext.d

# read-write mc files
#######################
[ -d $DIR1 ] || 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/ ' > $P/usr/local/tce.installed/$P
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# fix some file perms found by submitqc
DIR1=$P/usr/local/libexec/$P
chmod 644 $DIR1/*csh
chmod 644 $DIR1/*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.33
Author:          https://github.com/MidnightCommander/mc/blob/master/AUTHORS
Original-site:   https://github.com/MidnightCommander/mc
Copying-policy:  GPL v3
Size:            964K
Extension_by:    aus9 @linuxquestions.org
Tags:            tty console 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
                 2024/04/05 v 4.8.31 on 15x + mouse doc/locale, rw file associations, remove aterm dep (aus9)
                 2025/11/19 v 4.8.33 on 16x new upstream site + aterm multiple changes
Current:         2025/12/13 strip out aterm dep + desktop + mc-fix.sh on 16x   ' > $P-doc.tcz.info
echo 'man-db.tcz
mc.tcz' > $P-doc.tcz.dep

echo 'Title:           mc-locale.tcz
Description:     locales
Version:         4.8.33
Author:          https://github.com/MidnightCommander/mc/blob/master/AUTHORS
Original-site:   https://github.com/MidnightCommander/mc
Copying-policy:  GPL v3
Size:            1.4M
Extension_by:    aus9 @linuxquestions.org
Tags:            tty console 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
                 2024/04/05 v 4.8.31 on 15x + mouse doc/locale, rw file associations, remove aterm dep (aus9)
                 2025/11/19 v 4.8.33 on 16x new upstream site + aterm multiple changes
Current:         2025/12/13 strip out aterm dep + desktop + mc-fix.sh on 16x           ' > $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.33
Author:          https://github.com/MidnightCommander/mc/blob/master/AUTHORS
Original-site:   https://github.com/MidnightCommander/mc
Copying-policy:  GPL v3
Size:            892K
Extension_by:    aus9 @linuxquestions.org
Tags:            tty console terminal file manager
Comments:        Upgraders NOTE This TCE has been stripped of desktop/wbar/mc-fix.sh
                 If you are a console (tty) user, less is loaded now.
                 If you run a virtual terminal except for aterm or aterm2, it should render correctly
                 If you have locale set with aterm or aterm2, graphics likely to glitch as per image YMMV
                 https://postimg.cc/Nyj8K7LS 
                                                                                   
                 Optional deps:  perl5 for misc. functions
                 No support for trash can
                 On first launch and exit, configs at ~/.config/mc
                 mc allows live config changes, mc-> Options -> Configuration 
                 
                 mc has read-write file association files under /usr/local/libexec/mc/ext.d
                 Any you change and you need over reboot, add to your backup please.
                 eg usr/local/libexec/mc/ext.d/doc.sh               
                                 
                 mouse is supported this build for virtual terminal users.

                 aterm/aterm2 If graphics glitch 
                 #################################
                 (1) edit ~/.Xdefaults to include a line that reads (edit to suit)
                     Aterm*font: 12x24 
                     Or launch aterm with your preferred font size (eg) $ aterm -fn 12x24 &
                 (2) $ export LANG=C && mc

                 On my AMD APU, console or VT sudo mc takes > 5 seconds to load 
                                             
 Change-log:     2014/01/18 v 4.8.11 (bmarkus)
                 2014/06/16 v 4.8.12
                 2024/04/05 v 4.8.31 on 15x + mouse doc/locale, rw file associations, remove aterm dep (aus9)
                 2025/11/19 v 4.8.33 on 16x new upstream site + aterm multiple changes
Current:         2025/12/13 delete aterm dep + desktop + mc-fix.sh on 16x  ' > $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
#[libglib-2.0.so.0]glib2
#[libpcre2-8.so.0] pcre21042 glib2

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

submitqc --libs
rm -rf *.zsync


