* Updated Italian translation
[pacman.git] / scripts / repo-add
blobd6e2c5aef62f1e8a551c990b2182c935f4d808c7
1 #!/bin/bash
3 # repo-add : add a package to a given repo database file
4 #
5 # Copyright (c) 2006 Aaron Griffin <aaron@archlinux.org>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 # USA.
22 myver='3.0.0'
24 FORCE=0
25 REPO_DB_FILE=""
27 DB_COMPRESSION="gz" #TODO this is gross
28 DB_CHECKSUMS=(md5)
29 TMP_DIR=""
31 # print usage instructions
32 usage() {
33 echo "repo-add $myver"
34 echo
35 echo "usage: repo-add <path-to-db> [--force] <package> ..."
36 echo
37 echo "repo-add will update a package database by reading a package file."
38 echo "Multiple packages to add can be specified on the command line."
39 echo
40 echo "The --force flag will add a 'force' entry to the sync database, which"
41 echo "tells pacman to skip its internal version number checking and update"
42 echo "the package regardless."
43 echo
44 echo "Example:"
45 echo " repo-add /path/to/repo.db.tar.gz pacman-3.0.0.pkg.tar.gz"
46 echo
49 # return calculated checksum of package
50 # arg1 - checksum type
51 # arg2 - path to package
52 get_checksum () {
53 case "$(echo "$1" | tr A-Z a-z)" in
54 md5) sum=$(md5sum $2); echo ${sum% *} ;;
55 sha1) sum=$(sha1sum $2); echo ${sum% *} ;;
56 sha256) sum=$(sha256sum $2); echo ${sum% *} ;;
57 sha384) sum=$(sha256sum $2); echo ${sum% *} ;;
58 sha512) sum=$(sha256sum $2); echo ${sum% *} ;;
59 esac
62 # return PKGINFO string for checksum type
63 # arg1 - checksum type
64 checksum_name () {
65 case "$(echo "$1" | tr A-Z a-z)" in
66 md5) echo "MD5SUM" ;;
67 sha1) echo "SHA1SUM" ;;
68 sha256) echo "SHA256SUM" ;;
69 sha384) echo "SHA384SUM" ;;
70 sha512) echo "SHA512SUM" ;;
71 esac
74 # test if a file is a repository DB
75 test_repo_db_file () {
76 if [ -f "$REPO_DB_FILE" ]; then
77 [ "$(tar tf "$REPO_DB_FILE" | grep -c "/desc")" -gt 0 ] || return 1
78 else
79 true
83 # write an entry to the pacman database
84 # arg1 - path to package
85 db_write_entry()
87 # blank out all variables and set pkgfile
88 pkgfile=$(readlink -f $1)
89 export pkgname=""
90 pkgver=""
91 pkgdesc=""
92 url=""
93 builddate=""
94 packager=""
95 csize=""
96 size=""
97 _groups=""
98 _depends=""
99 _backups=""
100 _licenses=""
101 _replaces=""
102 _provides=""
103 _conflicts=""
105 OLDIFS="$IFS"
106 # IFS (field seperator) is only the newline character
107 IFS="
110 # read info from the zipped package
111 for i in $(tar xOf "$pkgfile" .PKGINFO | grep -v "^#" |sed 's|\(\w*\)\s*=\s*\(.*\)|\1="\2"|'); do
112 eval "${i}"
113 case "$i" in
114 group=*) _groups="$_groups $group" ;;
115 depend=*) _depends="$_depends $depend" ;;
116 backup=*) _backups="$_backups $backup" ;;
117 license=*) _licenses="$_licenses $license" ;;
118 replaces=*) _replaces="$_replaces $replaces" ;;
119 provides=*) _provides="$_provides $provides" ;;
120 conflicts=*) _conflicts="$_conflicts $conflicts" ;;
121 esac
122 done
124 IFS=$OLDIFS
126 # get compressed size of package
127 csize="$(du -b $pkgfile | cut -f1)"
129 cd $gstmpdir
131 # ensure $pkgname and $pkgver variables were found
132 if [ -z "$pkgname" -o -z "$pkgver" ]; then
133 echo " error: invalid package file"
134 return 1
137 # remove any other package in the DB with same name
138 for existing in *; do
139 if [ "${existing%-*-*}" = "$pkgname" ]; then
140 echo ":: removing existing package '$existing'"
141 rm -rf $existing
143 done
145 # create package directory
146 mkdir "$pkgname-$pkgver"
147 cd "$pkgname-$pkgver"
149 # create desc entry
150 echo ":: creating 'desc' db entry"
151 echo -e "%FILENAME%\n$(basename $1)\n" >> desc
152 echo -e "%NAME%\n$pkgname\n" >>desc
153 echo -e "%VERSION%\n$pkgver\n" >>desc
154 if [ -n "$pkgdesc" ]; then
155 echo -e "%DESC%\n$pkgdesc\n" >>desc
157 if [ -n "$_groups" ]; then
158 echo "%GROUPS%" >>desc
159 echo $_groups | tr -s ' ' '\n' >>desc
160 echo "" >desc
162 [ -n $csize ] && echo -e "%CSIZE%\n$csize\n" >>desc
163 [ -n $size ] && echo -e "%ISIZE%\n$size\n" >>desc
165 # compute checksums
166 for chk in ${DB_CHECKSUMS[@]}; do
167 name="$(checksum_name $chk)"
168 echo ":: computing $name checksums"
169 if [ -n "$name" ]; then
170 echo -e "%$name%\n$(get_checksum $chk $pkgfile)\n" >>desc
172 done
174 [ -n "$url" ] && echo -e "%URL%\n$url\n" >>desc
175 if [ -n "$_licenses" ]; then
176 echo "%LICENSE%" >>desc
177 echo $_licenses | tr -s ' ' '\n' >>desc
178 echo "" >>desc
180 [ -n "$arch" ] && echo -e "%ARCH%\n$arch\n" >>desc
181 [ -n "$builddate" ] && echo -e "%BUILDDATE%\n$builddate\n" >>desc
182 [ -n "$packager" ] && echo -e "%PACKAGER%\n$packager\n" >>desc
184 if [ -n "$_replaces" ]; then
185 echo "%REPLACES%" >>desc
186 echo $_replaces | tr -s ' ' '\n' >>desc
187 echo "" >>desc
189 [ "$FORCE" = "1" ] && echo -e "%FORCE%\n" >>desc
191 # create depends entry
192 echo ":: creating 'depends' db entry"
193 if [ -n "$_depends" ]; then
194 echo "%DEPENDS%" >>depends
195 echo $_depends | tr -s ' ' '\n' >>depends
196 echo "" >>depends
198 if [ -n "$_conflicts" ]; then
199 echo "%CONFLICTS%" >>depends
200 echo $_conflicts | tr -s ' ' '\n' >>depends
201 echo "" >>depends
203 if [ -n "$_provides" ]; then
204 echo "%PROVIDES%" >>depends
205 echo $_provides | tr -s ' ' '\n' >>depends
206 echo "" >>depends
209 # preserve the modification time
210 touch -r "$pkgfile" desc depends
211 } # end db_write_entry
213 # PROGRAM START
215 # check for help flags
216 if [ "$1" = "-h" -o "$1" = "--help" ]; then
217 usage
218 exit 0
221 # check for correct number of args
222 if [ $# -lt 2 ]; then
223 usage
224 exit 1
227 # main routine
228 if [ $# -gt 1 ]; then
229 gstmpdir=$(mktemp -d /tmp/gensync.XXXXXXXXXX) || (\
230 echo "cannot create temp directory for database building"; \
231 exit 1)
233 success=0
234 # parse arguements
235 for arg in $@; do
236 if [ "$arg" == "--force" -o "$arg" == "-f" ]; then
237 FORCE=1
238 elif [ -z "$REPO_DB_FILE" ]; then
239 REPO_DB_FILE="$(readlink -f $arg)"
240 if ! test_repo_db_file; then
241 echo "error: repository file '$REPO_DB_FILE' is not a proper pacman db"
242 exit 1
243 elif [ -f "$REPO_DB_FILE" ]; then
244 echo ":: extracting database to a temporary location"
245 tar xf "$REPO_DB_FILE" -C "$gstmpdir"
247 else
248 if [ -f "$arg" ]; then
249 if ! tar xf "$arg" .PKGINFO 2>&1 >/dev/null; then
250 echo "error: '$arg' is not a package file, skipping"
251 else
252 echo ":: adding package '$arg'"
254 this_dir="$(pwd)"
255 if db_write_entry "$arg"; then
256 success=1
258 cd $this_dir
260 else
261 echo "error: package '$arg' not found"
264 done
266 # if all operations were a success, rezip database
267 if [ "$success" = "1" ]; then
268 echo ":: creating updated database file ${REPO_DB_FILE}"
269 cd $gstmpdir
270 if [ -n "$(ls)" ]; then
271 [ -f "${REPO_DB_FILE}.old" ] && rm "${REPO_DB_FILE}.old"
272 [ -f "$REPO_DB_FILE" ] && mv "$REPO_DB_FILE" "${REPO_DB_FILE}.old"
273 case "$DB_COMPRESSION" in
274 gz) tar c * | gzip -9 >$REPO_DB_FILE ;;
275 bz2) tar c * | bzip2 -9 >$REPO_DB_FILE ;;
276 *) echo "warning: no compression set"
277 tar c * >$REPO_DB_FILE;;
278 esac
280 else
281 echo ":: no packages modified, nothing to do"
285 # remove the temp directory used to unzip
286 [ -d "$gstmpdir" ] && rm -rf $gstmpdir
288 # vim: set ts=2 sw=2 noet: