#!/bin/sh
## To be run in the directory where "doc" is the dir to be made into the doc extension

. /etc/init.d/tc-functions
PKG=doc

DIR0=`pwd`

#Choose "gzip" or "bzip2"
COMP="bzip2"


unset LINK MAN RUN INFO



for D in `find "$PKG" -type d -name info`; do
	cd "$D"

	
	#Convert hard links to symbolic links
	for I in `ls`; do for F in `find . -xdev -samefile "$I" -not -name "$I"`; \
do ln -sf "$I" "$F"; done ; done
	#Fix links about to be broken
        for eachpage in `find . -type l` ; do
	 if echo $eachpage | grep -v ".bz2$" | grep -v ".gz$" > /dev/null 2>&1; then 
	   if ! readlink $eachpage | grep ".bz2" >/dev/null && ! readlink $eachpage \
| grep ".gz" >/dev/null; then
	     INFOLINK=1
	    if [ "$COMP" == "gzip" ]; then  
	     ln -s $(readlink $eachpage).gz $eachpage.gz 
             rm $eachpage
	    fi
	    if [ "$COMP" == "bzip2" ]; then  
	     ln -s $(readlink $eachpage).bz2 $eachpage.bz2 
             rm $eachpage
	    fi
	    
	   fi
	    
	  fi
	
        done

 if ls *.info* > /dev/null 2>&1; then	
  for I in `ls *.info*`; do	
	     if file "$I" | grep -v "symbolic link" > /dev/null; then
	       INFO=1
	       if [ "$COMP" == "gzip" ]; then	
	          gzip -9 "$I"
	       fi
	       if [ "$COMP" == "bzip2" ]; then	
	          bzip2 -9 "$I"
	       fi	
	        if [ ! "$RUN1" == "1" ]; then		
		  echo "${BLUE}"$T":  ${YELLOW}Compressing info pages..${NORMAL}"
		  RUN1=1
	        fi	
	     fi	
 
  done
 fi
 cd "$DIR0"

done


for D in `find "$PKG" -type d -name man`; do
   cd "$D"
    for manpagedir in `find . -type d -name "man*"` ; do
DIR1=`pwd`
        cd $manpagedir

	#Convert hard links to symbolic
	for I in `ls`; do for F in `find . -xdev -samefile "$I" -not -name "$I"`; \
do ln -sf "$I" "$F"; done ; done

	
	#Fix links about to be broken
        for eachpage in `find . -type l` ; do
	 if echo $eachpage | grep -v ".bz2$" | grep -v ".gz$" > /dev/null 2>&1; then 
	   if ! readlink $eachpage | grep ".bz2" >/dev/null && ! readlink $eachpage \
| grep ".gz" >/dev/null; then
	     LINK=1
	    if [ "$COMP" == "gzip" ]; then  
	     ln -s $(readlink $eachpage).gz $eachpage.gz 
             rm $eachpage
	    fi
	    if [ "$COMP" == "bzip2" ]; then  
	     ln -s $(readlink $eachpage).bz2 $eachpage.bz2 
             rm $eachpage
	    fi
	    
	   fi
	    
	  fi
	if [ "$LINK" == "1" ] && [ ! "$RUN" == "1" ]; then		
		echo "${BLUE}"$T":  ${YELLOW}Fixing manpage symlinks..${NORMAL}"
		RUN=1
	fi	
        done
	
	#Compress man pages
        for I in `ls`; do	
	     if file "$I" | grep -v "symbolic link" | grep "text" > /dev/null; then
	     MAN=1
	       if [ "$COMP" == "gzip" ]; then	
	          gzip -9 "$I"
	       fi
	       if [ "$COMP" == "bzip2" ]; then	
	          bzip2 -9 "$I"
	       fi	
	 
	     fi	
	done
          if [ "$MAN" == "1" ] && [ ! "$RUN2" == "1" ]; then
		echo "${BLUE}"$T":  ${YELLOW}Compressing manpages..${NORMAL}"
		RUN2=1
          fi
        cd "$DIR1"
    done
  cd "$DIR0"
done
 





