#!/bin/bash
# Author: Daniele Rondina, geaaru@sabayonlinux.org

main () {

    wget https://raw.githubusercontent.com/Sabayon/sbi-tasks/master/infra/mirrors.yml -O /tmp/mirrors.yml
    wget https://raw.githubusercontent.com/Sabayon/sbi-tasks/master/infra/scripts/sabayon-repo-generator -O /tmp/sabayon-repo-generator
  chmod a+x /tmp/sabayon-repo-generator

  local f=""
  local descr=""
  local name=""
  local reposdir="/etc/entropy/repositories.conf.d"
  local repofiles=(
    "entropy_sabayon-limbo"
    "entropy_sabayonlinux.org"
    "entropy_sabayon-weekly"
  )

  for repo in ${repofiles[@]} ; do
    if [ -e "${reposdir}/${repo}" ] ; then
      f=${reposdir}/${repo}
    else
      f=${reposdir}/_${repo}
    fi

    if [[ ${repo} =~ .*limbo* ]] ; then
      descr="Sabayon Limbo Testing Repository"
    else
      descr="Sabayon Linux Official Repository"
    fi

    name=${repo//entropy_/}

    /tmp/sabayon-repo-generator --mirror-file /tmp/mirrors.yml --descr "${descr}" --name "${name}" --to "${f}"

  done

  rm -v /tmp/sabayon-repo-generator
  rm -v /tmp/mirrors.yml
}

main
