#!/bin/bash -e

if test -f /etc/setup-susebootstrap.conf; then
    . /etc/setup-susebootstrap.conf
fi

user=${user:-"${USER},jenkins"}

if test ${#distros[@]} -lt 1; then
    distros=( opensuse
              fedora
              centos )
fi
if test ${#distros[@]} -ne ${#archs[@]}; then
    archs=( "i586 x86_64"
            "i686 x86_64"
            "x86_64" )
fi
if test ${#distros[@]} -ne ${#versions[@]}; then
    versions=( "42.1 13.2 13.1 tumbleweed"
               "23 22"
               "7 6 5" )
fi

for ((i=0; i<${#distros[@]}; ++i)); do
    distro=${distros[$i]}
    for arch in ${archs[$i]}; do
        case $arch in
            i?86) basearch=i386;;
            *) basearch=$arch;;
        esac
        for version in ${versions[$i]}; do
            echo "******** update $distro $version $basearch ********" 1>&2
            if ! test -d /var/chroot/${distro}-${version}_${basearch}; then
                echo " ----    install schroot $distro $version $arch" 1>&2
                sudo VERSION=${version} HW="${arch} noarch" \
                     SCHROOTUSER="$user" \
		     OS=${distro} \
                     ADDITIONAL-DEFINITIONS=${additional_definitions} \
                     ./susebootstrap.sh $*
            fi
        done
    done
done
