#!/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"

P=nano
V=7.2
V1=v7
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 No curses lib was found
ln -s /usr/local/lib/pkgconfig/ncursesw.pc /usr/local/lib/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
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

# no docs
##########
rm -rf $P/usr/local/share/man 
rm -rf $P/usr/local/share/info 
rm -rf $P/usr/local/share/doc

# 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`
LINK=/usr/local/share/nano/nanorc
HOME=/home/$USER/.nanorc
[ -f $HOME ] ||  cp $LINK $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-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-locale.tcz
Description:    locales for nano
Version:        5.2
Author:         see doc
Original-site:  http://www.nano-editor.org
Copying-policy: GPL v3
Size:           876K
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 
Current:        2023/03/13 v 7.2 on 14x  ' > $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:           148K
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 to your backup please
                
                online manual
                https://www.nano-editor.org/dist/latest/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
                2020/08/28 v 5.2 
Current:        2023/03/13 v 7.2 on 14x ' > $P.tcz.info

readelf -d $P/usr/local/bin/$P | grep 'NEEDED'
# 0x0000000000000001 (NEEDED) Shared library: [libz.so.1]        TCB
# 0x0000000000000001 (NEEDED) Shared library: [libmagic.so.1]    file
# 0x0000000000000001 (NEEDED) Shared library: [libncursesw.so.6]  ncursesw
# 0x0000000000000001 (NEEDED) Shared library: [libc.so.6]        TCB

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

echo 'nano.tcz' > $P-locale.tcz.dep

submitqc --libs
