#!/bin/sh
#
#
#    Scalix Public License
#
#
#    The contents of this file are subject to the Scalix Public License
#    Version 1.1 ("License"); you may not use this file except in
#    compliance with the License. You may obtain a copy of the License at
#
# 
#    http://www.scalix.com/community/licensing 
#
# 
#    Software distributed under the License is distributed on an "AS IS"
#    basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
#    the License for the specific language governing rights and limitations
#    under the License.
#
# 
#    The Original Code is Scalix Server. 
#    The Initial Developer of the Original Code is Scalix Corporation. 
#    Copyright (C) 2006 Scalix Corporation. 
#    All Rights Reserved.
#    Additional Copyrights may apply - see the notice in each file
#
#
#
#
#
#
# Copyright (C) 2008 Scalix Corporation. All rights reserved.
#
###############################################################################
# This script is used by the RES to aggregate information about mobile settings
# so that it can easily be fetched through one remote command. This script
# should only be used within the system administration console framework.
###############################################################################

#
# usage help function
#
usage() {
	echo "usage: $0 [OPTION]"
    echo ""
	  echo "    -d device   specify mobile device"
    echo "    -u authid   authorization id for the user"
    echo ""
    echo "    Where device can be replaced with: 'activesync'."
    echo ""
	exit 1
}
#
# Function to check if the mobile device is enabled.
#
checkMobileStatus () {
  if [ "$1" = "activesync" ]
  then
    STATUS=`/opt/scalix/bin/omshowu -n $2 | grep -i ActiveSync | grep -i yes`

	  if [ -z "$STATUS" ]
	  then
		  echo "ACTIVESYNC_ENABLED=FALSE"
	  else
		  echo "ACTIVESYNC_ENABLED=TRUE"
	  fi
  fi
}
#
# Function to check if mobile device/protocol available in the license
#
checkMobileAvailable () {
  if [ "$1" = "activesync" ]
  then
	  OUTPUT=`/opt/scalix/bin/sxlicense | grep ActiveSync`

	  if [ -z "$OUTPUT" ]
	  then
		  echo "ACTIVESYNC_AVAILABLE=FALSE"
	  else
      EXPIRYDATE=${OUTPUT#*;Expiry=}
      EXPIRYDATE=${EXPIRYDATE%%;*}

      EPOCHCURRENT=`date +%s`
      EPOCHFUTURE=`date -d $EXPIRYDATE +%s`

      if [ "$EPOCHCURRENT" -le "$EPOCHFUTURE" ]
      then
		    echo "ACTIVESYNC_AVAILABLE=TRUE"
      else
        echo "ACTIVESYNC_AVAILABLE=FALSE"
      fi
	  fi
  fi
}
#
# parse the arguments
#
OPTERR=0
while getopts d:u: opt
do
	case "$opt" in
		d) MOBILEDEVICE="$OPTARG"
			;;
		u) AUTHID="$OPTARG"
			;;
		[?]) usage
			;;
	esac
done

if [ -z $MOBILEDEVICE ] || [ -z $AUTHID ]
then
  usage
fi

checkMobileAvailable $MOBILEDEVICE
checkMobileStatus $MOBILEDEVICE $AUTHID

exit 0
