#!/bin/sh
#Copyright (c) 2007, Zane C. Bowers
#All rights reserved.
#
#Redistribution and use in source and binary forms, with or without modification,
#are permitted provided that the following conditions are met:
#
#   * Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
#WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
#INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
#DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
#LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
#THE POSSIBILITY OF SUCH DAMAGE.

shortRandom() {
    ( /bin/ps ax && /usr/bin/netstat -an && /bin/date ) | /usr/bin/sum | /usr/bin/cut -c4-5 | /usr/bin/sed 's/ //g'
}

getline() { #arg1=file    arg2=line
    /bin/cat "$1" | /usr/bin/head -n "$2" | /usr/bin/tail -n 1
}

manInstall(){
    #$1 = PREFIX
    #$2 = CENTER
    #$3 = VERSION
    
    #makes the man dir if it does not exist
    if /bin/[ ! -d "$1/man" ]; then
	/bin/mkdir "$1/man"
	/bin/chmod 755 "$1/man"
    fi    
    
    manInstallTMPfile=/tmp/`shortRandom``shortRandom``shortRandom`$$`shortRandom`
    /usr/bin/touch $manInstallTMPfile
    /bin/chmod go-rwx $manInstallTMPfile

    /bin/ls ./pod/*.pod | grep -v \\.svn >> $manInstallTMPfile

    manInstallNOL=`/bin/cat $manInstallTMPfile | /usr/bin/wc -l`
    manInstallCL=1

    while /bin/[ $manInstallCL -le $manInstallNOL ]; do
	manInstallLine=`getline $manInstallTMPfile $manInstallCL`

	manInstallMANname=`echo $manInstallLine | /usr/bin/sed 's/^.\/pod\///' |  /usr/bin/sed 's/.pod$//' | /usr/bin/sed 's/^.\///' | /usr/bin/cut -d@ -f1`

	manInstallMANsection=`echo $manInstallLine | /usr/bin/sed 's/^.\/pod\///' | /usr/bin/sed 's/.pod$//' | /usr/bin/sed 's/^.\///' | /usr/bin/cut -d@ -f2`

	#assume it is function related if it is not specified
	if /bin/[ -z $manInstallMANsection ]; then
	    manInstallMANsection=3
	fi    

	manInstallMANcenter=`echo $manInstallLine | /usr/bin/sed 's/^.\/pod\///' | /usr/bin/sed 's/.pod$//' | /usr/bin/sed 's/^.\///' | /usr/bin/cut -d@ -f3`

	#assume it is the same as $CENTER if not defined
	if /bin/[ -z "$manInstallMANcenter" ]; then
	    manInstallMANcenter="$2"
	fi
    


	#makes the man section dir if it does not exist
        if /bin/[ ! -d $1/man/man$manInstallMANsection ]; then
	    /bin/mkdir "$1/man/man$manInstallMANsection"
	    /bin/chmod 755 "$1/man/man$manInstallMANsection"
	fi

	#echo $line
	#echo $MANname
	#echo $MANsection
	#echo $MANcenter
    
	manInstallMANfile=`echo $1/man/man$manInstallMANsection/$manInstallMANname.$manInstallMANsection | sed 's/\/\//\//g'`
    
        /usr/local/bin/pod2man --section="$manInstallMANsection" --release="$3" --name="$manInstallMANname" --center="$manInstallMANcenter" "$manInstallLine" "$manInstallMANfile"

	/bin/chmod 644 "$manInstallMANfile"
    
	manInstallCL=`/bin/expr $manInstallCL + 1`
    done

    /bin/rm $manInstallTMPfile
}

binInstall(){
    #$1 = prefix

    if /bin/[ ! -d "$1/bin" ]; then
	/bin/mkdir "$1/bin"
        if /bin/[ ! -d $1/bin ]; then
	    echo "$1/bin does not a dir or does not exist and could not be created"
	    exit 1;
	fi
	/bin/chmod u+rwx "$1/bin"

    fi

    binInstallTMPfile=/tmp/`shortRandom``shortRandom``shortRandom`$$`shortRandom`
    /usr/bin/touch $binInstallTMPfile
    /bin/chmod go-rwx $binInstallTMPfile

    /bin/ls ./bin/ | /usr/bin/grep -v \\.svn >> $binInstallTMPfile

    binInstallNOL=`/bin/cat $binInstallTMPfile | /usr/bin/wc -l`
    binInstallCL=1

    while /bin/[ $binInstallCL -le $binInstallNOL ]; do
	binInstallLine=`getline $binInstallTMPfile $binInstallCL`
    
	/bin/cat "./bin/$binInstallLine" | /usr/bin/sed "s/%%PREFIX%%/"`echo $1 | sed 's/\\//\\\\\//g''`"/g" > "$1/bin/$binInstallLine"

	/bin/chmod 755 "$1/bin/$binInstallLine"
    
	binInstallCL=`/bin/expr $binInstallCL + 1`
    done
    
    /bin/rm $binInstallTMPfile
}

includeInstall(){
    #$1 = prefix

    includeInstallTMPfile=/tmp/`shortRandom``shortRandom``shortRandom`$$`shortRandom`
    /usr/bin/touch $includeInstallTMPfile
    /bin/chmod go-rwx $includeInstallTMPfile

    /usr/bin/find ./libexec | /usr/bin/grep -v \\.svn >> $includeInstallTMPfile

    includeInstallNOL=`/bin/cat $includeInstallTMPfile | /usr/bin/wc -l`
    includeInstallCL=1

    while /bin/[ $includeInstallCL -le $includeInstallNOL ]; do
	includeInstallLine=`getline $includeInstallTMPfile $includeInstallCL`
    
	if /bin/[ -d $includeInstallLine ]; then
	    if /bin/[ ! -d "$1/$includeInstallLine" ]; then
		mkdir "$1/$includeInstallLine"
		chmod 755 "$1/$includeInstallLine"
	    fi
	else
	    cat "$includeInstallLine" | /usr/bin/sed "s/%%PREFIX%%/"`echo $1 | sed 's/\\//\\\\\//g''`"/g" > "$1/$includeInstallLine"
	    /bin/chmod 644 "$1/$includeInstallLine"
	fi
    
	includeInstallCL=`/bin/expr $includeInstallCL + 1`
    done

    /bin/rm $includeInstallTMPfile
}

PREFIX=/usr/local/
VERSION=0.1.0
CENTER="Shell Include User Manual"

if /bin/[ ! -z $1 ]; then
    PREFIX=$1
fi

if /bin/[ ! -d "$PREFIX" ]; then
    /bin/mkdir "$PREFIX"
    if /bin/[ ! -d "$PREFIX" ]; then
	echo "$PREFIX does not a dir or does not exist and could not be created"
    fi
    /bin/chmod 755 "$PREFIX"
fi



includeInstall "$PREFIX"

manInstall "$PREFIX" "$CENTER" "$VERSION"

binInstall "$PREFIX"


syntax highlighted by Code2HTML, v. 0.9.1