#!/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.0
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
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://www.$P-editor.org/dist/$V1/$SRC.tar.xz   "  $USER

# 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

# 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/man $P-doc/usr/local/share/
mv $P/usr/local/share/info $P-doc/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
sed 's|# include "/usr| include "/usr|' -i $P/usr/local/share/nano/nanorc

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 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:    man pages, htmls & infos
Version:        8.0
Author:         nano Team
Original-site:  http://www.nano-editor.org
Copying-policy: GPL v3
Size:           144K
Extension_by:   aus9 @linuxquestions.org
Tags:           nano locale
Comments:       lots of information

Change-log:     2024/05/04 v 8.0 on 15x
Current:        2024/05/04   ' > $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.0
Author:         nano Team
Original-site:  http://www.nano-editor.org
Copying-policy: GPL v3
Size:           884K
Extension_by:   aus9 @linuxquestions.org
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 
Current:        2024/05/04 v 8.0 on 15x new kb bindings  ' > $P-locale.tcz.info
echo 'nano.tcz                                           ' > $P-locale.tcz.dep

echo 'Title:          nano.tcz
Description:    terminal editor
Version:        8.0
Author:         nano Team
Original-site:  http://www.nano-editor.org
Copying-policy: GPL v3
Size:           148K
Extension_by:   aus9 @linuxquestions.org
Tags:           nano terminal text editor
Comments:       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 starts line 230
                To persist over reboot, add that to your backup please
                
                New keyboard bindings (also affects nanorc)
                #####################
                ^ manual symbol = Ctrl key
                M manual symbol = is the Meta key = Alt key   

                $ nano --modernbindings <pathway2/file> 
                Ctrl + (x/c/v/z) cuts/copies/pastes/undo <at cursor postion>
                Ctrl + (q/w/o/r) quits/writes/opens/replace
              
                Many more as per https://www.nano-editor.org/dist/latest/nano.html
                Plus doc TCE has lots of new stuff. Due to these changes I suggest you 
                read the new doc TCE please

                Watch out for delete, it deletes entire line at cursor position!
                Paste does not paste external buffer only internal copy to nano
                Your window manager keybindings may also affect nano

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 
Current:        2024/05/04 v 8.0 on 15x new kb bindings' > $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


