#!/bin/sh -xeu
#
# Copyright (c) 2013, 2014, 2015 Intel, Inc.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; version 2 of the License
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.

ubuntu_repolist=/etc/apt/sources.list
arch_repolist=/etc/pacman.d/mirrorlist
suse_repodir=/etc/zypp/repos.d
fedora_repodir=/etc/yum.repos.d

create_rpm_repo() {
 reponame=$1
 url=$2

 repofile="$fedora_repodir/$reponame.repo"
 cat > $repofile <<EOF
[$reponame]
name=$reponame
baseurl=$url
enabled=1
proxy=_none_
gpgcheck=0
EOF
}

install_pkgs() {
 cmd=$1
 testreq_pkgs=$2
 pkgs=$3

 if [ -n "$testreq_pkgs" ]; then
   echo "======= Installing TEST REQ packages ========"
   $cmd $testreq_pkgs
 fi
 if [ -n "$pkgs" ]; then
   echo "======= Installing UNDER TEST packages ========"
   $cmd $pkgs
 fi
}

check_suse_duplicate_repo_add() {
 baseurl=$1
 reponame=$2
 if ! zypper repos -u | grep "$baseurl" > /dev/null; then
     [ -n "$(zypper lr | grep "$reponame")" ] && reponame=$reponame"$(date +%s)"
     zypper ar -fG $baseurl $reponame
 fi
}

add_extra_repos() {
 r_on=$1
 e_repos=$2
 rlist=$3

 OLDIFS=$IFS
 IFS=','
 for r in ${e_repos}; do
   echo "======= Adding EXTRA repository ========"
   reponame=`echo $r | sed 's/ //g' | sed 's/http://g' | tr -d '[:punct:][:space:]'`
   if [ $r_on = "ubuntu" ] ; then
     echo "$r" >>  $rlist
     tmp_file=$(mktemp)
     sort -u $rlist > $tmp_file
     mv $tmp_file $rlist
   elif [ $r_on = "opensuse" ] ; then
     check_suse_duplicate_repo_add "$r" "$reponame"
   elif [ $r_on = "fedora" ] ; then
     # For yum platforms, it use function create_rpm_repo, it overwritten the existed
     # same file, so no need to check duplicate repo
     create_rpm_repo $reponame $r
   elif [ $r_on = "arch" ] ; then
     echo "$e_repos" >>  $rlist
   fi
 done
 IFS=$OLDIFS
}

delete_all_repos() {
 r_on=$1
 if [ $r_on = "ubuntu" ] ; then
   rm -f $ubuntu_repolist
 elif [ $r_on = "opensuse" ] ; then
   rm -f $suse_repodir/*
 elif [ $r_on = "fedora" ] ; then
   rm -f $fedora_repodir/*
 elif [ $r_on = "arch" ] ; then
   rm -f $arch_repolist
 fi
}

project="$1"
repo="$2"
packages="$3"
sproject="$4"
testreq_packages="$5"
extra_repos="$6"
noupd="$7"
download_host="$8"

if [ -x /usr/bin/apt-get -a -d /etc/apt/sources.list.d/ ] ; then
# Ubuntu or Debian
   run_on="ubuntu"
   srclist=$ubuntu_repolist
   [ -n "$project" ] && echo "deb $download_host/home:/tester:/$project/$repo/ /" >  $srclist
   [ -n "$sproject" -a "/home:/tester:/$project" != "$sproject" ] && echo "deb $download_host/$sproject/$repo/ /" >> $srclist
   [ -n "$extra_repos" ] && add_extra_repos $run_on "$extra_repos" $srclist
   apt-get update
   [ "$noupd" != "noupdate" ] && apt-get upgrade -y --force-yes
   install_pkgs "apt-get install -q -y --force-yes" "$testreq_packages" "$packages"
elif [ -x /usr/bin/zypper -a -d $suse_repodir ] ; then
# OpenSuse
   run_on="opensuse"
   if [ -n "$project" ]; then
       baseurl="$download_host/home:/tester:/$project/$repo/"
       check_suse_duplicate_repo_add $baseurl $project
   fi
   reponame="tools-`echo \"$sproject\" | tr -d '[:punct:][:space:]'`"
   if [ -n "$sproject" ]; then
       baseurl="$download_host/$sproject/$repo/"
       check_suse_duplicate_repo_add $baseurl $reponame
   fi
   [ -n "$extra_repos" ] && add_extra_repos $run_on "$extra_repos" ""
   # zypper exit nonzero if all repositories are up to date
   zypper --non-interactive ref || true
   [ "$noupd" != "noupdate" ] && zypper --non-interactive up
   install_pkgs "zypper --non-interactive install" "$testreq_packages" "$packages"
elif [ \( -x /bin/yum -o -x /usr/bin/yum \) -a -d $fedora_repodir ] ; then
# Fedora
   run_on="fedora"
   [ -n "$project" ] && create_rpm_repo $project "$download_host/home:/tester:/$project/$repo/"
   [ -n "$sproject" ] && create_rpm_repo "tools" "$download_host/$sproject/$repo/"
   [ -n "$extra_repos" ] && add_extra_repos $run_on "$extra_repos" ""
   yum -y clean all
   [ "$noupd" != "noupdate" ] && yum -y update
   install_pkgs "yum -y install" "$testreq_packages" "$packages"
elif [ -x /usr/bin/pacman -a -d /etc/pacman.d ] ; then
# Arch
   run_on="arch"
   srclist=$arch_repolist
   reponame=`echo "home_tester_"$project"_"$repo`
   [ -n "$project" ] && echo "[$reponame]" >>  $srclist
   [ -n "$project" ] && echo "Server = $download_host/home:/tester:/$project/$repo/\$arch/" >>  $srclist
   [ -n "$extra_repos" ] && add_extra_repos $run_on "$extra_repos" "$srclist"
   # update databases after adding a new repo(s)
   pacman -S --refresh
   # upgrade packages
   [ "$noupd" != "noupdate" ] && pacman --noconfirm -Su
   install_pkgs "pacman --noconfirm -S" "$testreq_packages" "$packages"
fi
install_status=$?
# if clean state is requested, delete repo config
[ -f /home/build/need_shutdown ] && delete_all_repos $run_on
exit $install_status
