#!/bin/bash

set -e

versionsUniq() {
  tr ' ' '\n' | \
  sed 's|^python$|python>=0|' | \
  sort -rV | \
  sed 's@^\([^<=>]*\)\(\(>=\|=\|<\)[0-9.]*\)$@\1\2 \1@' | \
  sed 's|^\(\S*\)$|\1 \1|' | \
  uniq -f1 "$@" | \
  awk '{print $1}' | \
  sed 's|^python>=0$|python|' | \
  sort | \
  grep -v '^\s*$'
}

ladePkgbuild() {
  paket="$1"

  if [ -n "${paket%%python-*}" ]; then
    return 1
  fi

  cd "$(dirname "${me}")"
  cd "${paket}"

  makepkg -f --verifysource

  . <(
    sed '
      /^\s*_pinned_dependencies\S*=(.*)/ d
      /^\s*_pinned_dependencies\S*=(/,/)/ d
    ' PKGBUILD
  )
  if grep -qF _pinned_dependencies PKGBUILD; then
    depends+=('"${_pinned_dependencies[@]}"')
  fi
}

versteheRequirements() {
  for version_prefix in '' 'v'; do
    for suffix in 'tar.gz' 'tgz'; do
      archiv="${_pkgname}-${version_prefix}${pkgver}.${suffix}"
      [ -r "${archiv}" ] && break
    done
    [ -r "${archiv}" ] && break
  done

  if [ ! -r "${archiv}" ]; then
    >&2 echo "Kann Quelldatei nicht finden!"
    return 2
  fi

  unset meta

  echo "setup.py lesen ..."

  meta="$(
    {
      {
        printf '%s = []\n' \
          "${metaRegexe[@]}"
        tar -Oxzf "${archiv}" "${_pkgname}-${pkgver}/setup.py" \
        | sed -n "$(
          printf '/=/ s/^\\s\\+//\n'
          printf '/^%s = \[/,/^\s*$/ p\n' \
            "${metaRegexe[@]}"
        )" \
        | sed '
          s/^\s*\(\S\+_requires = \[\)$/\1/
          s/ % [^[:space:],]\+\([],]\)$/ % ""\1/
        '
        for to_print in "${!metaRegexe[@]}"; do
          printf 'for item in %s:\n' \
            "${metaRegexe["${to_print}"]}"
          printf '    print("%s:%%s" %% item)\n' \
            "${to_print}"
        done
      } \
      | python
      {
        tar -Oxzf "${archiv}" "${_pkgname}-${pkgver}/requirements.txt"
        tar -tzf "${archiv}" | \
          grep '^.*/.*/requirements.txt' | \
          xargs -r tar -Oxzf "${archiv}"
      } | \
        sed '
          /^[a-zA-Z]/! d
          s/==/=/
          s/\s*\([<=>]\+\)\s*/\1/
          s/\s*[#;].*$//
          /^$/d
          /^numpydoc\s*\([<>=].*\)\?$/d
          s,^https\?:.*/mysql-connector-python-[^/]*$,mysql-connector,
          s/^/depends:/
        '
      ! grep -qwF _pinned_dependencies PKGBUILD \
      || printf 'depends:"${_pinned_dependencies[@]}"\n'
    } | \
      tr '[:upper:]' '[:lower:]' | \
      sort -u
  )"

  for wasAusgeben in "${!metaRegexe[@]}"; do
    metaInfos["${wasAusgeben}"]=$(
      printf '%s\n' "${meta}" | \
        sed -n '
          s/^'"${wasAusgeben}"'://
          T
          y/ /\n/
          p
        ' | \
        sed '
          /^cython\($\|[<>=]\)\|^python-\|^"\?\$/! s/^/python-/
          /[^<>]=/ s/=/>=/
        '
    )
  done

  for wasAusgeben in "${!metaRegexe[@]}"; do
    eval 'tmp=("${'${wasAusgeben}'[@]}")'
    pkgBuild["${wasAusgeben}"]=$(
      echo "${tmp[@]}" | \
        tr " '" '\n\n' | \
        grep -v '^$' | \
        sort -u
    )
    if [ -z "${pkgBuild["${wasAusgeben}"]}" ] && \
      [ "${wasAusgeben}" == "${pythonPrefix}depends" ]; then
      pkgBuild["${wasAusgeben}"]="python"
    fi

    metaYml["${wasAusgeben}"]="${metaInfos["${wasAusgeben}"]}"

    if [ "${wasAusgeben}" == "${pythonPrefix}depends" ]; then
      metaYml["${wasAusgeben}"]=$(
        {
          printf '%s\n' "${metaYml["${wasAusgeben}"]}"
          printf 'python>=%s\n' "${pyver}"
          printf 'python<%s.%s\n' "${pyver%%.*}" "$((${pyver#*.}+1))"
        } \
        | sort -u
      )
    fi
    if [ "${wasAusgeben}" == "${pythonPrefix}makedepends" ]; then
      metaYml["${wasAusgeben}"]=$(
        {
          {
            {
              {
                echo "${metaYml["${wasAusgeben}"]}"
                echo "${metaYml["${pythonPrefix}depends"]}"
              } | \
                versionsUniq
              echo "${metaYml["${pythonPrefix}depends"]}"
            } | \
              sort | \
              uniq -d | \
              sed 'p'
            echo "${metaYml["${wasAusgeben}"]}"
          } | \
            versionsUniq -u
          echo "${pkgBuild["${wasAusgeben}"]}" | \
            grep '^\s*\(cython\|python-setuptools\)\($\|[<>=]\)'
        } | \
          versionsUniq
      )
    fi
    if [ "${wasAusgeben}" == "${pythonPrefix}checkdepends" ]; then
      metaYml["${wasAusgeben}"]=$(
        {
          {
            {
              {
                echo "${metaYml["${wasAusgeben}"]}"
                echo "${metaYml["_depends"]}"
              } | \
                versionsUniq
              echo "${metaYml["_depends"]}"
            } | \
              sort | \
              uniq -d | \
              sed 'p'
            echo "${metaYml["${wasAusgeben}"]}"
          } | \
            versionsUniq -u
          echo "${pkgBuild["${wasAusgeben}"]}" | \
            grep '^\s*\(python-nose\)\($\|[<>=]\)'
        } | \
          versionsUniq
      )
    fi
  done

  echo "... fertig"
}

declare -A metaInfos
declare -A metaRegexe
declare -A metaSubst
declare -A metaYml
declare -A pkgBuild

if [ "$1" = 'python2' ]; then
  pythonPrefix='_'
  pyver=$(python2 --version)
else
  pythonPrefix=''
  pyver=$(python --version)
fi
pyver=$(
  printf '%s\n' "${pyver}" | \
    cut -d' ' -f2 | \
    cut -d. -f1,2
)
metaRegexe["${pythonPrefix}depends"]="install_requires"
metaSubst["${pythonPrefix}depends"]=true
metaRegexe["${pythonPrefix}makedepends"]="setup_requires"
metaSubst["${pythonPrefix}makedepends"]=true
metaRegexe["${pythonPrefix}checkdepends"]="test_requires"
metaSubst["${pythonPrefix}checkdepends"]=true