functions: changed pkginstalled() to use $* instead of "$@" when generating the pattern
[opensde-nopast.git] / bin / sde-update-package
blob9aaad29fa0fbc43f759249280e2f16556e81d112
1 #!/bin/sh
2 # --- SDE-COPYRIGHT-NOTE-BEGIN ---
3 # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
5 # Filename: bin/sde-update-package
6 # Copyright (C) 2006 - 2008 The OpenSDE Project
8 # More information can be found in the files COPYING and README.
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; version 2 of the License. A copy of the
13 # GNU General Public License can be found in the file COPYING.
14 # --- SDE-COPYRIGHT-NOTE-END ---
16 #Description: Updates a package to a new upstream version
17 #Alias: pkg
19 [ -n "$SDEROOT" ] ||
20 export SDEROOT=$( cd "${0%/*}/.."; pwd -P )
22 . "$SDEROOT/lib/libsde.in"
23 . "$SDEROOT/lib/sde-package.in"
25 update_usage() {
26 local progname=${0##*/}
27 cat <<EOT
28 Usage: ${progname//-/ } [--location <url>] [<package>] [<version>]
29 Updates a package (which is autodetected if you are on it) to a
30 given version, or simply download and regenerate checksums.
31 Using --location you are able to specify which will be the new
32 download location for this update.
34 ${progname//-/ } [--no-location] --md5 <url> (Alias: up)
35 Updates a set of packages based on a remote .md5 file
36 Using --no-location you tell the tool to not assume the download
37 location of the md5 file for the updates but keep their individual
38 download locations.
39 EOT
42 shortopts=
43 longopts='help,location:,no-location,md5:'
44 options=$( getopt -o "$shortopts" -l "$longopts" -- "$@" )
45 if [ $? -ne 0 ]; then
46 update_usage
47 exit -1
50 # load new arguments list
51 eval set -- "$options"
53 location=
54 nolocation=
55 md5file=
57 while [ $# -gt 0 ]; do
58 case "$1" in
59 --help)
60 update_usage
61 exit 0 ;;
63 --location)
64 location=$2; shift
66 --no-location)
67 nolocation=yes
69 --md5)
70 md5file=$2; shift
73 --) shift; break ;;
74 *) echo_abort 1 "Unknown argument '$1', aborting."
75 esac
76 shift
77 done
79 update_patch_package() {
80 local descfile="$1" ver="$2" location="$3"
81 local oldver= sedopt=
82 local tmpfile=$( mktemp )
84 oldver=$( sed -n -e 's,^\[V\][ \t]\+\([^ \t]\+\)[ \t]*,\1,p' "$descfile" | head -n 1 )
85 echo_info "$pkg ($oldver -> $ver)"
87 gawk -f $SDEROOT/lib/sde-package/package-update.awk -v "ver=$ver" -v "location=$location" "$descfile" > $tmpfile
88 if diff -u $descfile $tmpfile; then
89 echo_warning "No change detected."
90 else
91 cp $tmpfile "$descfile"
93 rm -f $tmpfile
96 update_package() {
97 local pkg="$1" ver="$2" location="$3"
98 local confdir=$( package_confdir "$pkg" )
99 local descfile="${confdir}/${pkg}.desc"
100 local oldver=
102 if [ -z "$confdir" -o ! -f "$descfile" ]; then
103 echo_error "Package '$pkg' doesn't exist."
104 return 1
105 elif [ -z "$ver" ]; then
106 echo_info "Updating checksum for $pkg."
107 else
108 update_patch_package "$descfile" "$ver" "$location"
111 cd "$SDEROOT"
112 ./bin/sde-download -q "$pkg" && ./lib/sde-package/patch-cksum.sh "$pkg" | patch -p0
115 update_package_md5() {
116 local md5="$1" baseurl=
117 local nolocation= location=
118 shift
120 echo_info "Loading MD5 file from '$md5'"
121 if [ "$1" = "--no-location" ]; then
122 nolocation=yes; shift
123 echo_info "(Using individual download locations)"
126 baseurl="${md5%/*}"
127 $SDEROOT/bin/sde-parse-md5 "$md5" | while read pkg ver file; do
128 if [ -z "$nolocation" ]; then
129 location="$baseurl/$file";
130 update_package "$pkg" "$ver" "${location%/*}/"
131 else
132 update_package "$pkg" "$ver"
134 done
137 if [ -n "$md5file" ]; then
138 update_package_md5 "$md5file" ${nolocation:+--no-location}
139 return $?
140 elif [ $# -eq 2 ]; then
141 # package and version
142 pkg="$( echo "$1" | tr A-Z a-z)"
143 ver="$2"
144 elif [ $# -eq 1 ]; then
145 # package or version?
146 pkg="$( echo "$1" | tr A-Z a-z)"
147 if [ ! -d "$( package_confdir "$pkg" )" ]; then
148 pkg="$( package_autodetect )"
149 ver="$1"
151 elif [ $# -eq 0 ]; then
152 # can i refresh an autodetected package?
153 pkg=$( package_autodetect )
154 else
155 echo_error "Invalid Syntax."
156 update_usage
157 exit 1
160 if [ -n "$pkg" ]; then
161 update_package "$pkg" "$ver" "${location}"
162 else
163 echo_error "I could't guess which package you want to update."
164 update_usage
165 exit 2