#!/bin/bash
#
# Written by Xueshan Feng <sfeng@stanford.edu>
# Copyright 2012 Board of Trustees, Leland Stanford Jr. University
#

# Send request to dell server to retrieve warranty
get_dell_warranty () {
    result=`curl http://xserv.dell.com/services/assetservice.asmx \
	-s -S --data-binary @- --header "Expect:" \
	--header "Soapaction: \"http://support.dell.com/WebServices/GetAssetInformation\"" \
	--header "Content-Type: text/xml; charset=utf-8" \
	<< EOF
<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
  <env:Body>
    <n1:GetAssetInformation xmlns:n1="http://support.dell.com/WebServices/">
      <n1:guid>11111111-1111-1111-1111-111111111111</n1:guid>
      <n1:applicationName>get-dell-warranty.sh</n1:applicationName>
      <n1:serviceTags>${servicetag}</n1:serviceTags>
    </n1:GetAssetInformation>
  </env:Body>
</env:Envelope>
EOF`
}

# Parse soap response
read_dom () {
    local IFS=\>
    read -d \< ENTITY CONTENT
}

######
# Main
######

if [ x`whoami` != "xroot" ]; then
  echo "$0 must be run as user root."
  exit 1
fi

# Default values
host=$(hostname --short)
if [ "$1" != "" ]; then
    servicetag=$1
else
    servicetag=$(dmidecode -s system-serial-number)
fi
manufacturer=$(dmidecode -s system-manufacturer)

echo ""

if [[ "$manufacturer" == *VMware* ]]; then
    echo "Hostname: $host"
    echo "SystemType: VMWare"
    exit 0
fi

if [ ! -x '/usr/bin/curl' ]; then
    echo "curl command is required."
    exit 0
fi

get_dell_warranty

echo "Hostname: $host"
if [ -z "$result" ]; then
  echo "Cannot get warranty information."
else
  echo $result | \
  while read_dom; do
      case $ENTITY in
        ServiceTag|SystemType|SystemModel|ServiceLevelDescription)
          echo "$ENTITY: $CONTENT";;
        SystemShipDate|StartDate|EndDate)
          content=`echo $CONTENT | sed -e 's/T0.*$//'`
          echo "$ENTITY: $content"
          ;;
        DaysLeft)
          echo "$ENTITY: $CONTENT"
          exit 0
          ;;
      esac
  done
fi

exit 0

# Documentation.  Use a hack to hide this from the shell.  Because of the
# above exit line, this should never be executed.
DOCS=<<__END_OF_DOCS__

=for stopwords
Xueshan Feng dmidecode

=head1 NAME

get-dell-warranty - Retrieve Dell service warranty information

=head1 SYNOPSIS

B<get-dell-warranty> I<service-tag>

=head1 DESCRIPTION

Retrieve Dell service warranty information.  If no service tag is given,
use B<dmidecode> to find the host's service tag.  It requires the curl
package and must be run as the root user.

=head1 EXAMPLE

    mx2:~> get-dell-warranty
    Hostname: mx2
    ServiceTag: 9XFHWR1
    SystemType: PowerEdge
    SystemModel: R610
    SystemShipDate: 2011-12-09
    ServiceLevelDescription: P, Gold or ProMCritical
    StartDate: 2011-12-09
    EndDate: 2016-12-09
    DaysLeft: 1565

=head1 AUTHOR

Xueshan Feng <sfeng@stanford.edu>

=cut

__END_OF_DOCS__
