File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / smartmontools / update-smart-drivedb.in
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Mon Oct 14 07:54:04 2013 UTC (11 years, 2 months ago) by misho
Branches: smartmontools, elwix, MAIN
CVS tags: v6_2, HEAD
v 6.2

#! /bin/sh
#
# smartmontools drive database update script
#
# Copyright (C) 2010-13 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: update-smart-drivedb.in,v 1.1.1.2 2013/10/14 07:54:04 misho Exp $
#

set -e

# Set by config.status
PACKAGE="@PACKAGE@"
VERSION="@VERSION@"
prefix="@prefix@"
exec_prefix="@exec_prefix@"
sbindir="@sbindir@"
datarootdir="@datarootdir@"
datadir="@datadir@"
drivedbdir="@drivedbdir@"

# Download tools
os_dltools="@os_dltools@"

# drivedb.h update branch
BRANCH="@DRIVEDB_BRANCH@"

# Default drivedb location
DEST="$drivedbdir/drivedb.h"

# Smartctl used for syntax check
SMARTCTL="$sbindir/smartctl"

# Download URL for sourceforge code browser
SRCEXPR='http://sourceforge.net/p/smartmontools/code/HEAD/tree/$location/smartmontools/drivedb.h?format=raw'

# 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
DOWNLOAD=
for t in $os_dltools; do
  if which $t >/dev/null 2>/dev/null; then
    case $t in
      curl)  DOWNLOAD="curl ${q:+-s }"'-f -o "$DEST.new" "$SRC"' ;;
      lynx)  DOWNLOAD='lynx -source "$SRC" >"$DEST.new"' ;;
      wget)  DOWNLOAD="wget $q"'-O "$DEST.new" "$SRC"' ;;
      fetch) DOWNLOAD='fetch -o "$DEST.new" "$SRC"' ;; # FreeBSD
      ftp)   DOWNLOAD='ftp -o "$DEST.new" "$SRC"' ;; # OpenBSD
    esac
    break
  fi
done
if [ -z "$DOWNLOAD" ]; then
  echo "$0: found none of: $os_dltools" >&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 '<title>.*Error has Occurred' "$DEST.new" >/dev/null; then
    errmsg="download from $location failed (SF code browser 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
rm -f "$DEST.lastcheck"
if [ -f "$DEST" ]; then
  if cmp "$DEST" "$DEST.new" >/dev/null 2>/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"


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>