###
### load up common variables and functions
###
dbc_go(){
	local prev_dbc_upgrade importing_from_non_dbc
	. /usr/share/dbconfig-common/dpkg/common
	dbc_config $@
	# read in debconf responses (which are seeded from the config)
	dbc_read_package_debconf $@
	# and write them to file.
	dbc_write_package_config

	###
	### begin main code execution
	###
	if [ "$dbc_command" = "configure" -o "$dbc_command" = "reconfigure" ]; 
	then
		###
		### if they don't want our help, quit
		###
		if [ "$dbc_install" != "true" ]; then return 0; fi

		###
		### perform a few sanity checks on the data
		###

		# if dbserver is unset, that means localhost.  
		if [ -z "$dbc_dbserver" ]; then 
			# if the server is local, only allow connections from localhost
			dbc_dballow="localhost"
		else
			# otherwise, only tell the remote database to allow from us
			dbc_dballow=`hostname`
		fi

		# export the config file if it exists, for the scripts
		if [ "$dbc_generate_include" ]; then
			# strip the leading format string for convenience
			dbc_config_include=`echo "$dbc_generate_include" | sed -e 's/^[^:]*://'`
			export dbc_generate_include
			export dbc_config_include
		fi

		# now, determine if we're upgrading from a non-dbc version.  if so,
		# there's a bunch of stuff that we do NOT want to do
		if [ "$dbc_first_version" ] && [ "$dbc_oldversion" ]; then
			if dpkg --compare-versions "$dbc_first_version" gt "$dbc_oldversion"; then
				importing_from_non_dbc="true"
			fi
		fi

		# don't perform the following block of code during upgrades
		if [ ! "${importing_from_non_dbc}" ]; then
			###
			### now, create the app user account
			###
			$dbc_createuser_cmd || dbc_install_error "creating user"
			[ "$dbc_tried_again" ] && return 0
	
			###
			### create the database
			###
			$dbc_createdb_cmd || dbc_install_error "creating database"
			[ "$dbc_tried_again" ] && return 0
	
			###
			### populate the database
			###
			# sqlfile is the file to use for installing the database, this is
			dbc_sqlfile=$dbc_share/data/$dbc_package/install/$dbc_dbtype
			dbc_sqlfile_adm=$dbc_share/data/$dbc_package/install-dbadmin/$dbc_dbtype
			dbc_scriptfile=$dbc_share/scripts/$dbc_package/install/$dbc_dbtype
			if [ -f "$dbc_scriptfile" ]; then
				if ! sh -c "$dbc_scriptfile $*"; then
					dbc_error="$dbc_scriptfile exited with non-zero status"
					dbc_install_error "populating database"
					[ "$dbc_tried_again" ] && return 0
				fi
			fi
			
			if [ -f "$dbc_sqlfile_adm" ]; then
				$dbc_sqlfile_cmd $dbc_sqlfile_adm || dbc_install_error "populating database with administrative sql"
				[ "$dbc_tried_again" ] && return 0
			fi
			
			if [ -f "$dbc_sqlfile" ]; then
				_dbc_asuser="yes"
				$dbc_sqlfile_cmd $dbc_sqlfile || dbc_install_error "populating database"
				_dbc_asuser=""
				[ "$dbc_tried_again" ] && return 0
			fi
		fi	

	# end install/reconfigure section

		if [ "$importing_from_non_dbc" ]; then
			if ! $dbc_checkuser_command; then
				upgrade_error "importing dbconfig-settings"
				[ "$dbc_tried_again" ] && return 0
			fi
		fi

	# begin upgrade section

		if [ "$dbc_oldversion" ]; then
			# check for new upgrades in these two locations
			_dbc_upgradedir=$dbc_share/data/$dbc_package/upgrade/$dbc_dbtype
			_dbc_upgradedir_adm=$dbc_share/data/$dbc_package/upgrade-dbadmin/$dbc_dbtype
			_dbc_scriptupgradedir=$dbc_share/scripts/$dbc_package/upgrade/$dbc_dbtype

			if [ -d $_dbc_scriptupgradedir ]; then
				for _dbc_f in `find $_dbc_scriptupgradedir -type f`; do
					_dbc_newvers=`basename $_dbc_f`
					if dpkg --compare-versions $dbc_oldversion lt $_dbc_newvers; then
						_dbc_scriptupgrades_pending="$_dbc_scriptupgrades_pending $_dbc_f"
					fi
				done
			fi
			
			if [ -d $_dbc_upgradedir_adm ]; then
				_dbc_asuser=""
				for _dbc_f in `find $_dbc_upgradedir -type f`; do
					_dbc_newvers=`basename $_dbc_f`
					if dpkg --compare-versions $dbc_oldversion lt $_dbc_newvers; then
						_dbc_upgrades_pending_adm="$_upgrades_pending_adm $_dbc_f"
					fi
				done
			fi
			
			if [ -d $_dbc_upgradedir ]; then
				_dbc_asuser="yes"
				for _dbc_f in `find $_dbc_upgradedir -type f`; do
					_dbc_newvers=`basename $_dbc_f`
					if dpkg --compare-versions $dbc_oldversion lt $_dbc_newvers; then
						_dbc_upgrades_pending="$_upgrades_pending $_dbc_f"
					fi
				done
				_dbc_asuser=""
			fi

			# if there are any upgrades to be applied
			if [ "$_dbc_upgrades_pending" -o "$_dbc_scriptupgrades_pending" -o "$_dbc_upgrades_pending_adm" ]; then

				# ask if they want our help in the process at all
				prev_dbc_upgrade="$dbc_upgrade"
				db_fset $dbc_package/dbconfig-upgrade seen false
				db_input high $dbc_package/dbconfig-upgrade || true
				db_go || true
				db_get $dbc_package/dbconfig-upgrade && dbc_upgrade="$RET"

				# if their answer has changed, synchronize config to disk
				if [ "$prev_dbc_upgrade" != "$dbc_upgrade" ]; then
					dbc_write_package_config
				fi

				# and if they don't want our help, we'll go away
				if [ "$dbc_upgrade" != "true" ]; then return 0; fi

				###
				### perform a few sanity checks on the data
				###

				# this is the file into which upgrade backups go
				_dbc_dbfile=/var/cache/dbconfig-common/backups/${dbc_package}_${dbc_oldversion}.$dbc_dbtype	
				echo dbconfig-common: creating database backup in $_dbc_dbfile >&2
				# backup before we upgrade
				_dbc_asuser=""
				$dbc_dump_cmd > $_dbc_dbfile
			fi

			# now go through script updates
			for _dbc_f in $_dbc_scriptupgrades_pending; do
				_dbc_newvers=`basename $_dbc_f`
				# execute the script
				echo dbconfig-common: upgrading with $_dbc_f >&2
				if !  sh -c "$_dbc_f $*"; then
					dbc_error="$_dbc_f exited with non-zero status"
					dbc_upgrade_error "processing $_dbc_f"
					[ "$dbc_tried_again" ] && return 0
				fi
			done
			
			# now go through the pending admin sql upgrades, one by one
			for _dbc_f in $_dbc_upgrades_pending_adm; do
				_dbc_asuser=""
				# execute the script
				echo dbconfig-common: upgrading with $_dbc_f >&2
				$dbc_sqlfile_cmd $_dbc_f || dbc_upgrade_error "processing $_dbc_f"
				[ "$dbc_tried_again" ] && return 0
			done
			
			# now go through the pending sql upgrades, one by one
			for _dbc_f in $_dbc_upgrades_pending; do
				# execute the script
				_dbc_asuser="yes"
				echo dbconfig-common: upgrading with $_dbc_f >&2
				$dbc_sqlfile_cmd $_dbc_f || dbc_upgrade_error "processing $_dbc_f"
				[ "$dbc_tried_again" ] && return 0
				_dbc_asuser=""
			done

		fi
	# end upgrade section
	fi

	# don't forget to clean up after ourselves
	dbc_postinst_cleanup
}
