#!/bin/bash
#
# slackbuilder
# Copyright (C) 2005 Stefano Stabellini stefano@stabellini.net
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

__help(){
        cat << EOF
        usage: slackbuilder <options> <action>
        
        where action is:
        --update		updates packages databases
        --search <package>	search a <package>
        --get <package>		gets <package> sources
        --build <package>	builds <package> from source
        --install <package>	builds and installs <package> from source
        
        where options can be:
        --mute			doesn\'t ask anything
        --no-dep		doesn\'t check for dependencies
        --regexp		uses regular expressions
        --noclean		doesn\'t delete temporary files
        --no-requiredbuilder	doesn\'t call requiredbuilder even if it is present
        --no-md5		doesn\'t check MD5
        --no-sources		doesn\'t get any files written in sources
        --versions		check package versions in dependencies
        
        version is optional and can be specified after <package>:
        -gt <version>		greater than <version>
        -ge <version>		greater than or equal to <version>
        -lt <version>		less than <version>
        -le <version>		less than or equal to <version>
        -eq <version>		equal to <version>

EOF
        exit 1
}


__checks(){
	if [[ "$UID" != 0 ]]; then
		echo "slackbuilder must be run as root"
		exit 1
	fi
	if ! which awk &>/dev/null
	then
		echo "awk is required"
		exit 1
	fi
	if ! which sed &>/dev/null
	then
		echo "sed is required"
		exit 1
	fi
	if ! which wget &>/dev/null
	then
		echo "wget is requird"
		exit 1
	fi
	if ! which md5sum &>/dev/null
	then
		echo "md5sum is required"
		exit 1
	fi
	if ! which grep &>/dev/null
	then
		echo "grep is required"
		exit 1
	fi
}

#$1 operator
#$2 first operand
#$3 second operand
comp(){
	A2=`echo $2 | cut -d "-" -f 1`
	A3=`echo $3 | cut -d "-" -f 1`
	if test $1 = ge || test $1 = le || test $1 = eq
	then
		if test $A2 = $A3
		then
			echo 1
			return
		fi
	fi
	if test $1 = eq
	then
		echo 0
		return
	fi
	NF2=`echo $A2 | awk -F . '{print NF}'`
	NF3=`echo $A3 | awk -F . '{print NF}'`
	if test $NF2 -le $NF3
	then
		NF=$NF2
	else
		NF=$NF3
	fi
	for (( ci=1; ci <= $NF; ci++ ))
	do
		E2=`echo $A2 | cut -d . -f $ci`
		E3=`echo $A3 | cut -d . -f $ci`
		if test $1 = ge || test $1 = gt
		then
			if test $E2 = $E3
			then
				continue
			elif [[ $E2 > $E3 ]]
			then
				echo 1
				return
			else
				echo 0
				return
			fi
		else
			if test $E2 = $E3
			then
				continue
			elif [[ $E2 < $E3 ]] 
			then
				echo 1
				return
			else
				echo 0
				return
			fi
		fi
	done
	echo 1
	return
}

#converts glibc-2.3.5-i486-5 to 2.3.5-i486-5
completetoversion(){
	NUMFIELDS=`echo "$1" | awk -F - '{print NF}'`
	if test $NUMFIELDS -ge 4
	then
		echo ${1#${1%-*-*-*}-}
	fi
}

#converts glibc-2.3.5-i486-5 to glibc
completetoname(){
	NUMFIELDS=`echo "$1" | awk -F - '{print NF}'`
	if test $NUMFIELDS -ge 4
	then
		echo ${1%-*-*-*}
	fi
}

clean(){
	local PACKAGE="$1"
	
	cd /tmp
	rm -rf slackbuilder/$PACKAGE &>/dev/null
	RM=`find ./ -maxdepth 1 -cnewer /var/slackbuilder/TIMESTAMP -iname $PACKAGE-\*`
	if test "$RM"
	then
		rm -rf /tmp/$RM &>/dev/null
	fi
	RM=`find ./ -maxdepth 1 -cnewer /var/slackbuilder/TIMESTAMP -iname package-$PACKAGE`
	if test "$RM"
	then
		rm -rf /tmp/$RM &>/dev/null
	fi
	echo cleaned up temporary files
}

__exit(){
	if test "$CLEAN" && test $CLEAN -eq 1
	then
		clean $2
	fi
	exit $1
}

__copy(){
	cp $2 $1
}

#input: $1 = package
#input: $2 $3 = operator version
#output: RES RESREP RESVERSION DEP SUGGESTS CONFLICTS COUNTER
__search(){
	local PACKAGE="$1"
	local OPERATOR="$2"
	local V="$3"
	
	cd /var/slackbuilder
	touch /var/slackbuilder/TIMESTAMP
	COUNTER=0
	for ((i = 0; i  < ${#REP[@]}; i++))
	do
		if test ! -f ${FILELIST[$i]}.sources
		then
			if test "$OPERATOR" && test "$V"
			then
				echo "${REP[$i]}: no source packages versions available"
				continue
			fi
			LINERES=`grep $REGEXP "$PACKAGE" ${FILELIST[$i]}.packages`
			for j in $LINERES
			do
				if ! [[ " "${EXCLUDE[@]}" " == *" "$j" "* ]]
				then
					RES[$COUNTER]="$j"
					RESVERSION[$COUNTER]=""
					RESREP[$COUNTER]=$i
					COUNTER=$(( $COUNTER + 1 ))
				fi
			done
		else
			LINERES=`grep -n $REGEXP "$PACKAGE" ${FILELIST[$i]}.packages | cut -d \: -f 1`
			exec 6<&0
			exec < ${FILELIST[$i]}.sources
			LINECOUNTER=1
			read line
			for j in $LINERES
			do
				while test $LINECOUNTER -lt $j
				do
					read line
					LINECOUNTER=$(( $LINECOUNTER + 1 ))
				done
				if test $LINECOUNTER -eq $j
				then
					RES[$COUNTER]=`echo $line | awk '{print$1}'`
					if ! [[ " "${EXCLUDE[@]}" " == *" "${RES[$COUNTER]}" "* ]]
					then
						RESVERSION[$COUNTER]=`echo $line | awk '{print$2}'`
						if test "$OPERATOR" && test "$V"
						then
							if  test "${RESVERSION[$COUNTER]}" && test ${RESVERSION[$COUNTER]} != "none" && test "`comp $OPERATOR ${RESVERSION[$COUNTER]} $V`" -eq 1
							then
								DEP[$COUNTER]=`echo $line | cut -d \: -f 2`
								CONFLICTS[$COUNTER]=`echo $line | cut -d \: -f 3`
								SUGGESTS[$COUNTER]=`echo $line | cut -d \: -f 4`
								RESREP[$COUNTER]=$i
								COUNTER=$(( $COUNTER + 1 ))
							fi
						else
							DEP[$COUNTER]=`echo $line | cut -d \: -f 2`
							CONFLICTS[$COUNTER]=`echo $line | cut -d \: -f 3`
							SUGGESTS[$COUNTER]=`echo $line | cut -d \: -f 4`
							RESREP[$COUNTER]=$i
							COUNTER=$(( $COUNTER + 1 ))
						fi
					fi
				fi
			done
			exec 0<&6 6<&-	
		fi
	done
}

#input $COUNTER $RESREP $RES $RESVERSION
#output echo package
#output $NUM
__ask(){
	if test "$COUNTER" -eq 0
	then
		echo "No packages found"
		exit 1
	elif test "$COUNTER" -gt 1
	then
		if test "$MUTE" && test $MUTE -eq 0
		then
			echo "From ${REP[${RESREP[0]}]}"
			echo "0    ${RES[0]}    ${RESVERSION[0]}"
			oldi=0
			for ((i = 1; i  < $COUNTER; i++))
			do
				if test ${RESREP[$i]} -ne ${RESREP[$oldi]}
				then
					echo -e "\nFrom ${REP[${RESREP[$i]}]}"
				fi
				echo "$i    ${RES[$i]}    ${RESVERSION[$i]}"
				oldi=$i
			done
		
			echo -n "Which one do I have to install? Insert its number: "
			read NUM
		else
			NUM=0
		fi
	else
		NUM=0
	fi
	if test $COUNTER -le $NUM
	then
		echo "Wrong package number."
		exit 1
	fi	
}

#input $1 = package
#input $2 = indexrepos
__download(){
	local PACKAGE="$1"
	local INDEXREPOS="$2"
	
	cd /var/slackbuilder
	
	TDF=`grep \/$PACKAGE\/ ${FILELIST[$INDEXREPOS]}.list | grep -E '[sS]lack[bB]uild|\.[bB]uild' | awk '{print$NF}' | head -n 1`
	TDF=`dirname $TDF`/
	DF=`grep -e "-r[r,w,x,-][r,w,x,-][r,w,x,-][r,w,x,-][r,w,x,-][r,w,x,-][r,w,x,-][r,w,x,-]  " ${FILELIST[$INDEXREPOS]}.list | grep $TDF | awk '{print$NF}'`
	DD=`grep -e "dr[r,w,x,-][r,w,x,-][r,w,x,-][r,w,x,-][r,w,x,-][r,w,x,-][r,w,x,-][r,w,x,-]  " ${FILELIST[$INDEXREPOS]}.list | grep $TDF | awk '{print$NF}'`
	if test -d /tmp/slackbuilder/$PACKAGE
	then
		rm -rf $TMP/$PACKAGE
	fi
	mkdir $TMP/$PACKAGE &>/dev/null
	if test "$MD5" && test $MD5 -eq 1 && test -f ${FILELIST[$INDEXREPOS]}.md5
	then
		grep $TDF ${FILELIST[$INDEXREPOS]}.md5 > $TMP/$PACKAGE/.SLACKBUILDER.md5
		MD5=yes
	else
		MD5=no
	fi
	for i in $DD
	do
		i=${i#$TDF}
		if test "$i"
		then
			mkdir -p $TMP/$PACKAGE/$i &>/dev/null
		fi
	done
	cd $TMP/$PACKAGE
	for i in $DF
	do
		i=${i#$TDF}
		#NF=`basename $i`
		echo "Downloading ${REP[$INDEXREPOS]}$TDF$i"
		if ! ${WGET[$INDEXREPOS]} ./$i ${REP[$INDEXREPOS]}$TDF$i 2>/dev/null
		then
			echo "${REP[$INDEXREPOS]}$i error"
			__exit 1 $PACKAGE
		fi
	done
	if test "$MD5" && test $MD5 = "yes"
	then
		exec 6<&0 
		exec < .SLACKBUILDER.md5
		#doesn't need a blank line at the end of the file
		while read line
		do
			MDFILE=`echo $line | awk '{print$2}'`
			MDFIRM=`echo $line | awk '{print$1}'`
			MDFILE=${MDFILE#$TDF}
			DFIRM=`md5sum ./$MDFILE 2>/dev/null | awk '{print$1}'`
			if test "$DFIRM" != "$MDFIRM"
			then
				echo "${REP[$INDEXREPOS]}$MDFILE md5 did not match"
				__exit 1 $PACKAGE
			fi
		done
		exec 0<&6 6<&-
	fi
	if test $NOSOURCES -eq 0 && test -f sources
	then
		NAME=$PACKAGE
		DCNF=`cat sources | sed /^$/d | sed /#/d`
		for i in $DCNF
		do
			CNF=`eval echo $i`
			NF=`basename $CNF`
			echo "Downloading $CNF"
			if ! wget -c -O $NF $CNF 2>/dev/null
			then
				echo "$CNF error"
				__exit 1 $PACKAGE
			fi
		done
	fi
	
}

#input $1 = package
#output $FILE
__build(){
	local PACKAGE="$1"
	
	cd $TMP/$PACKAGE
	
	echo "Building $PACKAGE..."
	
	if test -f $PACKAGE.SlackBuild
	then
		SLACKBUILD=$PACKAGE.SlackBuild
	elif test -f $PACKAGE.Slackbuild
	then
		SLACKBUILD=$PACKAGE.Slackbuild
	elif test -f $PACKAGE.slackbuild
	then
		SLACKBUILD=$PACKAGE.slackbuild
	elif test -f $PACKAGE.slackBuild
	then
		SLACKBUILD=$PACKAGE.slackBuild
	elif test -f SlackBuild
	then
		SLACKBUILD=SlackBuild
	elif test -f Slackbuild
	then
		SLACKBUILD=Slackbuild
	elif test -f slackbuild
	then
		SLACKBUILD=slackbuild
	elif test -f slackBuild
	then
		SLACKBUILD=slackBuild
	elif test -f $PACKAGE.build
	then
		SLACKBUILD=$PACKAGE.build
	elif test -f build
	then
		SLACKBUILD="build"
	else
		echo "SlackBuild not found for $PACKAGE"
	fi
	
	mv $SLACKBUILD $SLACKBUILD.old &>/dev/null
	
	exec 6<&0
	exec < $SLACKBUILD.old
	
	while read -r line
	do
		if [[ $line = *VERSION=* ]]
		then
			VERSION=${line#*VERSION=}
		elif [[ $line = *BUILD=* ]]
		then
			BUILD=${line#*BUILD=}
		elif [[ $line = *ARCH=\$\{ARCH:-* ]]
		then
			ARCHSET=${line#*ARCH=\$\{ARCH:-}
			ARCHSET=${ARCHSET%\}}
			if test "$ARCH"
			then
				line=${line/ARCH=*/ARCH=$ARCH}
			fi
		elif [[ $line = *ARCH=* ]]
		then
			ARCHSET=${line#*ARCH=}
			if test "$ARCH"
			then
				line=${line/ARCH=*/ARCH=$ARCH}
			fi
		elif test "$SLKCFLAGS" && [[ $line = *SLKCFLAGS=* ]]
		then
			line=${line/SLKCFLAGS=*/SLKCFLAGS=$SLKCFLAGS}
		elif test "$ARCH" && [[ $line = *\$ARCH-slackware-linux* ]]
		then
			line=${line/\$ARCH/$ARCHSET}
		elif test "$NOREQUIREDBUILDER" && test $NOREQUIREDBUILDER -eq 1 && [[ $line = *requiredbuilder* ]]
		then
			line=${line/requiredbuilder/\#requiredbuilder}
		fi
		echo "$line" >> $SLACKBUILD
	done
	
	exec 0<&6 6<&-
	rm $SLACKBUILD.old &>/dev/null
	
	sh $SLACKBUILD | tee $TMP/$PACKAGE/slackbuilder.log
	
	echo ""
	echo ""
	
	if test -f /tmp/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
	then
		FILE=/tmp/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
	else
		FILE=`find /tmp -maxdepth 1 -cnewer /var/slackbuilder/TIMESTAMP -iname $PACKAGE\*.tgz`
	fi
	
	if test "`grep -e 'make\[[0-9]*\]: \*\*\* \[.*\] Error [0-9]*' $TMP/$PACKAGE/slackbuilder.log`" || test "`grep "make: *** No rule to make target" $TMP/$PACKAGE/slackbuilder.log`"
	then
		echo "WARNING: The building process seems to have failed"
		if test "$FILE" && test "`echo $FILE | awk '{print NF}'`" -eq 1
		then
			rm $FILE &>/dev/null
			__exit 1 $PACKAGE
		fi
	fi
	
	if test "$FILE" && test "`echo $FILE | awk '{print NF}'`" -eq 1
	then
		mv $FILE $CALLDIR &>/dev/null
		FILE=`basename $FILE`
	else
		echo "no package built found for $PACKAGE"
		__exit 1 $PACKAGE
	fi
	
	echo "The build process has been successfull: a Slackware tgz for $PACKAGE has been put in $CALLDIR"
}

#input: $1 = <package> conflicts
#input: ASUG
#input: AREQ AACON
#output FCON
__conflictscheck() {
	local TLINE
	local NF
	local i
	
	cd /var/log/packages
	
	ACON[$CRI]="$1"
	CRI=$(( $CRI + 1 ))
	
	for (( i=0; i < ${#ASUG[@]} ; i++ ))
	do
			if test "${ASUG[$i]}"
			then
				__search "^${ASUG[$i]}$"
				
				if test "$COUNTER" -gt 0
				then
					NUM=0
					ACON[$CRI]="${CONFLICTS[$NUM]}"
					CRI=$(( $CRI + 1 ))
				else
					ASUG[$i]=""
				fi
			fi
	done
	
	for (( i=0; i < ${#AREQ[@]}; i++ ))
	do
			if test "${AREQ[$i]}"
			then
				ACON[$CRI]="${AACON[$i]}"
				CRI=$(( $CRI + 1 ))
			fi
	done

	CRI=0
	
	for (( z=0; z < ${#ACON[@]}; z++ ))
	do
	
		TLINE="${ACON[$z]}"
		NF=`echo "$TLINE" | awk -F "," '{print NF}'`
		
		for (( i=1; i <= $NF; i++ ))
		do
			CON=`echo $TLINE| cut -d "," -f $i`
			if test -z "$CON" || test "$CON" = " "
			then
				break
			fi
			TRES=`ls $CON-* 2>/dev/null`
			F=0
			for j in $TRES
			do
				if test `completetoname $j` != $CON
				then
					continue
				else
					F=1
					break
				fi
			done
			
			if test $F -eq 0
			then
				for (( k=0; k< ${#AREQ[@]}; k++ ))
				do
					if test "${AREQ[$k]}" && test ${AREQ[$k]} = $CON
					then
						F=1
						break
					fi
				done
			fi
			
			if test $F -eq 1 && ! [[ " "${FCON[@]}" " == *" "$CON" "* ]]
			then	
				FCON[$CRI]="$CON"
				CRI=$(( $CRI + 1 ))
			fi
		done
	
	done
}


__suggestscheck() {
	local TLINE
	local NF
	local i
	
	cd /var/log/packages
	
	TLINE="$1"
	NF=`echo "$TLINE" | awk -F "," '{print NF}'`
	
	for (( i=1; i <= $NF; i++ ))
	do
		SUG=`echo $TLINE| cut -d "," -f $i`
		if test -z "$SUG" || test "$SUG" = " "
		then
			return
		fi
		TRES=`ls $SUG-* 2>/dev/null`
		F=0
		for j in $TRES
		do
			if test `completetoname $j` != $SUG
			then
				continue
			else
				F=1
				break
			fi
		done
		
		if test $F -eq 0
		then	
			ASUG[$SRI]="$SUG"
			SRI=$(( $SRI + 1 ))
		fi
	done
}


#input $DEP $NUM
#output:
#AREQ array with package name dependencies
#AVER array with versions
#AOP array with operators
#AACON array with conflicts
#ARI array index
__depcheck(){
	local i
	local NF
	local TDEP
	local TLINE
	local TLINE2
	local TAREQ
	local TAOP
	local TAVER
	local TVER
	local TREQ
	local TCON
	
	cd /var/log/packages
	
	TLINE="$1"
	
	NF=`echo "$TLINE" | awk -F "," '{print NF}'`
	for (( i=1; i <= $NF; i++ ))
	do
		TDEP=`echo $TLINE| cut -d "," -f $i`
		if test -z "$TDEP" || test "$TDEP" = " "
		then
			return
		fi
		if test "$VERSIONS" && test $VERSIONS -eq 0
		then
			TAREQ=`echo $TDEP| awk '{print$1}'`
			if [[ " "${EXCLUDE[@]}" " == *" "$TAREQ" "* ]]
			then
				continue
			fi
			if [[ " "$OLDSEARCH" " == *" "$TAREQ" "* ]]
			then
				continue
			fi
			TAOP=""
			TAVER=""
		else
			if test "`echo $TDEP | awk '{print NF}'`" -gt 2
			then
				TAREQ=`echo $TDEP | awk '{print$1}'`
				if [[ " "${EXCLUDE[@]}" " == *" "$TAREQ" "* ]]
				then
					continue
				fi
				if [[ " "$OLDSEARCH" " == *" "$TAREQ" "* ]]
				then
					continue
				fi
				TAOP=`echo $TDEP | awk '{print$2}'`
				if test "$TAOP" = '>='
				then
					TAOP=ge
				elif test "$TAOPT" = '>'
				then
					TAOP=gt
				elif test "$TAOP" = '<='
				then
					TAOP=le
				elif test "$TAOP" = '<'
				then
					TAOP=lt
				elif test "$TAOP" = '='
				then
					TAOP=eq
				else
					echo "syntax error in dependencies"
					continue
				fi
				TAVER=`echo $TDEP | awk '{print$3}'`
			elif test "`echo $TDEP | awk '{print NF}'`" -eq 1
			then
				TAREQ="$TDEP"
				TAOP=""
				TAVER=""
			fi
		fi
	
		TRES=`ls $TAREQ-* 2>/dev/null`
		F=0
		for j in $TRES
		do
			if test `completetoname $j` != $TAREQ
			then
				continue
			else
				if test -z "$TAOP"
				then
					F=1
					break
				else
					TEMPV3=`completetoversion $j`
					if test "`comp $TAOP $TEMPV3 $TAVER`" -eq 1
					then
						F=1
						break
					fi
				fi
			fi
		done
		
		if test $F -eq 0
		then
			OLDSEARCH="$OLDSEARCH $TAREQ "
			
			__search "^$TAREQ$" "$TAOP" "$TAVER"
			
			if test "$COUNTER" -eq 0
			then
				echo "unsolved dependency: $TAREQ $TAOP $TAVER"
				exit 1
			fi
			
			NUM=0
			TCON="${CONFLICTS[$NUM]}"
			
			__depcheck "${DEP[$NUM]}"
			
			AREQ[$ARI]=$TAREQ
			AVER[$ARI]=$TAVER
			AOP[$ARI]=$TAOP
			AACON[$ARI]="$TCON"
			ARI=$(( $ARI + 1 ))
		fi

	done
}


__install(){
	local PACKAGE="$1"
	local OPERATOR="$2"
	local V="$3"
	
	__search "^$PACKAGE$" "$OPERATOR" "$V"
	
	if test $COUNTER -gt 0
	then
		NUM=0
		PACKAGE="${RES[$NUM]}"
		
		__download $PACKAGE ${RESREP[$NUM]}
		
		__build $PACKAGE
		
		cd $CALLDIR
	
		upgradepkg --install-new $FILE
		
		clean $PACKAGE
		
		echo "$PACKAGE: Installation successfull"
		
		return 0
	else
		echo "$PACKAGE not found"
		return 1
	fi
}


__depinstall(){
	for ((i = 0; i  < ${#AREQ[@]}; i++))
	do
		if test "${AREQ[$i]}"
		then
			if ! __install ${AREQ[$i]} ${AOP[$i]} ${AVER[$i]}
			then
				exit 1
			fi
		fi
	done
}


__suggestsinstall(){
	for ((i = 0; i  < ${#ASUG[@]}; i++))
	do
		if test "${ASUG[$i]}"
		then
			 __install ${ASUG[$i]}
		fi
	done
}

#input:
#AREQ array with package name dependencies
#AVER array with versions
#AOP array with operators
#ARI array index
__depask(){
	if test "$ARI" && test $ARI -gt 0
	then
		echo "$PACKAGE has these unsatisfied dependencies:"
		echo "${AREQ[@]}"
		cd $CALLDIR
		
		if test "$MUTE" && test $MUTE -eq 0
		then
			for ((i = 0; i  < ${#AREQ[@]}; i++))
			do
				echo -n "Should I build and install ${AREQ[$i]}? [yes] "
				read j
				if ! ( test -z "$j" || test "$j" = yes || test "$j" = y || test "$j" = Y || test "$j" = Yes )
				then
					AREQ[$i]=""
				fi
			done				
		fi
	fi
}



__conflictsask(){
	if test "$CRI" && test $CRI -gt 0
	then
		echo "$PACKAGE or its dependencies conflict with these packages:"
		echo ${FCON[@]}
		
		if test "$MUTE" && test $MUTE -eq 0
		then
			echo -n "Should I continue anyway? [No] "
			read j
			if test -z "$j" || test "$j" = no || test "$j" = n || test "$j" = N || test "$j" = No
			then
				exit 0
			fi
		else
			exit 0
		fi
	fi
}


__suggestsask(){
	if test "$SRI" && test $SRI -gt 0
	then
		echo "These other packages are suggested:"
		echo ${ASUG[@]}
		
		if test "$MUTE" && test $MUTE -eq 0
		then
			for ((i = 0; i  < ${#ASUG[@]}; i++))
			do
				echo -n "Should I build and install ${ASUG[$i]}? [yes] "
				read j
				if ! (test -z "$j" || test "$j" = yes || test "$j" = y || test "$j" = Y || test "$j" = Yes)
				then
					$ASUG[$i]=""
				fi				
			done
		fi
	fi
}


####
####

update(){
	cd /var/slackbuilder
	
	for ((i = 0; i < ${#REP[@]}; i++))
	do
		rm "${FILELIST[$i]}.list" &>/dev/null
		if ! ${WGET[$i]} ${FILELIST[$i]}.list ${REP[$i]}/FILELIST.TXT 2>/dev/null
		then
			if ! ${WGET[$i]} ${FILELIST[$i]}.list ${REP[$i]}/FILE_LIST 2>/dev/null
			then
				echo "${REP[$i]} error: no FILELIST.TXT or FILE_LIST"
				rm ${FILELIST[$i]}.list &>/dev/null
				continue
			fi
		fi
		rm "${FILELIST[$i]}.md5" &>/dev/null
		${WGET[$i]} ${FILELIST[$i]}.md5 ${REP[$i]}/CHECKSUMS.md5 2>/dev/null
		rm "${FILELIST[$i]}.sources" &>/dev/null
		if ! ${WGET[$i]} ${FILELIST[$i]}.sources ${REP[$i]}/SOURCES.TXT 2>/dev/null
		then
			rm  ${FILELIST[$i]}.sources 2>/dev/null
			TEMP="`grep -E '[sS]lack[bB]uild$|\.[bB]uild$' ${FILELIST[$i]}.list | awk '{print$NF}'`"
			rm ${FILELIST[$i]}.packages &>/dev/null
			for j in $TEMP
			do
				TT=`basename $j`
				if [[ $TT = *.[sS]lack[Bb]uild* ]]
				then
					TT=${TT%.[s,S]lack[b,B]uild}
				elif [[ $TT = *.[bB]uild ]]
				then
					TT=${TT%.[bB]uild}
				else
					TT=`dirname $j`
					TT=`basename $TT`
				fi
				echo "$TT" >> ${FILELIST[$i]}.packages
			done
			mv ${FILELIST[$i]}.packages ${FILELIST[$i]}.packages.old
			cat ${FILELIST[$i]}.packages.old | uniq > ${FILELIST[$i]}.packages
			rm ${FILELIST[$i]}.packages.old &>/dev/null
		else
			cat ${FILELIST[$i]}.sources | awk '{print$1}' > ${FILELIST[$i]}.packages
		fi
	done
	echo "All done!"
}

search(){

	__search "$PACKAGE" "$OPERATOR" "$V"
	
	echo "From ${REP[${RESREP[0]}]}"
	echo "${RES[0]}    ${RESVERSION[0]}"
	oldi=0
	for ((i = 1; i  < $COUNTER; i++))
	do
		if test ${RESREP[$i]} -ne ${RESREP[$oldi]}
		then
			echo -e "\nFrom ${REP[${RESREP[$i]}]}"
		fi
		echo "${RES[$i]}    ${RESVERSION[$i]}"
		oldi=$i
	done
	echo ""
}


get(){

	cd /var/slackbuilder
	
	__search "$PACKAGE" "$OPERATOR" "$V"
	
	__ask
	
	PACKAGE=${RES[$NUM]}
	
	__download $PACKAGE ${RESREP[$NUM]}
	
	echo "$PACKAGE sources downloded in $TMP/$PACKAGE"
	__exit 0 $PACKAGE
}


build(){
	cd /var/slackbuilder
	
	__search "$PACKAGE" "$OPERATOR" "$V"
	REGEXP="-e"
	
	__ask
	
	TDEP="${DEP[$NUM]}"
	PACKAGE=${RES[$NUM]}
	NUMP=${RESREP[$NUM]}
	
	if test "$NODEP" && test $NODEP -eq 0
	then
		ARI=0
		CRI=0
		
		__depcheck "$TDEP"
		__depask
		
		__conflictscheck
		__conflictsask
	fi
	
	__depinstall
	
	__download $PACKAGE $NUMP
	
	__build $PACKAGE
	
	__exit 0 $PACKAGE
}

install(){

	cd /var/slackbuilder
	
	__search "$PACKAGE" "$OPERATOR" "$V"
	REGEXP="-e"
	
	__ask
	
	TSUG="${SUGGESTS[$NUM]}"
	TCON="${CONFLICTS[$NUM]}"
	TDEP="${DEP[$NUM]}"
	PACKAGE=${RES[$NUM]}
	NUMP=${RESREP[$NUM]}
	
	if test "$NODEP" && test $NODEP -eq 0
	then	
		ARI=0
		CRI=0
		SRI=0
		
		__depcheck "$TDEP"
		__depask
		
		__suggestscheck "$TSUG"
		__suggestask
		
		__conflictscheck "$TCON"
		__conflictsask
	fi
	
	__depinstall
	
	__download $PACKAGE $NUMP
	
	__build $PACKAGE
	
	cd $CALLDIR

	upgradepkg --install-new $FILE
	
	echo "Installation successfull"
	
	__suggestsinstall
	
	__exit 0 $PACKAGE
}



###########################################################################################################################
#
#				CODE START
#
###########################################################################################################################


__checks

#parse config
if test ! -f /etc/slackbuilder.conf
then
	echo "/etc/slackbuilder.conf not found"
	exit 1
fi
ALLREP=`egrep "^SOURCE=" /etc/slackbuilder.conf `
if test -z "$ALLREP"
then
	echo "no repositories configured"
	exit 1
fi
IN=0
for i in $ALLREP
do
	REP[$IN]=${i#SOURCE=}
	if [[ ! ${REP[$IN]} = */ ]]
	then
		REP[$IN]=${REP[$IN]}/
	fi
 	if test "`echo ${REP[$IN]} | grep 'file://'`"
	then
		WGET[$IN]="__copy"
		REP[$IN]=${REP[$IN]#file://}
	else
		WGET[$IN]="wget -c -O"
	fi
	FILELIST[$IN]="`echo ${REP[$IN]} | tr \/ \#`"
	IN=$(( $IN + 1 ))
done	
ALLEXCLUDE=`egrep "^EXCLUDE="  /etc/slackbuilder.conf`
if test "$ALLEXCLUDE"
then
	IN=0
	for i in $ALLEXCLUDE
	do
		EXCLUDE[$IN]=${i#EXCLUDE=}
		IN=$(( $IN + 1 ))
	done
fi
ARCH=`egrep "^ARCH=" /etc/slackbuilder.conf`
if test "$ARCH"
then
	export ARCH=${ARCH#ARCH=}
fi
SLKCFLAGS=`egrep "^SLKCFLAGS="  /etc/slackbuilder.conf`
if test "$SLKCFLAGS"
then
	export SLKCFLAGS=${SLKCFLAGS#SLKCFLAGS=}
fi

#help
if test $# -lt 1
then
	__help
fi

#parameters check
REGEXP=""
MUTE=0
NODEP=0
CLEAN=1
NOREQUIREDBUILDER=0
MD5=1
VERSIONS=0
NOSOURCES=0
while test $# -ge 1
do
  if [ "$1" = "--update" ]; then
    ACTION=update
    shift 1
  elif [ "$1" = "--build" ]; then
    ACTION=build
    shift 1
    PACKAGE=$1;
    shift 1
  elif [ "$1" = "--search" ]; then
    ACTION=search
    shift 1
    PACKAGE=$1;
    shift 1
  elif [ "$1" = "--install" ]; then
    ACTION=install
    shift 1
    PACKAGE=$1;
    shift 1
  elif [ "$1" = "--get" ]; then
    ACTION=get
    CLEAN=0
    shift 1
    PACKAGE=$1;
    shift 1
  elif [ "$1" = "--mute" ]; then
    MUTE=1
    shift 1
  elif [ "$1" = "--no-dep" ]; then
    NODEP=1
    shift 1
  elif [ "$1" = "--regexp" ]; then
    REGEXP="-e"
    shift 1
  elif [ "$1" = "--noclean" ]; then
    CLEAN=0
    shift 1
  elif [ "$1" = "--no-requiredbuilder" ]; then
    NOREQUIREDBUILDER=1
    shift 1
  elif [ "$1" = "--no-md5" ]; then
    MD5=0
    shift 1
  elif [ "$1" = "--no-sources" ]; then
    NOSOURCES=1
    shift 1
  elif [ "$1" = "--versions" ]; then
    VERSIONS=1
    shift 1
  elif [ "$1" = "-gt" ]; then
    OPERATOR=gt
    shift 1
    V="$1"
    shift 1
  elif [ "$1" = "-ge" ]; then
    OPERATOR=ge
    shift 1
    V="$1"
    shift 1
  elif [ "$1" = "-lt" ]; then
    OPERATOR=lt
    shift 1
    V="$1"
    shift 1
  elif [ "$1" = "-le" ]; then
    OPERATOR=le
    shift 1
    V="$1"
    shift 1
  elif [ "$1" = "-eq" ]; then
    OPERATOR=eq
    shift 1
    V="$1"
    shift 1
  elif [ "$1" = "-" ]; then
    shift 1
  else
    __help
  fi
done

OLDPACKAGE="$OLDPACKAGE $PACKAGE"


#action
if test ! -d /var/slackbuilder
then
	mkdir /var/slackbuilder
fi
if test ! -d /tmp/slackbuilder
then
	mkdir /tmp/slackbuilder
fi
CALLDIR="`pwd`"
TMP=/tmp/slackbuilder

$ACTION

cd $CALLDIR