#!/bin/bash
current_path=$(
  pwd
  cd $(dirname $0)
)
download_path=${current_path}/download_rpms
rpms_file=${current_path}/delete.txt
gitee_url="https://gitee.com/src-openeuler/"

function init_env() {
  if [[ -d ${download_path} ]]; then
    rm -rf ${download_path}/*
  else
    mkdir -p ${download_path}
  fi
}

function download_rpm_from_gitee() {
  while read line; do
    echo "[INFO] Start to clone rmp: ${line}"
    git clone ${gitee_url}/${line} ${download_path}/${line}
  done <${rpms_file}
}

function download_rpm_from_repo() {
  while read line; do
    echo "[INFO] Start to download rmp: ${line}"
    dnf download --source ${line} --destdir ${download_path}
  done <${rpms_file}
}

function main() {
  init_env
  if [[ $1 == "repo" ]]; then
    download_rpm_from_repo
  elif [ $1 == "gitee" ]; then
    download_rpm_from_gitee
  fi
}

main "$@"
