#!/bin/bash

# This script automates the download and verification of the DeviceAtlas data 
# file. It downloads the data file and compares to a md5 checksum file.
#
# The DeviceAtlas data file is updated on a daily basis.


# Your DeviceAtlas licence key
DA_LICENCE_KEY="YOUR_DEVICEATLAS_LICENCE_KEY"

# No trailing slash on this directory
DA_COPY_TO="/LOCATION/TO/PLACE/EXTRACTED/JSON/FILE"

# The final filename to save the downloaded json to - this is the file your 
# API loads from the directory configured above
DA_LIVE="EXTRACTED_JSON_FILE_NAME.json"

#choose zip or gz
DA_ARCHIVE_FORMAT=gz



#-----------------------------------------#
#            Sample Config                #
#-----------------------------------------#

#DA_LICENCE_KEY="abcdefghijklmnopqrstuvwxyz0123456789"
#DA_COPY_TO="/var/www/deviceatlas/json"
#DA_LIVE="DeviceAtlas.json"
#DA_ARCHIVE_FORMAT=gz



#-----------------------------------------#
#       Automated processes are           #
#          below this notice              #
#-----------------------------------------#

function error_exit {
  echo "$1" 1>&2
  exit 1
}

TMP_DIR=/tmp
TMP_EXTRACT=${TMP_DIR}/da_download
TODAY=`date '+%Y%m%d'`

LIVE_FILE=${DA_COPY_TO}/${DA_LIVE}

if [ ! -d ${TMP_EXTRACT} ]; then
  mkdir ${TMP_EXTRACT}
  if [ "$?" -ne "0" ]; then error_exit "Could not create temporary directory: ${TMP_EXTRACT}"; fi
fi

TMP_EXTRACTED=${TMP_EXTRACT}/DeviceAtlas.json
DOWNLOAD_TO=${TMP_EXTRACTED}.${DA_ARCHIVE_FORMAT}
DOWNLOAD_TO_MD5=${DOWNLOAD_TO}.md5

GET_URL="http://deviceatlas.com/getJSON.php?licencekey=${DA_LICENCE_KEY}&format=${DA_ARCHIVE_FORMAT}"
echo "Downloading JSON archive to ${DOWNLOAD_TO}"
wget --quiet "${GET_URL}" -O ${DOWNLOAD_TO}
if [ "$?" -ne "0" ]; then error_exit "Download of JSON failed"; fi

echo "Downloading JSON MD5 to ${DOWNLOAD_TO_MD5}"
wget --quiet "${GET_URL}&md5=1" -O ${DOWNLOAD_TO_MD5}
if [ "$?" -ne "0" ]; then error_exit "Download of JSON MD5 failed"; fi

echo "Verifying download..."
md5=`md5sum ${DOWNLOAD_TO} | cut -d " " -f1`
if [ "$?" -ne "0" ]; then error_exit "Unable to MD5Sum ${DOWNLOAD_TO} - are 'md5sum' and 'cut' installed?"; fi
md5_check=`cat ${DOWNLOAD_TO_MD5}`
if [ "$?" -ne "0" ]; then error_exit "Unable to Cat ${DOWNLOAD_TO} - is 'cat' installed?"; fi

if [ "${md5}" != ${md5_check} ]; then
  error_exit "MD5 Verification Failure:\nDownloaded:\t${md5}\nCalculated:\t${md5_check}"
fi

echo "Extracting..."
if [ "${DA_ARCHIVE_FORMAT}" == "zip" ]; then
  unzip -q -o -p ${DOWNLOAD_TO} > ${TMP_EXTRACTED}
  if [ "$?" -ne "0" ]; then error_exit "Unable to unzip ${DOWNLOAD_TO} - is 'unzip' installed?"; fi
else
  gunzip -f -q ${DOWNLOAD_TO}
  if [ "$?" -ne "0" ]; then error_exit "Unable to gunzip ${DOWNLOAD_TO} - is 'gunzip' installed?"; fi
fi

cmp -s ${TMP_EXTRACTED} ${LIVE_FILE} > /dev/null
if [ "$?" -eq 1 ] || [ ! -f ${LIVE_FILE} ]; then
  echo "Copying ${TMP_EXTRACTED} to ${LIVE_FILE}"
  cp --backup=simple -f ${TMP_EXTRACTED} ${LIVE_FILE}
  if [ "$?" -ne "0" ]; then error_exit "Unable to copy from ${TMP_EXTRACTED} to ${LIVE_FILE}"; fi
else
  echo "Identical JSON file, no copy needed ;)"
fi

echo "Cleaning up..."
rm ${TMP_EXTRACT}/*.*
if [ "$?" -ne "0" ]; then error_exit "Unable to delete contents of ${TMP_EXTRACT}"; fi
rmdir ${TMP_EXTRACT}
if [ "$?" -ne "0" ]; then error_exit "Unable to delete directory: ${TMP_EXTRACT}"; fi
echo "DONE"

exit 0

