#!/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=nano
V=8.7
V1=v8
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

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

# fix No curses lib was found
ln -s /usr/local/lib/pkgconfig/ncursesw.pc /usr/local/lib/pkgconfig/ncurses.pc

tar xvf $SRC*xz
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
make check  #  10sec
make install-strip DESTDIR=/tmp/$P
cd /tmp

# no dev
# doc
#####
mkdir -p $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/
mv $P/usr/local/share/doc $P-doc/usr/local/share/

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

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

# edit to get color by removing comment......from 8.0 to 8.7 the wording and use of " has changed!
FILE=$P/usr/local/share/nano/nanorc 
# this now fails....sed 's|# include /usr/local/share/nano/*.nanorc|include /usr/local/share/nano/*.nanorc|' -i $FILE
# work around
sed '252d' -i $FILE
sed 's|## === Syntax coloring ===|&\ninclude /usr/local/share/nano/*.nanorc|' -i $FILE 

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

# tce.installed
##########
mkdir -p $P/usr/local/tce.installed
echo '#!/bin/sh
USER=`cat /etc/sysconfig/tcuser`
SRC=/usr/local/share/nano/nanorc
RC=/home/$USER/.nanorc
[ -f $RC ] ||  cp $SRC $RC
chown $USER:staff $RC ' > $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 . -not -type d | cut -c 2- | sort > /tmp/$Z.tcz.list
  cd /tmp
done

ls -hal

echo 'Title:          nano-doc.tcz
Description:    man & html pages & infos
Version:        8.7
Author:         nano Team
Original-site:  http://www.nano-editor.org
Copying-policy: GPL v3
Size:           148K
Extension_by:   aus9 
Tags:           nano locale
Comments:       -

Change-log:     2024/05/04 v 8.0 on 15x new kb bindings
Current:        2026/01/22 v 8.7 on 17x   ' > $P-doc.tcz.info
echo 'man-db.tcz 
texinfo.tcz            ' > $P-doc.tcz.dep 

echo 'Title:          nano-locale.tcz
Description:    locales for nano
Version:        8.6
Author:         nano Team
Original-site:  http://www.nano-editor.org
Copying-policy: GPL v3
Size:           904K
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
                2020/08/28 v 5.2 
                2023/03/13 v 7.2 on 14x 
                2024/05/04 v 8.0 on 15x new kb bindings
Current:        2026/01/22 v 8.7 on 17x  ' > $P-locale.tcz.info
echo 'nano.tcz ' > $P-locale.tcz.dep

echo 'Title:          nano.tcz
Description:    terminal editor
Version:        8.6
Author:         nano Team
Original-site:  http://www.nano-editor.org
Copying-policy: GPL v3
Size:           152K
Extension_by:   aus9 
Tags:           nano terminal text editor
Comments:       keyboard bindings 
                #####################
                ^ manual symbol = Ctrl key
                M manual symbol = is the Meta key = Alt key   

                Ctrl + (x/c/v/z) cuts/copies/pastes/undo <at cursor postion>
                Ctrl + (q/w/o/r) quits/writes/opens/replace
              
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
                2020/08/28 v 5.2 
                2023/03/13 v 7.2 on 14x 
                2024/05/04 v 8.0 on 15x new kb bindings
Current:        2026/01/22 v 8.7 on 17x' > $P.tcz.info

readelf -d $P/usr/local/bin/$P | grep 'NEEDED' # ignore TCBs
#libmagic.so.1]    file
#libncursesw.so.6]  ncursesw

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

submitqc --libs
rm -rf *.zsync


