]> git.proxmox.com Git - mirror_smartmontools-debian.git/blob - update-smart-drivedb.in
Fixed example path in man pages
[mirror_smartmontools-debian.git] / update-smart-drivedb.in
1 #! /bin/sh
2 #
3 # smartmontools drive database update script
4 #
5 # Copyright (C) 2010 Christian Franke <smartmontools-support@lists.sourceforge.net>
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, or (at your option)
10 # any later version.
11 #
12 # You should have received a copy of the GNU General Public License
13 # (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
14 #
15 # $Id: update-smart-drivedb.in 3072 2010-03-04 21:56:41Z chrfranke $
16 #
17
18 set -e
19
20 # Set by config.status
21 PACKAGE="@PACKAGE@"
22 VERSION="@VERSION@"
23 prefix="@prefix@"
24 exec_prefix="@exec_prefix@"
25 sbindir="@sbindir@"
26 datarootdir="@datarootdir@"
27 datadir="@datadir@"
28 drivedbdir="@drivedbdir@"
29
30 # Default drivedb location
31 DEST="$drivedbdir/drivedb.h"
32
33 # Smartctl used for syntax check
34 SMARTCTL="$sbindir/smartctl"
35
36 # Trac repository browser (does not return HTTP 404 errors)
37 #SRCEXPR='http://sourceforge.net/apps/trac/smartmontools/export/HEAD/$location/smartmontools/drivedb.h'
38
39 # ViewVC repository browser
40 SRCEXPR='http://smartmontools.svn.sourceforge.net/viewvc/smartmontools/$location/smartmontools/drivedb.h?revision=HEAD'
41
42 # Convert version into branch name: 5.41[.X] -> "RELEASE_5_41_DRIVEDB"
43 BRANCH="`echo $VERSION | sed -n 's|^\([0-9][0-9]*\)\.\([0-9][0-9]*\)\([^0-9].*\)\?$|RELEASE_\1_\2_DRIVEDB|p'`"
44
45 if [ -z "$BRANCH" ]; then
46 echo "$0: syntax error in version number: $VERSION" >&2; exit 1
47 fi
48
49
50 # Parse options
51 q="-q "
52 case "$1" in
53 -v) q=; shift ;;
54 esac
55
56 case "$*" in
57 -*|*\ *)
58 cat <<EOF
59 smartmontools $VERSION drive database update script
60
61 Usage: $0 [-v] [DESTFILE]
62
63 -v verbose output
64
65 Updates $DEST
66 or DESTFILE from smartmontools SVN repository.
67 Tries to download first from branch $BRANCH
68 and then from trunk.
69 EOF
70 exit 1
71 ;;
72
73 "") ;;
74 *) DEST="$1" ;;
75 esac
76
77 # Abort if 'which' is not available
78 which which >/dev/null || exit 1
79
80 # Find download tool
81 if which curl >/dev/null 2>/dev/null; then
82 DOWNLOAD="curl ${q:+-s }"'-f -o "$DEST.new" "$SRC"'
83 elif which wget >/dev/null 2>/dev/null; then
84 DOWNLOAD="wget $q"'-O "$DEST.new" "$SRC"'
85 elif which lynx >/dev/null 2>/dev/null; then
86 DOWNLOAD='lynx -source "$SRC" >"$DEST.new"'
87 else
88 echo "$0: curl, wget or lynx not available" >&2; exit 1
89 fi
90
91 # Try possible branch first, then trunk
92 for location in "branches/$BRANCH" "trunk"; do
93 test -n "$q" || echo "Download from $location"
94
95 errmsg=
96 rm -f "$DEST.new"
97 SRC="`eval echo "$SRCEXPR"`"
98
99 if eval $DOWNLOAD; then :; else
100 errmsg="download from $location failed (HTTP error)"
101 continue
102 fi
103 if grep -i 'ViewVC Exception' "$DEST.new" >/dev/null; then
104 errmsg="download from $location failed (ViewVC error)"
105 continue
106 fi
107
108 break
109 done
110
111 if [ -n "$errmsg" ]; then
112 rm -f "$DEST.new"
113 echo "$0: $errmsg" >&2
114 exit 1
115 fi
116
117 # Adjust timestamp and permissions
118 touch "$DEST.new"
119 chmod 0644 "$DEST.new"
120
121 # Check syntax
122 rm -f "$DEST.error"
123 if $SMARTCTL -B "$DEST.new" -P showall >/dev/null; then :; else
124 mv "$DEST.new" "$DEST.error"
125 echo "$DEST.error: rejected by $SMARTCTL, probably no longer compatible" >&2
126 exit 1
127 fi
128
129 # Keep old file if identical, ignore differences in Id string
130 rm -f "$DEST.lastcheck"
131 if [ -f "$DEST" ]; then
132 if cat "$DEST" | sed 's|\$''Id''[^$]*\$|$''Id''$|' | cmp - "$DEST.new" >/dev/null; then
133 rm -f "$DEST.new"
134 touch "$DEST.lastcheck"
135 echo "$DEST is already up to date"
136 exit 0
137 fi
138 mv "$DEST" "$DEST.old"
139 fi
140
141 mv "$DEST.new" "$DEST"
142
143 echo "$DEST updated from $location"
144