#!/bin/bash
# shellcheck disable=SC2145
current_path=$(
  cd $(dirname $0)
  pwd
)
get_packages=0
get_source_list=0
get_binary_list=0
obs_project=""
pkg_list=""
out_file=""

function parse_args() {
  if [ $# -eq 0 ]; then
    args_help
    exit 1
  fi

  while [ $# -gt 0 ]; do
    case $1 in
    get_rpm)
      get_packages=1
      shift
      ;;
    get_source_rpm)
      get_source_list=1
      shift
      ;;
    get_binary_rpm)
      get_binary_list=1
      shift
      ;;
    -p)
      obs_project=$2
      shift 2
      ;;
    -l)
      pkg_list=$2
      shift 2
      ;;
    -o)
      out_file=$2
      shift 2
      ;;
    *)
      args_help
      exit 1
      ;;
    esac
  done
}

function get_rpm_method() {
  echo "[INFO] Start to get package list in ${obs_project}"
  if [[ -z ${out_file} ]]; then
    out_file=${current_path}/obs_rpm.list
  fi

  result=$(osc list ${obs_project} >${out_file} 2>/dev/null)
  if [[ $? -eq 0 ]]; then
    echo "[INFO] Get packages successful, file is ${out_file}"
    exit 0
  else
    echo "[ERROR] Get packages failed, message is ${result}"
    exit 1
  fi
}

function _pre_env() {
  if [[ -z ${pkg_list} ]]; then
    echo "[INFO] Package list is not specified, all packages in the project will be queried"
    osc list ${obs_project} >${current_path}/obs_rpm.list 2>/dev/null
    pkg_list=${current_path}/obs_rpm.list
  fi

  if [[ -z ${out_file} ]]; then
    out_file=${current_path}/obs_source_rpm.list
  fi
  rm -rf ${out_file}
}

function get_source_rpm_method() {
  echo "[INFO] Start to get source package list in ${obs_project} ..."
  _pre_env

  for rpm in $(cat ${pkg_list}); do
    source_rpm=$(osc ls -b ${obs_project} ${rpm} standard_aarch64 aarch64 2>/dev/null | grep src.rpm)
    if [[ -z ${source_rpm} ]]; then
      source_rpm=$(osc ls -b ${obs_project} ${rpm} standard_x86_64 x86_64 2>/dev/null | grep src.rpm)
      if [[ -z ${source_rpm} ]]; then
        echo "[WARNING] ${rpm} not found in ${obs_project}"
      fi
    fi
    source_rpm_name=$(echo ${source_rpm} | awk -F "-[0-9]" '{print $1}')
    echo "${rpm},${source_rpm_name}" >>${out_file}
  done

  if [[ "${pkg_list}" == "${current_path}/obs_rpm.list" ]]; then
    echo "" >${pkg_list}
  fi

  echo "[INFO] Get source packages successful, file is ${out_file}"
  exit 0
}

function get_binary_rpm_method() {
  echo "[INFO] So sorry,This function is not yet implemented"
  exit 1
}

function args_help() {
  echo "
OBS RPM TOOL
Usage:    sh ${NAME} COMMAND [Options]
COMMAND:
  get_rpm
    Get all packages of specify obs project
  get_source_rpm
    Get the source package name of the software package on the OBS project
    If you execute the query package, please enter in obs_rpm.list
  get_binary_rpm
    Get the binary package name of the software package on the OBS project
    If you execute the query package, please enter in obs_rpm.list
Options:
  -p obs_project
    Specify the obs project name
  -l pkg_list
    Specify the package list file,default is 'obs_rpm.list' of current path
  -o
    Specify the output file,default is 'obs_source_rpm.list/obs_binary_rpm.list' of current path

For example:
  sh obs_rpm_info.sh get_rpm -p openEuler:20.03:LTS:SP2 -o obs_rpm.list
  sh obs_rpm_info.sh get_source_rpm -p openEuler:20.03:LTS:SP2 [-l obs_rpm.list] -o obs_source_rpm.list
  sh obs_rpm_info.sh get_binary_rpm -p openEuler:20.03:LTS:SP2 [-l obs_rpm.list] -o obs_binary_rpm.list
"
  exit 0
}

function main() {
  parse_args "$@"
  if [[ ${get_packages} -eq 1 ]]; then
    get_rpm_method
  elif [[ ${get_source_list} -eq 1 ]]; then
    get_source_rpm_method
  elif [[ ${get_binary_list} -eq 1 ]]; then
    get_binary_rpm_method
  fi
}

main "$@"
