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

# remove cxx bit at end after viewing error
export CC="gcc -flto -fuse-linker-plugin -mtune=generic -Os -pipe" 
export CXX="g++ -flto -fuse-linker-plugin -mtune=generic -Os -pipe"

M=man-pages
D=man-pages-doc

USER=`cat /etc/sysconfig/tcuser`

LIST="compiletc submitqc5 xz man-db-dev util-linux sed"
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
# bypass certificate issue
su -c "/usr/local/bin/wget -nc --no-check-certificate -t 0 \
https://www.kernel.org/pub/linux/docs/man-pages/man-pages-4.00.tar.xz \
" $USER 

xz -d $M-4.00.tar.xz
tar xvf $M-4.00.tar
cd $M-4.00
make install prefix=/tmp/$M/usr/local

cd /tmp

# TCZ man-pages-as-man
########################
LIST2="$M"
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

# start conversion
##################
# first load above tcz
su -c "tce-load -i /tmp/$M.tcz" $USER

# define the first text file to manipulate
INPUT=$M.tcz.list
# now strip the leading characters like usr/local etc
cat $INPUT | sed -r -s 's/.{25}//' > output1
# now strip the trailing .number
cat output1 | sed -r -s 's/.{2}$//' > output2
# sort alphabetically to make it look neater
# not pure alphabetic
cat output2 | sort -df > output3

mkdir -p $D/usr/local/share/doc/$M
DOC=$D/usr/local/share/doc/$M

# now convert
LIST3=`cat output3`
for Z in $LIST3
do
	man $Z | col -bx > $DOC/$Z
done

## create man-pages-doc
########################
LIST2="$D"
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 info files let submitqc5 add size
####################
echo "Title:          man-pages.tcz
Description:    man-pages 
Version:        1
Author:         see inside each man page 
Original-site:  https://www.kernel.org/pub/linux/docs/man-pages/
Copying-policy: GPL v3
Size:		
Extension_by:   gordons64
Tags:           man-pages man
Comments:      The man-pages from kernel.org needs man command

                                     
                --x86 maintainer curaga
		Compiled for Corepure64 6.x
Change-log:     2015/07/15 First version
Current:        2015/07/15
" > man-pages.tcz.info

echo "Title:          man-pages-doc.tcz
Description:    man-pages 
Version:        1
Author:         see inside each man page 
Original-site:  https://www.kernel.org/pub/linux/docs/man-pages/
Copying-policy: GPL v3
Size:		
Extension_by:   gordons64
Tags:           man-pages man
Comments:      The man-pages from kernel.org as text files

               A very small number of files start with the 
		understem symbol=_

		some files show conversion errors.  

                        
                --x86 maintainer curaga
		Compiled for Corepure64 6.x
Change-log:     2015/07/15 First version
Current:        2015/07/15
" > man-pages-doc.tcz.info

echo "man-db.tcz" > man-pages.tcz.dep

submitqc5