#!/bin/bash

curdir=$(readlink -e $(dirname $0))
. "${curdir}/lib/common.inc"

#
# self test
#

testPackages() {
	local p
	for p in "${pkgdir}"/*; do
		pushd $p >/dev/null
		namcap *.pkg.tar.* || fail 'namcap failed'
		popd >/dev/null
	done
}


#
# db-update
#

testAddSimplePackages() {
	local arches=('i686' 'x86_64')
	local pkgs=('pkg-simple-a' 'pkg-simple-b')
	local pkgbase
	local arch

	for pkgbase in ${pkgs[@]}; do
		for arch in ${arches[@]}; do
			releasePackage extra ${pkgbase} ${arch}
		done
	done

	../db-update extra

	for pkgbase in ${pkgs[@]}; do
		for arch in ${arches[@]}; do
			checkPackage extra ${pkgbase}-1-1-${arch}.pkg.tar.xz ${arch}
		done
	done
}

testAddAnyPackages() {
	local pkgs=('pkg-any-a' 'pkg-any-b')
	local pkgbase

	for pkgbase in ${pkgs[@]}; do
		releasePackage extra ${pkgbase} any
	done

	../db-update extra

	for pkgbase in ${pkgs[@]}; do
		checkAnyPackage extra ${pkgbase}-1-1-any.pkg.tar.xz
	done
}

testAddSplitPackages() {
	local arches=('i686' 'x86_64')
	local pkgs=('pkg-split-a' 'pkg-split-b')
	local pkg
	local pkgbase
	local arch

	for pkgbase in ${pkgs[@]}; do
		for arch in ${arches[@]}; do
			releasePackage extra ${pkgbase} ${arch}
		done
	done

	../db-update extra

	for pkgbase in ${pkgs[@]}; do
		for arch in ${arches[@]}; do
			for pkg in "${pkgdir}/${pkgbase}"/*-${arch}.pkg.tar.*; do
				checkPackage extra $(basename ${pkg}) ${arch}
			done
		done
	done
}


#
# db-remove
#

testRemovePackages() {
	local arches=('i686' 'x86_64')
	local pkgs=('pkg-simple-a' 'pkg-simple-b' 'pkg-split-a' 'pkg-split-b')
	local pkgbase
	local arch

	for pkgbase in ${pkgs[@]}; do
		for arch in ${arches[@]}; do
			releasePackage extra ${pkgbase} ${arch}
		done
	done

	../db-update extra

	for pkgbase in ${pkgs[@]}; do
		for arch in ${arches[@]}; do
			../db-remove ${pkgbase} extra ${arch}
		done
	done

	for pkgbase in ${pkgs[@]}; do
		for arch in ${arches[@]}; do
			checkRemovedPackage extra ${pkgbase} ${arch}
		done
	done
}

testRemoveAnyPackages() {
	local pkgs=('pkg-any-a' 'pkg-any-b')
	local pkgbase

	for pkgbase in ${pkgs[@]}; do
		releasePackage extra ${pkgbase} any
	done

	../db-update extra

	for pkgbase in ${pkgs[@]}; do
		../db-remove ${pkgbase} extra any
	done

	for pkgbase in ${pkgs[@]}; do
		checkRemovedAnyPackage extra ${pkgbase}
	done
}


#
# db-move
#

testMoveSimplePackages() {
	local arches=('i686' 'x86_64')
	local pkgs=('pkg-simple-a' 'pkg-simple-b')
	local pkgbase
	local arch

	for pkgbase in ${pkgs[@]}; do
		for arch in ${arches[@]}; do
			releasePackage testing ${pkgbase} ${arch}
		done
	done

	../db-update testing

	for arch in ${arches[@]}; do
		../db-move pkg-simple-a testing extra ${arch}
	done

	for arch in ${arches[@]}; do
		checkPackage extra pkg-simple-a-1-1-${arch}.pkg.tar.xz ${arch}
		checkRemovedPackage testing pkg-simple-a-1-1-${arch}.pkg.tar.xz ${arch}

		checkPackage testing pkg-simple-b-1-1-${arch}.pkg.tar.xz ${arch}
	done
}

testMoveAnyPackages() {
	local pkgs=('pkg-any-a' 'pkg-any-b')
	local pkgbase

	for pkgbase in ${pkgs[@]}; do
		releasePackage testing ${pkgbase} any
	done

	../db-update testing
	../db-move pkg-any-a testing extra any

	checkAnyPackage extra pkg-any-a-1-1-any.pkg.tar.xz
	checkRemovedAnyPackage testing pkg-any-a
	checkAnyPackage testing pkg-any-b-1-1-any.pkg.tar.xz
}

testMoveSplitPackages() {
	local arches=('i686' 'x86_64')
	local pkgs=('pkg-split-a' 'pkg-split-b')
	local pkg
	local pkgbase
	local arch

	for pkgbase in ${pkgs[@]}; do
		for arch in ${arches[@]}; do
			releasePackage testing ${pkgbase} ${arch}
		done
	done

	../db-update testing

	for arch in ${arches[@]}; do
		../db-move pkg-split-a testing extra ${arch}
	done

	for arch in ${arches[@]}; do
		for pkg in "${pkgdir}/pkg-split-a"/*-${arch}.pkg.tar.*; do
			checkPackage extra $(basename ${pkg}) ${arch}
		done
	done
	for arch in ${arches[@]}; do
		for pkg in "${pkgdir}/pkg-split-b"/*-${arch}.pkg.tar.*; do
			checkPackage testing $(basename ${pkg}) ${arch}
		done
	done

	checkRemovedAnyPackage testing pkg-split-a
}


#
# ftpdir-cleanup
#

testCleanupSimplePackages() {
	local arches=('i686' 'x86_64')
	local pkgs=('pkg-simple-a' 'pkg-simple-b')
	local pkgbase
	local arch

	for pkgbase in ${pkgs[@]}; do
		for arch in ${arches[@]}; do
			releasePackage extra ${pkgbase} ${arch}
		done
	done

	../db-update extra

	for arch in ${arches[@]}; do
		../db-remove pkg-simple-a extra ${arch}
	done

	../cron-jobs/ftpdir-cleanup

	for arch in ${arches[@]}; do
		local pkg1="pkg-simple-a-1-1-${arch}.pkg.tar.xz"
		[ -f "${FTP_BASE}/packages/arch/${arch}/${pkg1}" ] && fail "packages/arch/${arch}/${pkg1} found"
		[ -f "${FTP_BASE}/${repo}/os/${arch}/${pkg1}" ] && fail "${repo}/os/${arch}/${pkg1} found"

		local pkg2="pkg-simple-b-1-1-${arch}.pkg.tar.xz"
		checkPackage extra ${pkg2} ${arch}
	done
}

testCleanupAnyPackages() {
	local pkgs=('pkg-any-a' 'pkg-any-b')
	local pkgbase
	local arch='any'

	for pkgbase in ${pkgs[@]}; do
		releasePackage extra ${pkgbase} any
	done

	../db-update extra
	../db-remove pkg-any-a extra any
	../cron-jobs/ftpdir-cleanup

	local pkg1='pkg-any-a-1-1-any.pkg.tar.xz'
	[ -f "${FTP_BASE}/packages/arch/${arch}/${pkg1}" ] && fail "packages/arch/${arch}/${pkg1} found"
	[ -f "${FTP_BASE}/${repo}/os/${arch}/${pkg1}" ] && fail "${repo}/os/${arch}/${pkg1} found"

	local pkg2="pkg-any-b-1-1-${arch}.pkg.tar.xz"
	checkAnyPackage extra ${pkg2}
}

testCleanupSplitPackages() {
	local arches=('i686' 'x86_64')
	local pkgs=('pkg-split-a' 'pkg-split-b')
	local pkg
	local pkgbase
	local arch

	for pkgbase in ${pkgs[@]}; do
		for arch in ${arches[@]}; do
			releasePackage extra ${pkgbase} ${arch}
		done
	done

	../db-update extra

	for pkgbase in ${pkgs[@]}; do
		for arch in ${arches[@]}; do
			../db-remove pkg-split-a extra ${arch}
		done
	done

	../cron-jobs/ftpdir-cleanup

	for arch in ${arches[@]}; do
		for pkg in "${pkgdir}/${pkgs[0]}"/*-${arch}.pkg.tar.*; do
			[ -f "${FTP_BASE}/packages/arch/${arch}/${pkg}" ] && fail "packages/arch/${arch}/${pkg} found"
			[ -f "${FTP_BASE}/${repo}/os/${arch}/${pkg}" ] && fail "${repo}/os/${arch}/${pkg} found"
		done

		for pkg in "${pkgdir}/${pkgs[1]}"/*-${arch}.pkg.tar.*; do
			checkPackage extra $(basename ${pkg}) ${arch}
		done
	done
}

#
# transition tests
#

testMovePackagesWithoutPool() {
	local arches=('i686' 'x86_64')
	local pkgs=('pkg-simple-a' 'pkg-simple-b' 'pkg-split-a' 'pkg-split-b')
	local pkgbase
	local arch
	local pkg
	local old

	for pkgbase in ${pkgs[@]}; do
		for arch in ${arches[@]}; do
			releasePackage testing ${pkgbase} ${arch}
		done
	done

	../db-update testing

	# transform two packages to old style layout
	for arch in ${arches[@]}; do
		for old in 0 2; do
			for pkg in "${pkgdir}/${pkgs[${old}]}"/*-${arch}.pkg.tar.*; do
				pkg=$(basename $pkg)
				mv -f "${FTP_BASE}/packages/arch/${arch}/${pkg}" "${FTP_BASE}/testing/os/${arch}/${pkg}"
			done
		done
	done

	../cron-jobs/ftpdir-cleanup

	for pkgbase in ${pkgs[@]}; do
		for arch in ${arches[@]}; do
			../db-move ${pkgbase} testing extra ${arch}
		done
	done

	../cron-jobs/ftpdir-cleanup

	for pkgbase in ${pkgs[@]}; do
		for arch in ${arches[@]}; do
			for pkg in "${pkgdir}/${pkgbase}"/*-${arch}.pkg.tar.*; do
				checkPackage extra $(basename ${pkg}) ${arch}
			done
			checkRemovedPackage testing ${pkgbase} ${arch}
		done
	done
}

testMoveAnyPackagesWithoutPool() {
	local pkgs=('pkg-any-a' 'pkg-any-b')
	local pkgbase
	local arch
	local pkg

	for pkgbase in ${pkgs[@]}; do
		releasePackage testing ${pkgbase} any
	done

	../db-update testing

	# transform a package to old style layout
	for pkg in "${pkgdir}/${pkgs[0]}"/*-any.pkg.tar.*; do
		pkg=$(basename $pkg)
		mv -f "${FTP_BASE}/packages/arch/any/${pkg}" "${FTP_BASE}/testing/os/any/${pkg}"
		for arch in i686 x86_64; do
			ln -sf "../any/${pkg}" "${FTP_BASE}/testing/os/${arch}/${pkg}"
		done
	done

	../cron-jobs/ftpdir-cleanup

	for pkgbase in ${pkgs[@]}; do
		../db-move ${pkgbase} testing extra any
	done

	../cron-jobs/ftpdir-cleanup

	for pkgbase in ${pkgs[@]}; do
		for pkg in "${pkgdir}/${pkgbase}"/*-any.pkg.tar.*; do
			checkAnyPackage extra $(basename ${pkg})
		done
		checkRemovedAnyPackage testing ${pkgbase}
	done
}

. "${curdir}/lib/shunit2"