#!/bin/sh

case "${1}" in
	custom)
		# Configure custom sources.list
		case "${LIVE_DISTRIBUTION}" in
			stable|"${CODENAME_STABLE}"|testing|"${CODENAME_TESTING}")
				echo "deb ${LIVE_MIRROR} ${LIVE_DISTRIBUTION} ${LIVE_SECTIONS}" > "${LIVE_CHROOT}"/etc/apt/sources.list

				if [ "${LIVE_SOURCE}" = "yes" ]
				then
					echo "deb-src ${LIVE_MIRROR} ${LIVE_DISTRIBUTION} ${LIVE_SECTIONS}" >> "${LIVE_CHROOT}"/etc/apt/sources.list
				fi

				echo "deb ${LIVE_MIRROR_SECURITY} ${LIVE_DISTRIBUTION}/updates ${LIVE_SECTIONS}" >> "${LIVE_CHROOT}"/etc/apt/sources.list

				if [ "${LIVE_SOURCE}" = "yes" ]
				then
					echo "deb-src ${LIVE_MIRROR_SECURITY} ${LIVE_DISTRIBUTION}/updates ${LIVE_SECTIONS}" >> "${LIVE_CHROOT}"/etc/apt/sources.list
				fi
				;;

			unstable|"${CODENAME_UNSTABLE}")
				echo "deb ${LIVE_MIRROR} unstable ${LIVE_SECTIONS}" > "${LIVE_CHROOT}"/etc/apt/sources.list

				if [ "${LIVE_SOURCE}" = "yes" ]
				then
					echo "deb-src ${LIVE_MIRROR} unstable ${LIVE_SECTIONS}" >> "${LIVE_CHROOT}"/etc/apt/sources.list
				fi

				if [ "${LIVE_DISTRIBUTION_EXPERIMENTAL}" = "yes" ]
				then
					echo "deb ${LIVE_MIRROR} experimental ${LIVE_SECTIONS}" >> "${LIVE_CHROOT}"/etc/apt/sources.list

					if [ "${LIVE_SOURCE}" = "yes" ]
					then
						echo "deb-src ${LIVE_MIRROR} experimental ${LIVE_SECTIONS}" >> "${LIVE_CHROOT}"/etc/apt/sources.list
					fi

cat > "${LIVE_CHROOT}"/etc/apt/preferences << EOF
Package: *
Pin: release a=experimental
Pin-Priority: 999
EOF
				fi
				;;
		esac
		;;

	default)
		# Configure default sources.list
		case "${LIVE_DISTRIBUTION}" in
			oldstable|"${CODENAME_OLDSTABLE}"|stable|"${CODENAME_STABLE}"|testing|"${CODENAME_TESTING}")
				echo "deb http://ftp.debian.org/debian/ ${LIVE_DISTRIBUTION} ${LIVE_SECTIONS}" > "${LIVE_CHROOT}"/etc/apt/sources.list

				if [ "${LIVE_SOURCE}" = "yes" ]
				then
					echo "deb-src http://ftp.debian.org/debian/ ${LIVE_DISTRIBUTION} ${LIVE_SECTIONS}" >> "${LIVE_CHROOT}"/etc/apt/sources.list
				fi

				echo "deb http://security.debian.org/ ${LIVE_DISTRIBUTION}/updates ${LIVE_SECTIONS}" >> "${LIVE_CHROOT}"/etc/apt/sources.list

				if [ "${LIVE_SOURCE}" = "yes" ]
				then
					echo "deb-src http://security.debian.org/ ${LIVE_DISTRIBUTION}/updates ${LIVE_SECTIONS}" >> "${LIVE_CHROOT}"/etc/apt/sources.list
				fi
				;;

			unstable|"${CODENAME_UNSTABLE}")
				echo "deb http://ftp.debian.org/debian/ unstable ${LIVE_SECTIONS}" > "${LIVE_CHROOT}"/etc/apt/sources.list

				if [ "${LIVE_SOURCE}" = "yes" ]
				then
					echo "deb-src http://ftp.debian.org/debian/ unstable ${LIVE_SECTIONS}" >> "${LIVE_CHROOT}"/etc/apt/sources.list
				fi

				if [ "${LIVE_DISTRIBUTION_EXPERIMENTAL}" = "yes" ]
				then
					echo "deb http://ftp.debian.org/debian/ experimental ${LIVE_SECTIONS}" >> "${LIVE_CHROOT}"/etc/apt/sources.list

					if [ "${LIVE_SOURCE}" = "yes" ]
					then
						echo "deb-src http://ftp.debian.org/debian/ experimental ${LIVE_SECTIONS}" >> "${LIVE_CHROOT}"/etc/apt/sources.list
					fi
				fi
				;;
		esac
		;;
esac

# Add custom repositories
echo "" >> "${LIVE_CHROOT}"/etc/apt/sources.list
echo "# Custom repositories" >> "${LIVE_CHROOT}"/etc/apt/sources.list

for NAME in ${LIVE_REPOSITORIES}
do
	eval REPOSITORY="$`echo LIVE_REPOSITORY_$NAME`"
	eval REPOSITORY_DISTRIBUTION="$`echo LIVE_REPOSITORY_DISTRIBUTION_$NAME`"
	eval REPOSITORY_SECTIONS="$`echo LIVE_REPOSITORY_SECTIONS_$NAME`"

	# Configure /etc/apt/sources.list
	if [ -n "${REPOSITORY_DISTRIBUTION}" ]
	then
		echo "deb ${REPOSITORY} ${REPOSITORY_DISTRIBUTION} ${REPOSITORY_SECTIONS}" >> "${LIVE_CHROOT}"/etc/apt/sources.list
	else
		echo "deb ${REPOSITORY} ${LIVE_DISTRIBUTION} ${REPOSITORY_SECTIONS}" >> "${LIVE_CHROOT}"/etc/apt/sources.list
	fi
done

# Update indices
if [ "${2}" = "initial" ]
then
	lh_chroot "apt-get update"
else
	lh_chroot "aptitude update"
fi

if [ "${LIVE_DISTRIBUTION_EXPERIMENTAL}" = "yes" ]
then
	# experimental is sometimes broken,
	# therefore this is intentionally kept interactive.
	lh_chroot "aptitude upgrade" || return 0
	lh_chroot "aptitude dist-upgrade" || return 0
fi