aboutsummaryrefslogtreecommitdiffstats
path: root/bin/pkgs
blob: ab325b112f7d1988d35e91dc62f81f0f48c8268c (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env bash

set -eu

# source makepkg.conf to use PACKAGER
. /etc/makepkg.conf
# shellcheck source=/dev/null
[ -f "${HOME}/.makepkg.conf" ] && . "${HOME}/.makepkg.conf"

package_dir_base="${HOME}/packages/"
nvchecker_dir_base="${HOME}/.config/nvchecker/"
repo=""
package=""
mode=""
new_package=0

declare -A upstreams=(
  ["aur"]="aur@aur.archlinux.org"
  ["community"]="svn+ssh://svn-community@repos.archlinux.org/srv/repos/svn-community/svn"
  ["core"]="svn+ssh://svn-packages@repos.archlinux.org/srv/repos/svn-packages/svn"
  ["extra"]="svn+ssh://svn-packages@repos.archlinux.org/srv/repos/svn-packages/svn"
)
declare -A package_lists=(
  ["aur"]="$HOME/.config/packages/aur.txt"
  ["community"]="$HOME/.config/packages/community.txt"
  ["core"]="$HOME/.config/packages/core.txt"
  ["extra"]="$HOME/.config/packages/extra.txt"
)
declare -A package_dirs=(
  ["aur"]="${package_dir_base}aur"
  ["community"]="${package_dir_base}community"
  ["core"]="${package_dir_base}core"
  ["extra"]="${package_dir_base}extra"
)

check_dependencies() {
  # check whether required commands are installed
  local _commands=('svn' 'git')
  local _command

  for _command in "${_commands[@]}"; do
    if ! command -v "${_command}" > /dev/null; then
      printf "The required command %s can not be found. Exiting...\n" "$_command"
      exit 1
    fi
  done
}

create_tmp_packagelist() {
  local list_file=""
  list_file=$(basename "$1")
  mktemp -p "$XDG_RUNTIME_DIR/" "$list_file.XXXX"
}

add_package() {
  local name="$1"
  local tmp_file=""
  tmp_file=$(create_tmp_packagelist "${package_lists[$repo]}")
  if [[ ! -f "${package_lists[$repo]}" ]]; then
    echo "Adding initial package list: ${package_lists[$repo]}"
    touch "${package_lists[$repo]}"
  fi
  echo "Adding $package to list of $repo packages."
  echo "$name" | cat "${package_lists[$repo]}" - |uniq |sort > "$tmp_file"
  cat "$tmp_file" > "${package_lists[$repo]}"
  rm "$tmp_file"
}

create_nvchecker_config() {
  if [[ ! -d "${nvchecker_dir_base}" ]]; then
    mkdir -p "${nvchecker_dir_base}"
  fi
  if [[ ! -f "${nvchecker_dir_base}${repo}.toml" ]]; then
    printf "[__config__]\noldver = \"%s.old.json\"\nnewver = \"%s.new.json\"\n" "${repo}" "${repo}" \
      > "${nvchecker_dir_base}${repo}.toml"
  fi
}

create_package() {
  local name="$1"
  local pkgbuild=""
  create_repo_dir
  cd "${package_dirs[$repo]}"
  if [ "$repo" == "aur" ]; then
    git clone "${upstreams[$repo]}:${name}"
    pkgbuild="${name}/PKGBUILD"
  else
    svn update "${name}"
    if [[ ! -d "${name}" ]]; then
      mkdir -p "${name}"/{trunk,repos}
      pkgbuild="${name}/trunk/PKGBUILD"
    fi
  fi
  if [[ -n "${pkgbuild}" ]] && [[ ! -f "${pkgbuild}" ]]; then
    cp /usr/share/pacman/PKGBUILD.proto "${pkgbuild}"
    sed -e "s/NAME/$name/g" \
        -e "1i# Maintainer: ${PACKAGER:-}" \
        -e '/#/d' \
        -i "${pkgbuild}"
  fi

  create_nvchecker_config
  if ! grep -q "\[$name\]" "${nvchecker_dir_base}${repo}.toml"; then
    printf '\n[%s]\n\n' "${name}" >> "${nvchecker_dir_base}${repo}.toml"
  fi
}

remove_package() {
  local name="$1"
  local tmp_file=""
  tmp_file=$(create_tmp_packagelist "${package_lists[$repo]}")
  echo "Removing $package from list of $repo packages."
  cat "${package_lists[$repo]}" |grep -vE "^$name$" |uniq |sort > "$tmp_file"
  cat "$tmp_file" > "${package_lists[$repo]}"
  rm "$tmp_file"

  if [[ "$repo" == aur ]]; then
    rm -rf "${package_dirs[$repo]:?}/$name"
  fi
  # TODO: remove package dirs from svn

  # remove entry from nvchecker config
  if grep -q "\[$name\]" "${nvchecker_dir_base}${repo}.toml"; then
    sed -e "/^\[$name\]/,/^\[/{/^\[/!d};/^\[$name\]/d" \
      -i "${nvchecker_dir_base}${repo}.toml"
  fi
}

check_mode_set(){
  if [ -n "$mode" ]; then
    echo "Error: A mode can only be set once."
    exit 1
  fi
}

check_repo_set(){
  if [ -n "$repo" ]; then
    echo "Error: A repo can only be set once."
    exit 1
  fi
}

create_repo_dir() {
  if [ ! -d "${package_dirs[$repo]}" ];then
    if [ "$repo" == "aur" ]; then
      echo "Creating directory for $repo."
      mkdir -pv "${package_dirs[$repo]}"
    else
      (
        echo "Checking out $repo directory."
        mkdir -pv "$(dirname "${package_dirs[$repo]}")"
        cd "$(dirname "${package_dirs[$repo]}")"
        svn checkout -N "${upstreams[$repo]}" \
          "$(basename "${package_dirs[$repo]}")"
      )
    fi
  fi
}

print_help() {
  echo "Usage: pkgs -<a,u,h,A,C,E>"
}

clean_packages() {
  if [ -d "${package_dirs[$repo]}" ]; then
    cd "${package_dirs[$repo]}"
    for package in $(cat "${package_lists[$repo]}"); do
      if [ -d "$package" ]; then
        (
        cd "$package"
        echo "Cleaning package $repo/$package."
        if [ "$repo" == "aur" ]; then
          git clean -f
        else
          svn cleanup --remove-unversioned .
        fi
        )
      fi
    done
  else
    echo "Package dir for $repo is empty."
    exit 1
  fi
}

update_packages() {
  create_repo_dir
  cd "${package_dirs[$repo]}"
  for package in $(cat "${package_lists[$repo]}"); do
    if [ "$repo" == "aur" ]; then
      if [ -d "$package" ]; then
        (
          echo "Pulling package: $package."
          cd "$package"
          git pull
        )
      else
        echo "Cloning package '$package'."
        git clone "${upstreams[$repo]}:$package"
      fi
    else
      svn update "$package"
    fi
  done
}

if [ ${#@} -gt 0 ]; then
  while getopts ':a:chlnur:ACEP' flag; do
    case "${flag}" in
      a)
        check_mode_set
        mode="add"
        if [ -n "${OPTARG}" ]; then
          package="${OPTARG}"
        else
          echo "Package name can not be empty."
          exit 1
        fi
        ;;
      c)
        check_mode_set
        mode="clean"
        ;;
      h)
        print_help
        ;;
      l)
        check_mode_set
        mode="list"
        ;;
      n)
        new_package=1
        ;;
      # TODO: introduce -m flag for moving package (e.g. community2extra, extra2community)
      r)
        check_mode_set
        mode="remove"
        if [ -n "${OPTARG}" ]; then
          package="${OPTARG}"
        else
          echo "Package name can not be empty."
          exit 1
        fi
        ;;
      u)
        check_mode_set
        mode="update"
        ;;
      A)
        check_repo_set
        repo="aur"
        ;;
      C)
        check_repo_set
        repo="community"
        ;;
      E)
        check_repo_set
        repo="extra"
        ;;
      P)
        check_repo_set
        repo="core"
        ;;
      *)
        echo "Error. Unrecognized option: ${flag}."
        exit 1
        ;;
    esac
  done
else
  print_help
fi

case "${mode}" in
  add)
    if [ -z "$repo" ]; then
      echo "A repository must be defined for adding a package."
      exit 1
    elif [ -z "$package" ]; then
      echo "A package must be defined for adding a package."
      exit 1
    else
      check_dependencies
      add_package "$package"
      [ $new_package -gt 0 ] && create_package "$package"
    fi
    ;;
  remove)
    if [ -z "$repo" ]; then
      echo "A repository must be defined for removing a package."
      print_help
      exit 1
    elif [ -z "$package" ]; then
      echo "A package must be defined for removing a package."
      exit 1
    else
      check_dependencies
      remove_package "$package"
    fi
    ;;
  update)
    check_dependencies
    if [ -z "$repo" ];then
      for list in aur community core extra; do
        if [ -f "${package_lists[$list]}" ]; then
          repo="$list"
          update_packages
        fi
      done
    else
      update_packages
    fi
    ;;
  list)
    if [ -z "$repo" ]; then
      for list in aur community core extra; do
        if [ -f "${package_lists[$list]}" ]; then
          cat "${package_lists[$list]}"
        fi
      done
    else
      if [ -f "${package_lists[$repo]}" ]; then
        cat "${package_lists[$repo]}"
      fi
    fi
    ;;
  clean)
    check_dependencies
    if [ -z "$repo" ]; then
      for list in aur community core extra; do
        repo="$list"
        clean_packages
      done
    else
      clean_packages
    fi
    ;;
esac

exit 0