#!/bin/sh

#-----------------------------------------------------------
# Program : bulksms                        Project : SMSLink
# Author  : Philippe Andersson
# Date    : 17/01/00
# Version : 1.0
# Notice  : (c) Les Ateliers du Heron, 2000 for Scitex Europe, S.A.
# Comment : Sends bulk sms through the sendsms client program.
# History : * 1.0 (17/01/00) - Initial release.
#-----------------------------------------------------------
# Uncomment for debugging
#set -v -x

# Constants

# Set default parameter value
GSMLIST="./gsmlist.txt"               # destination GSM list
LOGFILE="./bulksms.log"
MSG=""
SERVER=""

#***********************************************************
#                     SOME FUNCTIONS
#***********************************************************
#***********************************************************

#***********************************************************
#         CODE BEGINS - GET COMMAND LINE PARAMETERS
#***********************************************************
# Disable filename generation while parsing parameters
set -f

#---------------------------------------------Get parameters
while getopts :i:o:m:s: argname; do
  case ${argname} in
    i) GSMLIST=${OPTARG}
       ;;
    o) LOGFILE=${OPTARG}
       ;;
    m) MSG=${OPTARG}
       ;;
    s) SERVER=${OPTARG}
       ;;
    :) echo "bulksms: missing required value for -${OPTARG} parameter."
       exit 1
       ;;
    ?) echo "bulksms 1.0 - Sends bulk SMS"
       echo " "
       echo "Usage: bulksms [-i gsmlist] [-o logfile] -m message -s server"
       echo " "
       echo "where: -i = gsm file (opt. - def. ./gsmlist.txt)"
       echo "       -o = log file (opt. - def. ./bulksms.log)"
       echo "       -m = message (req. - no def.)"
       echo "       -s = SMS server (req. - no def.)"
       echo " "
       exit 1
       ;;
  esac
done                                         # while getopts

# Handle additional parameters (unused here)
shift $((${OPTIND} -1))
more_args=${*}

# Re-enable filename generation
set +f

#------------------------------Check for required parameters
# Message.
if [ -z "${MSG}" ]; then
  echo "bulksms: missing required parameter message."
  exit 1
fi

# Server.
if [ -z "${SERVER}" ]; then
  echo "bulksms: missing required parameter server."
  exit 1
fi

#----------------------------------------Validate parameters
# check config. file for existence
if [ ! -r ${GSMLIST} ]; then
  echo "bulksms: the specified file (${GSMLIST}) doesn't exist (or can't be read)."
  exit 1
fi

#***********************************************************
#                  PRE_PROCESS INPUT FILE
#***********************************************************
echo "Processing..."

if [ -x /usr/local/bin/frf ]; then
  # removes all trailing spaces
  /usr/local/bin/frf -i 0 -o 0 -f ${GSMLIST}
  if [ ${?} -ne 0 ]; then
    echo "bulksms: error while prodessing ${GSMLIST} through frf."
  fi
else
  echo "bulksms : can't pre-process input file through frf (not found)."
  echo "          Continue with input file \"as is\"... In God we trust (?)"
fi

#***********************************************************
#                      MAIN READ LOOP
#***********************************************************
if [ -r ${GSMLIST} ]; then
  # Write header line to output file (reset it)
  echo "Bulk SMS sending started $(date)." > ${LOGFILE}

  # Now scan input file and process it...
  cat ${GSMLIST} | while read in_line; do
    # ignores full comment lines
    if [ "$(echo ${in_line} | cut -c 1)" = "#" ]; then
      continue
    fi

    # removes comment at end of line
    in_line=${in_line%#*}

    # in case of EOL comments, should also remove trailing spaces.

    # ignores empty lines
    if [ -z ${in_line} ]; then
      continue
    fi

    # now process it...
    echo -n "Sending to ${in_line}..."
    echo -n "Sending to ${in_line}..." >> ${LOGFILE}
    sendsms -d ${in_line} -m "${MSG}" ${SERVER} >> ${LOGFILE} 2>&1
    retval=${?}
    if [ ${retval} -eq 0 ]; then
      echo " OK"
    else
      echo " FAIL"
    fi
    echo " " >> ${LOGFILE}

  done

  # Now close the log with a display of the current time
  echo "Bulk SMS sending finished $(date)." >> ${LOGFILE}

else
  echo "bulksms: can't read ${GSMLIST} config. file."
  exit 1
fi

