#!/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 \
pcre21042-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
# clean out any pngs so we only inject our mc pngs
rm -rf *.png
/usr/local/bin/wget -nc --no-check-certificate \
https://github.com/MidnightCommander/$P/archive/refs/tags/$V.tar.gz \
http://tinycorelinux.net/15.x/x86_64/tcz/src/$P/$P.png 
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 
# desktop - icon
################
mkdir -p $P/usr/local/share/pixmaps
mv mc.png $P/usr/local/share/pixmaps
# desktop-file-validate has been run against both desktop and simplified

mkdir -p $P/usr/local/share/applications $P/usr/local/share/$P/files
echo '[Desktop Entry]
Type=Application
Name=mc
Exec=/home/tc/.local/bin/mc-fix.sh
Icon=mc
X-FullPathIcon=/usr/local/share/pixmaps/mc.png  ' > $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
DIR=$P/usr/local/libexec/mc/ext.d
mv $DIR $P/usr/local/share/mc

# create file for desktop
echo '#!/bin/sh
export LANG=C && aterm -fn 12x24 -e mc ' > $P/usr/local/share/$P/files/mc-fix.sh
chmod 755 $P/usr/local/share/$P/files/mc-fix.sh

# 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=/usr/local/share/mc/ext.d
DIR3=/usr/local/share/applications
USER=`cat /etc/sysconfig/tcuser`  # user is now defined so below works
DIR4=/home/$USER/.local/bin
FILE4=$DIR4/mc-fix.sh
SRC4=/usr/local/share/mc/files/mc-fix.sh
# read-write mc files
#######################
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

# home bin file
############### Please read info file 
[ -f $FILE4 ] || cp $SRC4 $DIR4
chown $USER:staff $FILE4
chmod 755 $FILE4
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
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)
Current:        2025/11/19 v 4.8.33 on 16x new upstream site + aterm    ' > $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)
Current:        2025/11/19 v 4.8.33 on 16x new upstream site + aterm' > $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:            896K
Extension_by:    aus9 @linuxquestions.org
Tags:            tty console terminal file manager
Comments:        Arrow up/down works by default for aterm, lxterminal, sakura, tilda and urxvt (TCE=rxvt)
                 Other terms not tested. 
                 
                 aterm users
                 ############
                 If you are NOT using wbar NOR file manager to launch mc desktop file, 
                 then (1) launch aterm with your font size (eg) $ aterm -fn 12x24 &
                 (2) $ export LANG=C && mc 
                 For those who like kb shortcuts, link a key to command=mc-fix.sh
                 mc-fix.sh will appear in your $HOME/.local/bin and it is SAFE to
                 ignore "tc" but you may want to decrease font size as the line reads
                 "export LANG=C && aterm -fn 12x24 -e mc"
                 
                 aterm is not UTF8 capable so if you want UTF8 support search for one in Apps 
                 tags=terminal. If I delete LANG=C I get garbled display!
                 If you are using wbar or clicking desktop file ...the font size is determined by
                 your mc-fix.sh contents. If you use apps GUI, you will need aterm for downloads
                 otherwise run $ tce-load -w TCE-name                                                           
                 
                 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 has read-write file association files under /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               
                                 
                 mouse is supported this build for virtual terminal users. 
                            
 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)
Current:         2025/11/19 v 4.8.33 on 16x new upstream site + aterm multiple changes  ' > $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 
aterm.tcz ' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync


