]> pd.if.org Git - zpackage/blob - zpm-repo
use getopts in gc
[zpackage] / zpm-repo
1 #!/bin/sh
2
3 # control repositories
4
5 # repo ...
6 # add name <url>
7 # del name
8 # pri name <n>
9 # list [-v ] [ name ... ]
10 # note name <note>
11 # url name [url]
12 # packages [ name ... ] -- like zpm list
13 # contents [ name ... ] -- like zpm contents
14 # search [ name ] -- packages
15 # push name ckagefile ...
16 # pull name package ...
17 # extract -- extract metadata from repo
18 # json - extract metadata as json
19 # setpkgurl packageid url
20 # update - pull info from remote
21 # gc [name] - run zpm gc on repo file
22
23 chroot=1
24 pkgroot=
25
26 while getopts :R:d: opt; do
27         case $opt in
28                 R) pkgroot="$OPTARG" ;;
29                 d) ZPMDB="$OPTARG" ;;
30         esac
31 done
32
33 shift $((OPTIND - 1))
34
35 for cf in /etc/zpmrc ~/.zpmrc ./.zpmrc; do
36         test -r $cf && . $cf
37 done
38
39 pkgroot=${pkgroot%/}
40
41 : ${ZPMDB:=$pkgroot/var/lib/zpm/local.db}
42
43 repodir=/var/lib/zpm/repo
44 mkdir -p $repodir
45
46 export ZPMDB
47
48 warn() {
49         printf '%s:' "$0" 1>&2
50         printf ' %s' $* 1>&2
51         printf '\n' 1>&2
52         exit 1
53 }
54
55 die() {
56         printf '%s:' "$0" 1>&2
57         printf ' %s' $* 1>&2
58         printf '\n' 1>&2
59         exit 1
60 }
61
62 set -e
63 zpm test -v $ZPMDB
64
65 if [ $# -gt 0 ]; then
66         action=$1
67         shift
68 else
69         action=list
70 fi
71
72 select() (
73         name=$(zpm quote "$1")
74         cols=$2
75         if [ -n "$name" ]; then
76                 where="where name = '$name'"
77         fi
78         zpm shell -separator ' ' $ZPMDB "select $cols from repository $where order by priority,name"
79 )
80
81 update() (
82         name=$(zpm quote "$1")
83         col=$2
84         val=$(zpm quote "$3")
85         if [ -n "$name" ]; then
86                 where="where name = '$name'"
87         fi
88         zpm shell $ZPMDB "update repository set $col = '$val' $where"
89 )
90
91 refresh() {
92         zpm shell $ZPMDB "update repository set refreshed = strftime('%s', 'now') where name = '$1'"
93 }
94
95 update_info() {
96         if [ $# -eq 0 ]; then
97                 set $(select '' name)
98         fi
99         for repo in "$@"; do
100                 url=$(select "$repo" url)
101                 rf="$repodir/$repo.repo"
102                 if [ -z "$url" ]; then
103                         check_for_repo "$repo" || warn "no url for repo $repo"
104                         return 1
105                 fi
106                 if [ -f "$rf" ]; then
107                         # TODO merge in so packages aren't deleted
108                         curl -f '-#' -z "$rf" -o "$rf.tmp" "$url"
109                         rv=$?
110                 else
111                         curl -f '-#' -o "$rf.tmp" "$url" && refresh "$repo"
112                         rv=$?
113                 fi
114                 if [ $rv -eq 0 ]; then
115                         test -f "$rf.tmp" &&
116                         zpm test "$rf.tmp" &&
117                         mv "$rf.tmp" "$rf"
118                         refresh "$repo"
119                 fi
120         done
121 }
122
123 # response=$(curl --write-out %{http_code} --silent --output /dev/null servername)
124 # --head
125
126 head_response_code() {
127         curl --write-out '%{http_code}' --silent --output /dev/null -z $2 $1
128 }
129
130 download() {
131         repo=$1
132         shift
133         url=$(select "$repo" url)
134         base=${url%/*}
135         for package in "$@"; do
136                 pkgid=$(zpm findpkg -f $repodir/$repo.repo $package)
137                 if [ -z "$pkgid" ]; then
138                         warn "no package found for $package"
139                 else
140                         mkdir -p "$repodir/$repo"
141                         dest="$repodir/$repo/$pkgid.zpm"
142                         fetch="$base/packages/$pkgid.zpm"
143
144                         if [ -f "$dest" ]; then
145                                 rc=$(head_response_code $fetch $dest)
146                                 if [ $rc = 304 ]; then
147                                         echo "have $pkgid"
148                                         continue
149                                 fi
150                                 echo downloading $pkgid
151                                 curl -f '-#' -z "$dest" -o "$dest.tmp" $fetch
152                                 mv "$dest.tmp" "$dest"
153                         else
154                                 echo downloading $pkgid
155                                 curl -f '-#' -o "$dest.tmp" $fetch
156                                 mv "$dest.tmp" "$dest"
157                         fi
158                 fi
159         done
160 }
161
162 purge() {
163         repo=$1
164         shift
165         url=$(select "$repo" url)
166         base=${url%/*}
167         for package in "$@"; do
168                 pkgid=$(zpm findpkg -f $repodir/$repo.repo $package)
169                 if [ -z "$pkgid" ]; then
170                         warn "no package found for $package"
171                 else
172                         mkdir -p "$repodir/$repo"
173                         dest="$repodir/$repo/$pkgid.zpm"
174
175                         if [ -f "$dest" ]; then
176                                 rm $dest
177                         fi
178                         zpm rmpackage -f $repodir/$repo.repo $pkgid
179                 fi
180         done
181 }
182
183 check_for_repo() {
184         rn=$(select "$1" name)
185         if [ -z "$rn" ]; then
186                 warn "no repository $1"
187                 return 1
188         fi
189         return 0
190 }
191
192 case $action in
193         list)
194                 select '' priority,name,url,refreshed ;;
195         add)
196                 name=$(zpm quote "$1")
197                 url=$(zpm quote "$2")
198                 zpm shell $ZPMDB "insert into repository (name,url) values ('$name', '$url')"
199                 ;;
200         del)
201                 name=$(zpm quote "$1")
202                 zpm shell $ZPMDB "delete from repository where name = '$name'"
203                 rm -f $repodir/$1.repo
204                 test -d "$repodir/$repo" && rm -r "$repodir/$repo"
205                 ;;
206         url)
207                 if [ -n "$2" ]; then
208                         update "$1" url "$2"
209                 else
210                         select "$1" url
211                 fi
212                 ;;
213         pri*)
214                 if [ -n "$2" ]; then
215                         update "$1" priority "$2"
216                 else
217                         select "$1" priority
218                 fi
219                 ;;
220         fetch|download)
221                 download "$@"
222                 ;;
223         update|pull)
224                 update_info "$@"
225                 ;;
226         packages)
227                 repo=$1
228                 shift
229                 zpm list -f $repodir/$repo.repo "$@"
230                 ;;
231         contents)
232                 repo=$1
233                 shift
234                 zpm contents -f $repodir/$repo.repo "$@"
235                 ;;
236         purge)
237                 purge "$@"
238                 ;;
239
240         *)
241                 echo unknown action
242                 exit 1
243                 ;;
244 esac