From a27d8701968738dfcc0d266c8279a9d912b6476b Mon Sep 17 00:00:00 2001
From: chrfranke <chrfranke@4ea69e1a-61f1-4043-bf83-b5c94c648137>
Date: Thu, 4 Mar 2010 21:56:41 +0000
Subject: [PATCH] Add 'update-smart-drivedb' script (ticket #59).

git-svn-id: https://smartmontools.svn.sourceforge.net/svnroot/smartmontools/trunk@3072 4ea69e1a-61f1-4043-bf83-b5c94c648137
---
 smartmontools/CHANGELOG                    |   4 +
 smartmontools/Makefile.am                  |   9 ++
 smartmontools/NEWS                         |   1 +
 smartmontools/os_win32/smartctl_vc8.vcproj |   4 +
 smartmontools/os_win32/smartd_vc8.vcproj   |   4 +
 smartmontools/smartctl.8.in                |   3 +
 smartmontools/update-smart-drivedb.in      | 144 +++++++++++++++++++++
 7 files changed, 169 insertions(+)
 create mode 100644 smartmontools/update-smart-drivedb.in

diff --git a/smartmontools/CHANGELOG b/smartmontools/CHANGELOG
index f3d47a35d..c650002d9 100644
--- a/smartmontools/CHANGELOG
+++ b/smartmontools/CHANGELOG
@@ -43,6 +43,10 @@ NOTES FOR FUTURE RELEASES: see TODO file.
 
 <DEVELOPERS: ADDITIONS TO THE CHANGE LOG GO JUST BELOW HERE, PLEASE>
 
+  [CF] Add 'update-smart-drivedb' script (ticket #59).
+       The script updates the drive database from SVN.
+       It is installed if '--enable-drivedb' is configured.
+
   [MS] drivedb.h updates:
        - Seagate Medalist 1720
        - SuperTalent UltraDrive GX SSD
diff --git a/smartmontools/Makefile.am b/smartmontools/Makefile.am
index bf6ecc277..2304291e4 100644
--- a/smartmontools/Makefile.am
+++ b/smartmontools/Makefile.am
@@ -25,6 +25,11 @@ endif
 sbin_PROGRAMS = smartd 	\
 		smartctl
 
+if ENABLE_DRIVEDB
+sbin_SCRIPTS = update-smart-drivedb
+endif
+
+
 smartd_SOURCES =  smartd.cpp      \
                   atacmdnames.cpp \
                   atacmdnames.h   \
@@ -327,6 +332,10 @@ smartd.conf.5.in: smartd.8.in
 	cat $(top_builddir)/tmp.tail >> $(srcdir)/smartd.conf.5.in
 	rm -f $(top_builddir)/tmp.head $(top_builddir)/tmp.tail $(top_builddir)/tmp.directives
 
+update-smart-drivedb: update-smart-drivedb.in config.status
+	$(SHELL) ./config.status --file=$@
+
+
 if INSTALL_INITSCRIPT
 if OS_DARWIN
 initd_DATA = SMART                            \
diff --git a/smartmontools/NEWS b/smartmontools/NEWS
index 12e37973c..a1b9fcae7 100644
--- a/smartmontools/NEWS
+++ b/smartmontools/NEWS
@@ -12,6 +12,7 @@ Summary: smartmontools release 5.40
 - configure: New default value for '--with-docdir'.
 - Drive database is in a separate source file 'drivedb.h'
   which can be downloaded from SVN.
+- New script 'update-smart-drivedb'.
 - smartd libcap-ng support, option '-C, --capabilities'.
 - smartctl option '-l scterc[,...]' to get/set the
   SCT Error Recovery Control time limit.
diff --git a/smartmontools/os_win32/smartctl_vc8.vcproj b/smartmontools/os_win32/smartctl_vc8.vcproj
index d53105252..980ff787f 100644
--- a/smartmontools/os_win32/smartctl_vc8.vcproj
+++ b/smartmontools/os_win32/smartctl_vc8.vcproj
@@ -1037,6 +1037,10 @@
 			RelativePath="..\TODO"
 			>
 		</File>
+		<File
+			RelativePath="..\update-smart-drivedb.in"
+			>
+		</File>
 		<File
 			RelativePath="..\utility.cpp"
 			>
diff --git a/smartmontools/os_win32/smartd_vc8.vcproj b/smartmontools/os_win32/smartd_vc8.vcproj
index 2f7b69b90..807d4a2f0 100644
--- a/smartmontools/os_win32/smartd_vc8.vcproj
+++ b/smartmontools/os_win32/smartd_vc8.vcproj
@@ -989,6 +989,10 @@
 			RelativePath="..\TODO"
 			>
 		</File>
+		<File
+			RelativePath="..\update-smart-drivedb.in"
+			>
+		</File>
 		<File
 			RelativePath="..\utility.cpp"
 			>
diff --git a/smartmontools/smartctl.8.in b/smartmontools/smartctl.8.in
index a855c1cbb..a2245cc82 100644
--- a/smartmontools/smartctl.8.in
+++ b/smartmontools/smartctl.8.in
@@ -1313,6 +1313,9 @@ If this option is not specified, optional entries are read from the file
 .\" BEGIN ENABLE_DRIVEDB
 If \fB/usr/local/share/smartmontools/drivedb.h\fP is present, the
 contents of this file is used instead of the built in table.
+
+Run the script \fB/usr/local/sbin/update-smart-drivedb\fP to update this
+file from the smartmontools SVN repository.
 .\" END ENABLE_DRIVEDB
 
 The database files use the same C/C++ syntax that is used to initialize
diff --git a/smartmontools/update-smart-drivedb.in b/smartmontools/update-smart-drivedb.in
new file mode 100644
index 000000000..0f4ac7e19
--- /dev/null
+++ b/smartmontools/update-smart-drivedb.in
@@ -0,0 +1,144 @@
+#! /bin/sh
+#
+# smartmontools drive database update script
+#
+# Copyright (C) 2010 Christian Franke <smartmontools-support@lists.sourceforge.net>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# You should have received a copy of the GNU General Public License
+# (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
+#
+# $Id$
+#
+
+set -e
+
+# Set by config.status
+PACKAGE="@PACKAGE@"
+VERSION="@VERSION@"
+prefix="@prefix@"
+exec_prefix="@exec_prefix@"
+sbindir="@sbindir@"
+datarootdir="@datarootdir@"
+datadir="@datadir@"
+drivedbdir="@drivedbdir@"
+
+# Default drivedb location
+DEST="$drivedbdir/drivedb.h"
+
+# Smartctl used for syntax check
+SMARTCTL="$sbindir/smartctl"
+
+# Trac repository browser (does not return HTTP 404 errors)
+#SRCEXPR='http://sourceforge.net/apps/trac/smartmontools/export/HEAD/$location/smartmontools/drivedb.h'
+
+# ViewVC repository browser
+SRCEXPR='http://smartmontools.svn.sourceforge.net/viewvc/smartmontools/$location/smartmontools/drivedb.h?revision=HEAD'
+
+# Convert version into branch name: 5.41[.X] -> "RELEASE_5_41_DRIVEDB"
+BRANCH="`echo $VERSION | sed -n 's|^\([0-9][0-9]*\)\.\([0-9][0-9]*\)\([^0-9].*\)\?$|RELEASE_\1_\2_DRIVEDB|p'`"
+
+if [ -z "$BRANCH" ]; then
+  echo "$0: syntax error in version number: $VERSION" >&2; exit 1
+fi
+
+
+# Parse options
+q="-q "
+case "$1" in
+  -v) q=; shift ;;
+esac
+
+case "$*" in
+  -*|*\ *)
+    cat <<EOF
+smartmontools $VERSION drive database update script
+
+Usage: $0 [-v] [DESTFILE]
+
+  -v    verbose output
+
+Updates $DEST
+or DESTFILE from smartmontools SVN repository.
+Tries to download first from branch $BRANCH
+and then from trunk.
+EOF
+    exit 1
+    ;;
+
+  "") ;;
+  *)  DEST="$1" ;;
+esac
+
+# Abort if 'which' is not available
+which which >/dev/null || exit 1
+
+# Find download tool
+if which curl >/dev/null 2>/dev/null; then
+  DOWNLOAD="curl ${q:+-s }"'-f -o "$DEST.new" "$SRC"'
+elif which wget >/dev/null 2>/dev/null; then
+  DOWNLOAD="wget $q"'-O "$DEST.new" "$SRC"'
+elif which lynx >/dev/null 2>/dev/null; then
+  DOWNLOAD='lynx -source "$SRC" >"$DEST.new"'
+else
+  echo "$0: curl, wget or lynx not available" >&2; exit 1
+fi
+
+# Try possible branch first, then trunk
+for location in "branches/$BRANCH" "trunk"; do
+  test -n "$q" || echo "Download from $location"
+
+  errmsg=
+  rm -f "$DEST.new"
+  SRC="`eval echo "$SRCEXPR"`"
+
+  if eval $DOWNLOAD; then :; else
+    errmsg="download from $location failed (HTTP error)"
+    continue
+  fi
+  if grep -i 'ViewVC Exception' "$DEST.new" >/dev/null; then
+    errmsg="download from $location failed (ViewVC error)"
+    continue
+  fi
+
+  break
+done
+
+if [ -n "$errmsg" ]; then
+  rm -f "$DEST.new"
+  echo "$0: $errmsg" >&2
+  exit 1
+fi
+
+# Adjust timestamp and permissions
+touch "$DEST.new"
+chmod 0644 "$DEST.new"
+
+# Check syntax
+rm -f "$DEST.error"
+if $SMARTCTL -B "$DEST.new" -P showall >/dev/null; then :; else
+  mv "$DEST.new" "$DEST.error"
+  echo "$DEST.error: rejected by $SMARTCTL, probably no longer compatible" >&2
+  exit 1
+fi
+
+# Keep old file if identical, ignore differences in Id string
+rm -f "$DEST.lastcheck"
+if [ -f "$DEST" ]; then
+  if cat "$DEST" | sed 's|\$''Id''[^$]*\$|$''Id''$|' | cmp - "$DEST.new" >/dev/null; then
+    rm -f "$DEST.new"
+    touch "$DEST.lastcheck"
+    echo "$DEST is already up to date"
+    exit 0
+  fi
+  mv "$DEST" "$DEST.old"
+fi
+
+mv "$DEST.new" "$DEST"
+
+echo "$DEST updated from $location"
+
-- 
GitLab