#!/bin/sh

# sms.sh
# 2021-12-27
# by Gernot Walzl

# Sends personalized form text messages by replacing the variable $NAME
# to a list of recipients using termux-sms-send.

TO=${TO:-'recipients.csv'}
MSG=${MSG:-'message.txt'}
DELAY=${DELAY:-'15'}

if [ ! -r "$TO" ]; then
  echo 'Error: List of recipients not readable.'
  exit 1
fi
if [ ! -r "$MSG" ]; then
  echo 'Error: Message not readable.'
  exit 1
fi

NAME='Nick'
sed "s/\$NAME/$NAME/g" "$MSG"
echo ''
while read -r RECIPIENT; do
  FIRST=$(echo "$RECIPIENT" | cut -c1)
  if [ "$FIRST" = "#" ]; then
    continue
  fi
  NAME=$(echo "$RECIPIENT" | cut -d';' -f1)
  NUMBER=$(echo "$RECIPIENT" | cut -d';' -f2)
  TEXT=$(sed "s/\$NAME/$NAME/g" "$MSG")
  echo "to ${NAME} (${NUMBER}) in ${DELAY}s"
  sleep "$DELAY"
  termux-sms-send -n "$NUMBER" "$TEXT"
done < "$TO"