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

CFLAGS="gcc -flto -fuse-linker-plugin -mtune=generic -Os -pipe"
CXX="g++ -flto -fuse-linker-plugin -mtune=generic -Os -pipe -fno-exceptions -fno-rtti" 

P=nano
V=4.0
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`

LIST="compiletc submitqc xz ncursesw-dev groff file-dev sed"
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://www.$P-editor.org/dist/v4/$P-$V.tar.xz   "  $USER

# fixes as nano is not looking for ncursesw
ln -s /usr/local/share/pkgconfig/ncursesw.pc /usr/local/share/pkgconfig/ncurses.pc

xz -d $P*xz && tar xvf $P*tar
cd $SRC
sed 's|@documentencoding UTF-8||' -i doc/nano.texi

./configure  --disable-extra --disable-utf8 --enable-nanorc
make check  # < 1 minute
mkdir -p /tmp/$P
make install-strip DESTDIR=/tmp/$P

# there is NO dev

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

# doc
##########
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/info $P-doc/usr/local/share/
cp $SRC/AUTHORS $P-doc/usr/local/share/doc/$P/
cp $SRC/COPYING $P-doc/usr/local/share/doc/$P/

# main
#####
# missing nanorc as per README
cp $SRC/doc/sample.nanorc $P/usr/local/share/nano/nanorc

# edit to get color by removing comment
sed 's|# include "/usr| include "/usr|' -i $P/usr/local/share/nano/nanorc

# tce.installed
##########
mkdir -p $P/usr/local/tce.installed

echo '#!/bin/sh
USER=`cat /etc/sysconfig/tcuser`
LOOP=/tmp/tcloop/nano/usr/local/share/nano/nanorc
HOME=/home/$USER/.nanorc
[ -f $HOME ] ||  cp $LOOP $HOME 
chown $USER:staff $HOME ' > $P/usr/local/tce.installed/$P

chown -R tc:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

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

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

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

ls -hal

echo 'Title:          nano.tcz
Description:    A terminal editor
Version:        4.0
Author:         see doc
Original-site:  http://www.nano-editor.org
Copying-policy: GPL v3
Size:		188K
Extension_by:   aus9 at gmx dot com
Tags:           nano terminal text editor
Comments:       Editor for terminal

                For those who need to run nano as root, read your 
                .nanorc and if you like to, copy your .nanorc to 
                /root/.nanorc  and edit as per suggestions inside file.
                To persist over reboot, add that root file to your
                /opt/.filetool.lst

                v4 makes significant changes especially to overlong lines
                and new lines. If this affects you please read
                https://www.nano-editor.org/news.php and then the docs

                online manual
                https://www.nano-editor.org/dist/v4/nano.html

                Compiled for 10 x
Change-log:     2015/08/01 first version
                2017/05/05 v 2.8.1 (juanito)
                2017/08/12 v 2.8.6 and color (aus9)
                2017/08/12 v 2.8.7 
                2018/03/23 v 2.9.4 compiled against ncursesw 
                2018/12/15 v 3.2 change nanorc test
Current:        2019/03/30 v 4.0 UTF8 support not compiled, $ info nano will display OK in aterm' > $P.tcz.info

echo 'Title:          nano-doc.tcz
Description:    docs for nano
Version:        4.0
Author:         see doc
Original-site:  http://www.nano-editor.org
Copying-policy: GPL v3
Size:		128K
Extension_by:   aus9 at gmx dot com
Tags:           nano
Comments:       Contains man page and some docs

                Optional dep = nano
                --in case you want to view color syntax html

                Optional dep = texinfo
                to view info eg  $ info nano
                
                Compiled for 10 x
Change-log:     2015/08/01 first version
                2017/05/05 v 2.8.1 (juanito)
                2017/08/12 v 2.8.6 and color (aus9)
                2017/08/12 v 2.8.7 
                2018/03/23 v 2.9.4 compiled against ncursesw 
                2018/12/15 v 3.2 change nanorc test
Current:        2019/03/30 v 4.0   ' > $P-doc.tcz.info

echo 'Title:          nano-locale.tcz
Description:    locales for nano
Version:        4.0
Author:         see doc
Original-site:  http://www.nano-editor.org
Copying-policy: GPL v3
Size:		696K
Extension_by:   aus9 at gmx dot com
Tags:           nano locale
Comments:       for non-English users

                Compiled for 10 x
Change-log:     2015/08/01 first version
                2017/05/05 v 2.8.1 (juanito)
                2017/08/12 v 2.8.6 and color (aus9)
                2017/08/12 v 2.8.7 
                2018/03/23 v 2.9.4 compiled against ncursesw 
                2018/12/15 v 3.2 change nanorc test
Current:        2019/03/30 v 4.0 ' > $P-locale.tcz.info

submitqc --libs
