pkgdelta: rework option/argument parser
[pacman-ng.git] / scripts / pkgdelta.sh.in
blob511910bad36c3ff9d2c59c195c62440ff6b9ddf8
1 #!/bin/bash
3 # pkgdelta - create delta files for use with pacman and repo-add
4 # @configure_input@
6 # Copyright (c) 2009 Xavier Chantry <shiningxc@gmail.com>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 # bash options
23 set -o errexit
25 # gettext initialization
26 export TEXTDOMAIN='pacman-scripts'
27 export TEXTDOMAINDIR='@localedir@'
29 myver='@PACKAGE_VERSION@'
31 QUIET=0
33 # ensure we have a sane umask set
34 umask 0022
36 m4_include(library/output_format.sh)
38 # print usage instructions
39 usage() {
40 printf "pkgdelta (pacman) %s\n\n" "$myver"
41 printf -- "$(gettext "Usage: pkgdelta [options] <package1> <package2>\n")"
42 printf -- "$(gettext "\
43 pkgdelta will create a delta file between two packages.\n\
44 This delta file can then be added to a database using repo-add.\n\n")"
45 printf -- "$(gettext "Example: pkgdelta pacman-3.0.0.pkg.tar.gz pacman-3.0.1.pkg.tar.gz")\n"
46 echo
47 printf -- "$(gettext "Options:\n")"
48 printf -- " -q ""$(gettext "quiet\n")"
51 version() {
52 printf "pkgdelta (pacman) %s\n\n" "$myver"
53 printf -- "$(gettext "\
54 Copyright (c) 2009 Xavier Chantry <shiningxc@gmail.com>.\n\n\
55 This is free software; see the source for copying conditions.\n\
56 There is NO WARRANTY, to the extent permitted by law.\n")"
59 read_pkginfo()
61 pkgname= pkgver= arch=
62 local OLDIFS=$IFS
63 # IFS (field separator) is only the newline character
64 IFS="
66 local line var val
67 for line in $(bsdtar -xOqf "$1" .PKGINFO 2>/dev/null |
68 grep -v "^#" | sed 's|\(\w*\)\s*=\s*\(.*\)|\1="\2"|'); do
69 eval "$line"
70 if [[ -n $pkgname && -n $pkgver && -n $arch ]]; then
71 IFS=$OLDIFS
72 return 0
74 done
75 IFS=$OLDIFS
76 error "$(gettext "Invalid package file '%s'.")" "$1"
77 return 1
80 # $oldfile $oldmd5 $newfile $newmd5 $deltafile $deltamd5 $deltasize
81 create_xdelta()
83 local oldfile=$1
84 local newfile=$2
85 local \
86 oldname oldver oldarch \
87 newname newver newarch \
88 deltafile
90 read_pkginfo "$oldfile" || return 1
91 oldname="$pkgname"
92 oldver="$pkgver"
93 oldarch="$arch"
94 read_pkginfo "$newfile" || return 1
95 newname="$pkgname"
96 newver="$pkgver"
97 newarch="$arch"
99 if [[ $oldname != "$newname" ]]; then
100 error "$(gettext "The package names don't match : '%s' and '%s'")" "$oldname" "$newname"
101 return 1
104 if [[ $oldarch != "$newarch" ]]; then
105 error "$(gettext "The package architectures don't match : '%s' and '%s'")" "$oldarch" "$newarch"
106 return 1
109 if [[ $oldver == "$newver" ]]; then
110 error "$(gettext "Both packages have the same version : '%s'")" "$newver"
111 return 1
114 msg "$(gettext "Generating delta from version %s to version %s")" "$oldver" "$newver"
115 deltafile="$(dirname $newfile)/$pkgname-${oldver}_to_${newver}-$arch.delta"
116 local ret=0
118 xdelta3 -q -f -s "$oldfile" "$newfile" "$deltafile" || ret=$?
119 if (( ret )); then
120 error "$(gettext "Delta could not be created.")"
121 return 1
122 else
123 msg "$(gettext "Generated delta : '%s'")" "$deltafile"
124 (( QUIET )) && echo "$deltafile"
126 return 0
129 declare -a args
131 # parse arguments
132 while (( $# )); do
133 case "$1" in
134 -h|--help) usage; exit 0 ;;
135 -V|--version) version; exit 0 ;;
136 -q|--quiet) QUIET=1;;
137 --) shift; args+=("$@"); break 2 ;;
138 -*) echo "invalid option -- '$1'"; usage; exit 1 ;;
139 *) args+=("$1");;
140 esac
141 shift
142 done
144 if (( ${#args[@]} != 2 )); then
145 usage
146 exit 1
149 for i in "${args[@]}"; do
150 if [[ ! -f $i ]]; then
151 error "$(gettext "File '%s' does not exist")" "$i"
152 exit 1
154 done
156 if ! type xdelta3 &>/dev/null; then
157 error "$(gettext "Cannot find the xdelta3 binary! Is xdelta3 installed?")"
158 exit 1
161 create_xdelta "${args[@]}"
163 # vim: set ts=2 sw=2 noet: