summaryrefslogtreecommitdiff
path: root/libredbdiff-standalone
blob: f82e65fe3a9169f0053d4f2edb17845c17a5121a (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/bin/bash

# alfplayer
#
# Compare package versions of latest packages in Parabola and Arch
# The main version of this script is in libretools: https://projects.parabola.nu/packages/libretools.git/tree/src/abslibre-tools/libredbdiff

name="Libredbdiff"

baseconfpath="/etc/libredbdiff"
basedbpath="/var/lib/libredbdiff"

conffile="$baseconfpath/pacman.conf.parabola"
conffilearch="$baseconfpath/pacman.conf.archlinux"

dbpath="$basedbpath/pacman.parabola"
dbpatharch="$basedbpath/pacman.archlinux"

mirrorlist="$baseconfpath/mirrorlist.parabola"
mirrorlistarch="$baseconfpath/mirrorlist.archlinux"

mirror='http://repo.parabola.nu/$repo/os/$arch'
mirrorarch='http://mirrors.kernel.org/archlinux/$repo/os/$arch'


error() { echo -e "Error. $@" > /dev/stderr ; exit 1; }

if [[ $# == 1 ]] ; then
  [[ $1 == -n ]] && UPDATE=0
  [[ $1 =~ ^(-h|--help)$ ]] && { echo -e "${name}. Show [libre] packages that need to be updated from Arch repositories.\n\nUsage:\nlibredbdiff\t\tInitialize (first run) or update pacman databases\nlibredbdiff -n\t\tPrint results" ; exit 0 ; }
elif [[ $# != 0 ]] ; then
  error "Bad arguments. Nothing done."
else
  UPDATE=1
fi

if (( $UPDATE )) ; then
  [[ $EUID != 0 ]] && { error "To initialize $name or update $name pacman databases, the script must be run as root. Nothing done." ; }
 
  [[ -d "$baseconfpath" && -d "$basedbpath" && -d "$dbpath" && -d "$dbpatharch" && -e "${conffile}" && -e "${conffilearch}" && -e "${mirrorlist}" && -e  "${mirrorlist}" ]] || { echo "${name} files are missing. Initializing." ; init=1 ; }

  createdir() {
    if [[ ! -e $1 ]] ; then
      echo "Creating directory $1"
      mkdir "$1" || error "Failed to create directory $1. Exiting."
    else
      [[ $init ]] && echo "Warning. ${1} already exists. Skipping."
    fi
  }
  createdir "$baseconfpath"
  createdir "$basedbpath"
  createdir "$dbpath"
  createdir "$dbpatharch"

  downloadfile() {
    if [[ ! -e $1 ]] ; then
      echo "$2"
      if wget -q "$3" -O "$1"; then
	return 255
      else
        error "Failed to download ${1}. Exiting."
      fi
    else
      [[ $init ]] && echo "Warning. ${1} already exists. Skipping."
    fi
  }
  downloadfile "${conffile}" "Downloading Parabola pacman.conf" "https://projects.parabola.nu/abslibre.git/plain/libre/pacman/pacman.conf.x86_64"
  if [[ $? == 255 ]] ; then
    sed -i "s|^#DBPath .*|DBPath = ${dbpath}|" "${conffile}"
    echo "Enabling nonprism repo in ${conffile}"
    sed -i 's|^# after the header, and they will be used before the default mirrors.|# after the header, and they will be used before the default mirrors.\n\n[nonprism]\nInclude = /etc/pacman.d/mirrorlist|' "${conffile}"
    echo "Enabling libre-multilib repo in ${conffile}"
    sed -i "s/\#\[libre-multilib\]/[libre-multilib]/" "${conffile}"
    sed -i "\/\[libre-multilib\]/,+1 s/#Include/Include/" "${conffile}"
    echo "Enabling multilib repo in ${conffile}"
    sed -i "s/\#\[multilib\]/[multilib]/" "${conffile}"
    sed -i "\/\[multilib\]/,+1 s/#Include/Include/" "${conffile}"
  fi

  downloadfile "${conffilearch}" "Downloading Arch pacman.conf" "https://projects.archlinux.org/svntogit/packages.git/plain/pacman/trunk/pacman.conf.x86_64"
  if [[ $? == 255 ]] ; then
    sed -i "s|^#DBPath .*|DBPath = ${dbpatharch}|" "${conffilearch}"
    echo "Setting Arch mirrorlist file in ${conffilearch}"
    sed -i "s|/etc/pacman\.d/mirrorlist$|$baseconfpath/mirrorlist.archlinux|" "${conffilearch}"
    echo "Enabling multilib repo in ${conffilearch}"
    sed -i "s/\#\[multilib\]/[multilib]/" "${conffilearch}"
    sed -i "\/\[multilib\]/,+1 s/#Include/Include/" "${conffilearch}"
  fi

  setmirror() {
    local distro="$1"
    local mirror="$2"
    local mirrorlist="$3"
    [[ $init ]] && [[ $mirror ]] && {
      mirrorescaped="${mirror//./\\.}" ; mirrorescaped="${mirrorescaped//\$/\\$}"
        echo "Setting ${mirror} as the only enabled ${distro} mirror." && \
          sed -i 's|^#\(Server = '"${mirrorescaped}"'\)$|\1|' "${mirrorlist}"
    }
  }

  downloadfile "${mirrorlist}" "Downloading Parabola mirrorlist" "https://repo.parabola.nu/mirrorlist.txt"
  if [[ $? == 255 ]] ; then
    sed -i 's|^Server|#Server|' "${mirrorlist}"
    setmirror "Parabola" "$mirror" "$mirrorlist"
  fi

  downloadfile "${mirrorlistarch}" "Downloading Arch mirrorlist" "https://projects.archlinux.org/svntogit/packages.git/plain/pacman-mirrorlist/trunk/mirrorlist"
  if [[ $? == 255 ]] ; then
    setmirror "Arch" "$mirrorarch" "$mirrorlistarch"
  fi

  echo -e "\nSynchronizing $name pacman databases for Parabola"
  pacman --config "${conffile}" -b "${dbpath}" -Sy || error "Failed to synchronize pacman database for Parabola. Exiting."

  echo -e "\nSynchronizing $name pacman databases for Arch"
  pacman --config "${conffilearch}" -b "${dbpatharch}" -Sy || error "Failed to synchronize pacman database for Arch. Exiting."

  echo
  echo "$name pacman databases are updated. $name is ready. Run libredbdiff -n to print results."
  exit 0
else
  filenotfound() {
	  [[ ! -r $1 ]] && { error "Could not read $1. Nothing done.\nIt may be necessary to run libredbdiff without arguments as root to initialize ${name}." ; }
  }
  filenotfound "${dbpath}"
  filenotfound "${dbpatharch}"
  filenotfound "${conffile}"
  filenotfound "${conffilearch}"
  filenotfound "${mirrorlist}"
  filenotfound "${mirrorlistarch}"
fi

unset provides ver verarch
declare -Ax provides ver verarch

expac_version_test() {
if ! pkgname="$(pacman -Qoq expac)" ; then
    echo "The command expac could not be found installed. The package pcr/expac-relative 4-2.parabola1 (or later) must be installed." >&2
    exit 1
  elif [[ $pkgname == expac-git ]] ; then
    true
  elif [[ $pkgname == expac-relative ]] ; then
    if [[ $(vercmp "$(expac %v expac-relative)" 4-2.parabola1) != -1 ]] ; then
      echo "The version of expac-relative installed on the system is lower than needed." >&2
      exit 1
    fi
  else
    echo "expac command must be provided by pcr/expac-relative version 4-2.parabola1 (or later). This package must be installed." >&2
    exit 1
  fi
}

comparepkgs() {
  if [[ ${verarch["$pkgname"]} ]] ; then
    cmp=$(vercmp ${ver["$pkgname"]} ${verarch["$pkgname"]})
    [[ $cmp -lt 0 ]] && echo "${pkgname} needs update from the Arch package of the same name. Versions: ${ver["$pkgname"]} - ${verarch["$pkgname"]}"
  elif [[ ${provides["$pkgname"]} ]] ; then
    for provide in ${provides["$pkgname"]} ; do
      [[ ${verarch["$provide"]} ]] && {
        cmp=$(vercmp "${ver["$pkgname"]}" "${verarch["$provide"]}")
        [[ $cmp -lt 0 ]] && echo "${pkgname} may need update from provide candidate ${provide}. Versions: ${ver["$pkgname"]} - ${verarch["$provide"]}"
      }
    done
  else
	  echo "Could not find candidate to compare ${pkgname}"
  fi
}

expac_version_test

while read -a line ; do
  verarch["${line[0]}"]="${line[1]}"
done < <(pacman --dbpath "${dbpatharch}" --config "${conffilearch}" -Ss | grep -v '^ ' | awk -F/ '{print $2}')

for repo in libre pcr libre-multilib nonprism ; do
  echo "[$repo]"
  expac --config "${conffile}" -S '%r/%n %v %S' | awk -F/ '$1 == "libre" {print $2}' | while read -a line ; do
    ver["${line[0]}"]="${line[1]}"
    provides[${line[0]}]="${line[@]:2}"
    pkgname=${line[0]}
    comparepkgs
  done
done