aboutsummaryrefslogtreecommitdiff
blob: ff4136bc60b8edad9e133e1355985418273058a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#autoload

# Usage:
#   _gentoo_repos    -> returns the main repo (with PORTDIR fallback)
#   _gentoo_repos -o -> returns the list of non-main repos (with PORTDIR_OVERLAY fallback)

_gentoo_repos() {
  local mainreponame mainrepopath overlayname overlaypath

  if [[ -e /usr/share/portage/config/repos.conf ]]; then
    if [[ ${1} == "-o" ]]; then
      for overlayname in $(_parsereposconf -l); do
        overlaypath+=($(_parsereposconf ${overlayname} location))
      done

      source /etc/make.conf 2>/dev/null
      source /etc/portage/make.conf 2>/dev/null

      overlaypath+=(${(@)PORTDIR_OVERLAY})

      echo "${(@u)overlaypath}"
    else
      mainreponame=$(_parsereposconf DEFAULT main-repo)
      mainrepopath=$(_parsereposconf ${mainreponame} location)

      echo "${mainrepopath}"
    fi
  else
    source /usr/share/portage/config/make.globals 2>/dev/null
    source /etc/make.conf 2>/dev/null
    source /etc/portage/make.conf 2>/dev/null

    if [[ ${1} == "-o" ]]; then
      echo "${(@u)PORTDIR_OVERLAY}"
    else
      echo "${PORTDIR}"
    fi
  fi
}

_parsereposconf() {
  local v f insection section arr

  for f in /usr/share/portage/config/repos.conf \
           /etc/portage/repos.conf \
           /etc/portage/repos.conf/*.conf; do

    [[ -f ${f} ]] || continue
    insection=0
    declare -A arr
    IFS='= '

    while read -r name value; do
      [[ -z ${name} || ${name} == '#'* ]] && continue

      if [[ (${name} == '['*']') && (-z ${value}) ]]; then
        value=${name//(\]|\[)}
        name="section"
      fi
      arr[${name}]=${value}

      if [[ ${insection} == 1 && ${name} == "section" ]]; then
        break
      elif [[ ${name} == "section" ]]; then
        [[ ${value} == ${1} ]] && insection=1
        secname+=(${value})
      elif [[ ${insection} == 1 ]]; then
        if [[ ${name} == ${2} ]]; then
          v=${value}
        fi
      fi
      continue
    done < ${f}
  done

  if [[ ${1} == "-l" ]]; then
    echo "${(@)secname}"
  else
    echo "${v}"
  fi
}

_gentoo_repos "$@"

# vim: set et sw=2 ts=2 ft=zsh: