#!/bin/bash 

name="===>"
export portdir=`pwd`

test -e "/etc/ports" && source /etc/ports || exit 1
test "$distfiles" = "" && distfiles=$ports_dir/internals/distfiles/

test -e "$ports_dir/internals/functions.sh" || error $MISSING_FUNCTIONS
source $ports_dir/internals/functions.sh

parse_arguments "$@"

test "$ports_backend" = "" && error $FIX_CONF

if test "$needinstall" = "yes" ; then

	source_conf
	
	make_skel 2> /dev/null

	echo -e "#!/bin/sh\n$pkg_conf" > $portdir/build/configure.sh
	chmod 700 $portdir/build/configure.sh
	export pkg_conf="$portdir/build/configure.sh"
	echo -e "#!/bin/sh\n$pkg_make_install" > $portdir/build/install_script.sh
	chmod 700 $portdir/build/install_script.sh
	export pkg_make_install="$portdir/build/install_script.sh"

	if test "$force_install" != "yes" ; then
		source $ports_dir/internals/backends/$ports_backend/is-installed
	fi
	
	solve_deps

	get_file
	checksum
#	get_patches
		
	notes=$PWD/notes
	
	test "`echo $pkg_filename | sed -e s/.*tar.gz/gz/`" = gz  && pkg_unzip="gzip -c -d"
	test "`echo $pkg_filename | sed -e s/.*tar.bz2/bz2/`" = bz2  && pkg_unzip="bzip2 -c -d"
	cd $portdir/build
	printf "%s extracting %s ..." "$name" "$pkg_name"
	test -d "$portdir/build/$pkg_dir_intar" || cat "$distfiles/$pkg_filename" | $pkg_unzip | tar xf -
	test "$?" = 0 || error $COULD_NOT_UNPACK
	test -d "$portdir/build/$pkg_dir_intar" || error $COULD_NOT_UNPACK
	
	cd $portdir/build/$pkg_dir_intar || error $COULD_NOT_UNPACK
	printf " [done]\n"
	
	apply_patches
	
	echo -e "#!/bin/sh\n$pkg_make" > $portdir/build/make.sh
	
	chmod 700 $portdir/build/make.sh
	pkg_make="$portdir/build/make.sh" 
	export pkg_make
	build

	test -e "$portdir/build/make.sh" && rm $portdir/build/make.sh
	test -e "$portdir/build/configure.sh" && rm $portdir/build/configure.sh
	
	$ports_dir/internals/backends/$ports_backend/install
	test -e "$portdir/build/install_script.sh" && rm "$portdir/build/install_script.sh"
		
	clean_up_heap
	
	oldpwd=`pwd`
	
	cd $portdir/heap
	export ports_dir
	
	$ports_dir/internals/backends/$ports_backend/build-pkg
	
	cd $oldpwd
	
	if test "$onlyneedpackage" != "yes" ; then
		source $ports_dir/internals/backends/$ports_backend/install-pkg
	fi
	
	if test -e "./notes" ; then 
		cat ./notes 
	fi

elif test "$needremove" = "yes" ; then
	source_conf
	source $ports_dir/internals/backends/$ports_backend/remove-pkg

elif  test "$needhome" = "yes" ; then
	test -e "./conf" && export pkg_name=`get_xml ./conf port information name` || error $MISSING_CONF
	echo "Hompage for package $pkg_name [ using : $browser ]"
	$browser "$pkg_homepage"
elif test "$needsb" = "yes" ; then
	show_backends
elif test "$needinstalllist" = "yes" ; then
	install_pkg_list $installlist_filename
elif test "$needupdated" = "yes" ; then
	if test "$rec_updated" = "yes" ; then
		for files in `find ./ -name conf -type f`; do
			is_upto_date $files
		done
	else
		is_upto_date
	fi
		
elif test "$needcleanup" = "yes" ; then
	if test "$rec_cleanup" = "yes"; then
		find $portdir/* -name conf -type f -maxdepth 5 -exec $0 -c rc {} \; 
	else
		clean_up
	fi
elif test "$needsearch" = "yes" ; then
	for files in `find $portdir -name conf -type f`; do
		search $files $topic
	done
elif test "$needhelp" = "yes" ; then
	echo "Usage: 
cd $ports_dir/section/port/... 
pkg_ports install|-i|--install | installs a port [ argument force ]
pkg_ports install-list|-l|--install-list | installs a port-list next argument has to be an filename
pkg_ports remove|-r|--remove | removes a port ( via backend )
pkg_ports homepage|-h|--homepage | start $browser to browse the projekt homepage
pkg_ports show-backends|-s|--show-backends | shows all avail backends
pkg_ports package-only|-p|--package-only | just build the package perform no install
pkg_ports cleanup|-c|--clean-up | cleans up temp and downloaded files [ argument recursive ]
pkg_ports search|-s|--search | searches ports [desc and name]
pkg_ports updated|-u|--updated | shows version differences between installed version and version in 
ports. [ argument recursive ]"
fi
