summaryrefslogtreecommitdiff
path: root/src/shared/deptree.sh
blob: ffc0af820a1a4c728b924bb71702ad06cebcb8a7 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/bash
 ##############################################################################
 #                      parabola-riscv64-bootstrap                            #
 #                                                                            #
 #    Copyright (C) 2018  Andreas Grapentin                                   #
 #                                                                            #
 #    This program is free software: you can redistribute it and/or modify    #
 #    it under the terms of the GNU General Public License as published by    #
 #    the Free Software Foundation, either version 3 of the License, or       #
 #    (at your option) any later version.                                     #
 #                                                                            #
 #    This program is distributed in the hope that it will be useful,         #
 #    but WITHOUT ANY WARRANTY; without even the implied warranty of          #
 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           #
 #    GNU General Public License for more details.                            #
 #                                                                            #
 #    You should have received a copy of the GNU General Public License       #
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.   #
 ##############################################################################

check_deptree() {
  echo -n "checking for complete deptree ... "

  local have_deptree=yes
  [ -f "$DEPTREE".FULL ] || have_deptree=no
  echo $have_deptree

  [ "x$have_deptree" == "xyes" ] || return "$ERROR_MISSING"
}

build_deptree() {
  check_exe ed pacman sed || return

  # create empty deptree
  truncate -s0 "$DEPTREE".FULL

  # add the packages listed in the given groups
  local g p r
  for g in "$@"; do
    for p in $(pacman -Sg "$g" | awk '{print $2}'); do
      r=$(make_realpkg "$p") || return "$ERROR_MISSING"

      if ! grep -q "^$r :" "$DEPTREE".FULL; then
        echo "$r : [ ] # $g" >> "$DEPTREE".FULL
      else
        sed -i "s/^$r : \\[.*/&, $g/" "$DEPTREE".FULL
      fi
    done
  done
}

prepare_deptree() {
  check_deptree || build_deptree "$@" || return

  [ -f "$DEPTREE" ] || cp "$DEPTREE"{.FULL,}
  chown "$SUDO_USER" "$DEPTREE"{,.FULL}
}

deptree_next() {
  local pkg
  pkg=$(grep '\[ *\]' "$DEPTREE" | tail -n1 | awk '{print $1}')
  [ -n "$pkg" ] || return "$ERROR_MISSING"
  echo "$pkg"
}

deptree_remove() {
  sed -i "/^$1 :/d; s/ /  /g; s/ $1 / /g; s/  */ /g" "$DEPTREE"
}

deptree_check_depends() {
  local OPTIND o needed=yes
  while getopts "n" o; do
    case "$o" in
      n) needed=no ;;
      *) die -e "$ERROR_INVOCATION" "Usage: ${FUNCNAME[0]} [-p] deptree pkgname depend" ;;
    esac
  done
  shift $((OPTIND-1))

  local pkg="$1"
  shift

  local dep r res=0
  # shellcheck disable=SC2068
  for dep in $@; do
    r=$(make_realpkg "$dep") || { res="$ERROR_MISSING"; continue; }

    local have_pkg=yes
    check_pkgfile "$PKGDEST" "$r" || have_pkg=no

    if ! grep -q "^$r :" "$DEPTREE".FULL; then
      echo "$r : [ ] # $pkg" >> "$DEPTREE".FULL
      echo "$r : [ ] # $pkg" >> "$DEPTREE"
    else
      sed -i "/#.* $pkg\\(\\$\\|[ ,]\\)/! s/^$r : \\[.*/&, $pkg/" "$DEPTREE"{,.FULL}
    fi
    if [ "x$needed" == "xyes" ] && [ "x$have_pkg" == "xno" ]; then
      sed -i "s/^$pkg : \\[/& $r/" "$DEPTREE"{,.FULL}
    fi
  done

  return "$res"
}