aboutsummaryrefslogtreecommitdiffstats
path: root/bin/pkgs
blob: 59f2d9cb88d40f3e77c03f080858db8a55cf9bdc (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
#!/usr/bin/env bash

set -euo pipefail

aur_packagedir="aur-maintain"
aur_packagelist="$HOME/.config/packages-aur.txt"
community_packagedir="svn-community"
community_upstream="svn+ssh://svn-community@repos.archlinux.org/srv/repos/svn-community/svn"
community_packagelist="$HOME/.config/packages-community.txt"
packagedir="$HOME/packages/"

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

add_aur_package() {
  local tmp_file=$(create_tmp_packagelist "$aur_packagelist")
  echo $1 | cat "$aur_packagelist" - |uniq |sort > "$tmp_file"
  cat "$tmp_file" > "$aur_packagelist"
  rm "$tmp_file"
}

add_community_package() {
  local tmp_file=$(create_tmp_packagelist "$community_packagelist")
  echo $1 | cat "$community_packagelist" - |uniq |sort > "$tmp_file"
  cat "$tmp_file" > "$community_packagelist"
  rm "$tmp_file"
}

remove_aur_package() {
  local tmp_file=$(create_tmp_packagelist "$aur_packagelist")
  cat "$aur_packagelist" |grep -vE "^$1$" |uniq |sort > "$tmp_file"
  cat "$tmp_file" > "$aur_packagelist"
  rm "$tmp_file"
}

remove_community_package() {
  local tmp_file=$(create_tmp_packagelist "$community_packagelist")
  cat "$community_packagelist" |grep -vE "^$1$" |uniq |sort > "$tmp_file"
  cat "$tmp_file" > "$community_packagelist"
  rm "$tmp_file"
}

create_aur_dir() {
  if [ ! -d "$packagedir$aur_packagedir" ];then
    echo "Creating AUR dir."
    mkdir -pv "$packagedir$aur_packagedir"
  fi
}

create_aur_dir() {
  if [ ! -d "$packagedir$aur_packagedir" ];then
    echo "Creating AUR dir."
    mkdir -pv "$packagedir$aur_packagedir"
  fi
}

create_community_dir() {
  if [ ! -d "$packagedir$community_packagedir" ];then
    echo "Checking out [community] folder the first time."
    cd $packagedir
    svn checkout -N $community_upstream svn-community
  fi
}

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

clean_aur_packages() {
  if [ -d "$packagedir$aur_packagedir" ];then
    cd "$packagedir$aur_packagedir"
    for package in $(cat "$aur_packagelist"); do
      if [ -d $package ]; then
        echo "Cleaning package '$package'."
        cd $package
        git clean -f
        cd ..
      fi
    done
  else
    echo "There are no AUR packages."
  fi
}

clean_community_packages() {
  if [ -d "$packagedir$community_packagedir" ]; then
    cd "$packagedir$community_packagedir"
    for package in $(cat "$community_packagelist"); do
      if [ -d $package ]; then
        cd $package
        svn cleanup --remove-unversioned .
        cd ..
      fi
    done
  else
    echo "There are no community packages."
  fi
}

update_aur_packages() {
  create_aur_dir
  cd "$packagedir$aur_packagedir"
  for package in $(cat "$aur_packagelist"); do
    if [ -d $package ]; then
      echo "Pulling package '$package'."
      cd $package
      git pull
      cd ..
    else
      echo "Cloning package '$package' from the AUR for the first time."
      git clone aur@aur.archlinux.org:$package
    fi
  done
}

update_community_packages() {
  create_community_dir
  cd "$packagedir$community_packagedir"
  for package in $(cat "$community_packagelist"); do
    svn update $package
  done
}

add_mode=0
aur_mode=0
clean_mode=0
community_mode=0
list_mode=0
remove_mode=0
update_mode=0
package=""

if [ ${#@} -gt 0 ]; then
  while getopts ':a:chlur:AC' flag; do
    case "${flag}" in
      a)
        add_mode=1
        if [ -n "${OPTARG}" ]; then
          package="${OPTARG}"
        else
          echo "Package name can not be empty."
          exit 1
        fi
        ;;
      c)
        clean_mode=1
        ;;
      h)
        print_help
        ;;
      l)
        list_mode=1
        ;;
      r)
        remove_mode=1
        if [ -n "${OPTARG}" ]; then
          package="${OPTARG}"
        else
          echo "Package name can not be empty."
          exit 1
        fi
        ;;
      u)
        update_mode=1
        ;;
      A)
        aur_mode=1
        ;;
      C)
        community_mode=1
        ;;
      *)
        echo "Error. Unrecognized option: ${flag}."
        exit 1
        ;;
    esac
  done
else
  print_help
fi

# incompatible modes
if [ $add_mode -eq 1 ] && \
  [ $remove_mode -eq 1 ]; then
  echo "Add and remove mode are not complementary."
  print_help
  exit 1
fi

# add_mode (works with aur_mode and community_mode)
if [ $add_mode -eq 1 ] && \
  [ $aur_mode -eq 0 ] && \
  [ $community_mode -eq 0 ];then
  echo "A repository must be defined for adding a package."
  print_help
  exit 1
elif [ $add_mode -eq 1 ] && \
  [ $aur_mode -eq 1 ] && \
  [ $community_mode -eq 1 ];then
  echo "Only one repository can be defined for adding a package."
  print_help
  exit 1
elif [ $add_mode -eq 1 ] && \
  [ $aur_mode -eq 0 ] && \
  [ $community_mode -eq 1 ];then
  echo "Adding $package to list of community packages."
  add_community_package $package
elif [ $add_mode -eq 1 ] && \
  [ $aur_mode -eq 1 ] && \
  [ $community_mode -eq 0 ];then
  echo "Adding $package to list of AUR packages."
  add_aur_package $package
fi

# remove_mode (works with aur_mode and community_mode)
if [ $remove_mode -eq 1 ] && \
  [ $aur_mode -eq 0 ] && \
  [ $community_mode -eq 0 ];then
  echo "A repository must be defined for removing a package."
  print_help
  exit 1
elif [ $remove_mode -eq 1 ] && \
  [ $aur_mode -eq 1 ] && \
  [ $community_mode -eq 1 ];then
  echo "Only one repository can be defined for removing a package."
  print_help
  exit 1
elif [ $remove_mode -eq 1 ] && \
  [ $aur_mode -eq 0 ] && \
  [ $community_mode -eq 1 ];then
  echo "Removing $package from list of community packages."
  remove_community_package $package
elif [ $remove_mode -eq 1 ] && \
  [ $aur_mode -eq 1 ] && \
  [ $community_mode -eq 0 ];then
  echo "Removing $package from list of AUR packages."
  remove_aur_package $package
fi

# list_mode (works with aur_mode and community_mode)
if [ $list_mode -eq 1 ] && \
  [ $aur_mode -eq 0 ] && \
  [ $community_mode -eq 0 ];then
  echo "Define which repository to list."
  print_help
  exit 1
elif [ $list_mode -eq 1 ] && \
  [ $aur_mode -eq 1 ] && \
  [ $community_mode -eq 0 ];then
  cat "$aur_packagelist"
elif [ $list_mode -eq 1 ] && \
  [ $aur_mode -eq 0 ] && \
  [ $community_mode -eq 1 ];then
  cat "$community_packagelist"
elif [ $list_mode -eq 1 ] && \
  [ $aur_mode -eq 1 ] && \
  [ $community_mode -eq 1 ];then
  echo "All packages:"
  cat "$aur_packagelist" "$community_packagelist"
fi

# update_mode (works with aur_mode and community_mode)
if [ $update_mode -eq 1 ] && \
  [ $aur_mode -eq 0 ] && \
  [ $community_mode -eq 0 ];then
  echo "Define which repository to update."
  print_help
  exit 1
elif [ $update_mode -eq 1 ] && \
  [ $aur_mode -eq 1 ] && \
  [ $community_mode -eq 0 ];then
  echo "Updating AUR packages."
  update_aur_packages
elif [ $update_mode -eq 1 ] && \
  [ $aur_mode -eq 0 ] && \
  [ $community_mode -eq 1 ];then
  echo "Updating community packages."
  update_community_packages
elif [ $update_mode -eq 1 ] && \
  [ $aur_mode -eq 1 ] && \
  [ $community_mode -eq 1 ];then
  echo "Updating all packages:"
  update_aur_packages
  update_community_packages
fi

# clean_mode (works with aur_mode and community_mode)
if [ $clean_mode -eq 1 ] && \
  [ $aur_mode -eq 0 ] && \
  [ $community_mode -eq 0 ];then
  echo "Define which repository to clean."
  print_help
  exit 1
elif [ $clean_mode -eq 1 ] && \
  [ $aur_mode -eq 1 ] && \
  [ $community_mode -eq 0 ];then
  echo "Cleaning AUR packages."
  clean_aur_packages
elif [ $clean_mode -eq 1 ] && \
  [ $aur_mode -eq 0 ] && \
  [ $community_mode -eq 1 ];then
  echo "Cleaning community packages."
  clean_community_packages
elif [ $clean_mode -eq 1 ] && \
  [ $aur_mode -eq 1 ] && \
  [ $community_mode -eq 1 ];then
  echo "Cleaning all packages:"
  clean_aur_packages
  clean_community_packages
fi

exit 0