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

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

V1=v5
P=nano
V=5.2
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`

# texinfo to bypass missing makinfo error
LIST="compiletc submitqc xz ncursesw-dev groff file-dev sed texinfo"
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://www.$P-editor.org/dist/$V1/$SRC.tar.xz   "  $USER

# fix 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
# disable utf8 allows aterm to display correctly
sed 's|@documentencoding UTF-8||' -i doc/nano.texi
./configure  --prefix=/usr/local --disable-extra  --enable-nanorc --disable-utf8
# The curses library to be used is: ncurses but the only ncurses installed is ncursesw
make check  #  ~ 30sec
mkdir -p /tmp/$P
make install-strip DESTDIR=/tmp/$P
cd /tmp

# no dev

# locale
#########
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

mkdir -p $P/usr/local/share/doc/$P
echo 'see doc tcz for license' > $P/usr/local/share/doc/$P/COPYING

# 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 root: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

ls -hal

echo 'Title:          nano-doc.tcz
Description:    docs for nano
Version:        5.2
Author:         see doc
Original-site:  http://www.nano-editor.org
Copying-policy: GPL v3
Size:           140K
Extension_by:   aus9
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

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
                2019/03/30 v 4.0 UTF8 support not compiled, $ info nano will display OK in aterm
                2020/04/17 v 4.9.2 on 11x
Current:        2020/08/28 v 5.2    ' > $P-doc.tcz.info

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

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
                2019/03/30 v 4.0
                2019/03/30 v 4.0 UTF8 support not compiled, $ info nano will display OK in aterm
                2020/04/17 v 4.9.2 on 11x
Current:        2020/08/28 v 5.2  ' > $P-locale.tcz.info

echo 'Title:          nano.tcz
Description:    A terminal editor
Version:        5.2
Author:         see doc
Original-site:  http://www.nano-editor.org
Copying-policy: GPL v3
Size:           168K
Extension_by:   aus9
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. line  ~202
                To persist over reboot, add that root file to your
                /opt/.filetool.lst

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

Change-log:     2015/08/01 first version
                2017/05/05 v 2.8.1 (juanito)
                2017/08/12 v 2.8.6 add 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
                2019/03/30 v 4.0 UTF8 support not compiled, $ info nano will display OK in aterm
                2020/04/17 v 4.9.2 on 11x
Current:        2020/08/28 v 5.2 ' > $P.tcz.info

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

submitqc --libs
