#!/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
USER=`cat /etc/sysconfig/tcuser`

LIST="compiletc submitqc5 ncurses-dev groff file-dev"
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
su -c "/usr/local/bin/wget \
http://ftp.gnu.org/gnu/nano/nano-2.4.2.tar.gz  \                                  
" $USER 

# download a config for nano from bmarkus 32 TCZ
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://www.dropbox.com/s/6dp5lervnse7lsr/nanoconfig?dl=0 \
-O /tmp/nanoconfig" $USER 

# download the tce.installed
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://www.dropbox.com/s/w4ln986uv8sevhl/tceinstall?dl=0 \
-O /tmp/tceinstall " $USER

# we are root for rest of commands
tar zxvf $P-2.4.2.tar.gz
cd $P-2.4.2
# configure default=/usr/local
# it ignores my attempt at --sysconfdir=/usr/local/(etc) 
# and so tce.install adapted below
./configure  --enable-utf8 --enable-color --enable-extra \
--enable-multibuffer --enable-nanorc 
make check
mkdir -p /tmp/$P
make install DESTDIR=/tmp/$P

# Strip executables
cd /tmp/$P
find . | xargs file | grep "executable"    | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
find . | xargs file | grep "shared object" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
cd /tmp
##############
# create locale
##############
mkdir -p $P-locale/usr/local/share
mv $P/usr/local/share/locale $P-locale/usr/local/share

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

# tce.installed 
##########
mkdir -p $P/usr/local/tce.installed
cp /tmp/tceinstall  $P/usr/local/tce.installed/nano
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# add the config to TCZ
cp /tmp/nanoconfig $P/usr/local/share/nano/nanoconfig

# 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 -not -type d > /tmp/$Z.tcz.list
	cd /tmp
done

# create depend files
echo "file.tcz" > nano.tcz.dep

# create info files let submitqc5 add size

echo "Title:          nano.tcz  TESTING
Description:    A terminal editor 
Version:        2.4.2
Author:         see doc  
Original-site:  http://www.nano-editor.org
Copying-policy: GPL v3
Size:		
Extension_by:   gordons64
Tags:           nano utf8 terminal text editor
Comments:       Editor for terminal
                         
		Compiled for Corepure64 6.x
                x86 maintainer=bmarkus
Change-log:     2015/08/01 First version 
Current:        2015/08/01
" > nano.tcz.info

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

		Compiled for Corepure64 6.x
               x86 maintainer=bmarkus
Change-log:     2015/08/01 First version 
Current:        2015/08/01
" > nano-doc.tcz.info

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

		Compiled for Corepure64 6.x
               x86 maintainer=bmarkus
Change-log:     2015/08/01 First version 
Current:        2015/08/01 
" > nano-locale.tcz.info

submitqc5