#!/usr/bin/python
# SMS (using smstools)
# Notification via sms using the sms tools
# Note: You have to add the side user to the sendsms group
# and restart the site

import os, sys

send_path = None
for binary in [ 'sendsms', 'smssend' ]:
    if os.system('which %s >/dev/null' % binary) == 0:
        send_path = binary

if not send_path:
    sys.stderr.write('Error: SMS Tools binaries (sendsms or smssend) not found\n')
    sys.exit(1)

max_len = 160
message = os.environ['NOTIFY_HOSTNAME'] + " "

if os.environ['NOTIFY_WHAT'] == 'SERVICE':
    message += os.environ['NOTIFY_SERVICESTATE'][:2] + " "
    avail_len = max_len - len(message)
    message += os.environ['NOTIFY_SERVICEDESC'][:avail_len] + " "
    avail_len = max_len - len(message)
    message += os.environ['NOTIFY_SERVICEOUTPUT'][:avail_len]

else:
    message += "is " + os.environ['NOTIFY_HOSTSTATE']

empf = os.environ['NOTIFY_CONTACTPAGER']
os.system("%s %s '%s'" % (send_path, empf, message[:160]))
