#!/bin/sh
# test we are root
if [ "$USER" != "root" ] ; then
   echo "Run as root please, exiting." 
   exit 1                                                      
fi

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

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

LIST="compiletc submitqc ncurses-dev groff file-dev xz ncurses5 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.nano-editor.org/dist/v2.8/$P-$V.tar.xz \                                  
" $USER 

xz -d $P*xz && tar xvf $P*tar
cd $SRC
./configure --enable-nanorc
make check
mkdir -p /tmp/$P
make install-strip DESTDIR=/tmp/$P

# Stripped already 

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

# main
######

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

echo '#!/bin/sh
USER=`cat /etc/sysconfig/tcuser`
rm -rf /home/$USER/.config/nano
[ -f /home/$USER/.nanorc ] || cp /usr/local/share/nano/nanorc /home/$USER/.nanorc
sudo chown $USER:staff /home/$USER/.nanorc
' > $P/usr/local/tce.installed/$P
chown -R tc:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# 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\/local/include "\/usr\/local/' -i $P/usr/local/share/nano/nanorc

# missing COPYING
mkdir -p  $P/usr/local/share/doc/$P
cp $SRC/COPYING $P/usr/local/share/doc/$P/

# 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
	cd /tmp
done

# create depend files
echo 'file.tcz
ncurses5.tcz' > $P.tcz.dep

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

# create info files let submitqc add size

echo "Title:          nano.tcz  
Description:    A terminal editor 
Version:        2.8.7
Author:         see doc  
Original-site:  http://www.nano-editor.org
Copying-policy: GPL v3
Size:		
Extension_by:   juanito
Tags:           nano terminal text editor
Comments:       Editor for terminal

                The install script will delete old .config/nano and create new config
                         
		Compiled for Corepure64 8.x
Change-log:     2015/08/01 first version
                2017/05/05 updated 2.8.1 (juanito)
                2017/08/12 update 2.8.6 fix color (aus9)
Current:        2017/08/12 update 2.8.7 (aus9)
" > nano.tcz.info

echo "Title:          nano-doc.tcz
Description:    docs for nano
Version:        2.8.7
Author:         see doc
Original-site:  http://www.nano-editor.org
Copying-policy: GPL v3
Size:		
Extension_by:   Juanito
Tags:           nano 
Comments:       Contains man page and some docs

		Compiled for Corepure64 8.x
Change-log:     2015/08/01 first version
                2017/05/05 updated 2.8.1 (juanito)
                2017/08/12 update 2.8.6 fix color (aus9)
Current:        2017/08/12 update 2.8.7 (aus9)
" > nano-doc.tcz.info

echo "Title:          nano-locale.tcz
Description:    locales for nano
Version:        2.8.7
Author:         see doc
Original-site:  http://www.nano-editor.org
Copying-policy: GPL v3
Size:		
Extension_by:   Juanito
Tags:           nano locale 
Comments:       For non-English users of nano.

		Compiled for Corepure64 8.x
Change-log:     2015/08/01 first version
                2017/05/05 updated 2.8.1 (juanito)
                2017/08/12 update 2.8.6 fix color (aus9)
Current:        2017/08/12 update 2.8.7 (aus9)
" > nano-locale.tcz.info

submitqc --libs