#!/usr/bin/bash
# set -x  # debug if uncommented
# Purpose:
# download FCC complaint data file and optionally append to ncidd.blacklist
#
# Mike Stember 2016-01-26 initially created, inspired by user request
#              2016-06-08 Updated for new way to retrieve data announced by
#                         FCC on May 23rd, 2016
# Ed Attfield  2017-05-24 accommodate changes in the FCC data by establishing
#                         http://ncid-utils.ca to filter the growing dataset
# Todd Andrews 2017-09-10 Mac OS X compatibility for pkill, sed;
#                         code cleanup; some comments moved to man page;
#                         change wget log output to stdout instead of stderr
# Todd Andrews 2017-11-19 Handle older implementations that had 'FCC Bad list'
#                         (newer implementations use lowercase 'bad');
#                         show name of backed up ncidd.blacklist; 
#                         cosmetic change - new $updatemsg to appear after 
#                         backup done instead of before
#
# Todd Andrews 2018-02-10 On Mac OS X, the default PATH for cron jobs does not
#                         include /usr/local/bin. Added in case Homebrew is used
#                         to install 'wget' and 'awk'.
#
# John Chmielewski 2019-02-10 Changed /usr/local/etc to /etc

VERSION="(NCID) 1.18"

# make sure we can find wget, awk
#PATH=$PATH:/usr/local/bin

# target location for backup copies of ncidd.blacklist
BAKDIR=/var/backups/ncid

# the etc/ncid directory
ConfigDir="/etc/ncid"

# full path to ncidd.blacklist
blacklist="${ConfigDir}/ncidd.blacklist"

# name of FCC source file
FCC_list="fcc.blacklist" 

# full website path where FCC_list is found
URL="http://ncid-utils.ca/${FCC_list}"

# separator in case stdout/stderr are being redirected to log files
equals="==============================================================================="

usage() {
   cat <<EOF

Usage: $prog [options]
       
Options:
       [-h] [-V] [-a|-n|-r]

       -h = show this help

       -V = display version

       -a = download FCC data
            add to ncidd.blacklist
            (this is the default action)
            
       -n = download FCC data
            do not add to ncidd.blacklist
            (downloaded file is ready to use with hangup-fcc)

       -r = same as -n but is a special remove-only option to 
            remove "FCC bad list" lines from ncidd.blacklist
            (use only once to transition from using ncidd.blacklist
            to using hangup-fcc)

EOF

   exit 1
}

prog=`basename $0 .sh`

update_blacklist_after_download="a"

# Options on command line
while getopts :ahnrV opt ; do
    case $opt in
        h) usage;;
        a|n|r) update_blacklist_after_download=$opt;;
        V) echo "$prog $VERSION"; exit 0;;
       \?) echo "Invalid option: -$OPTARG"; usage;;
        :) echo "Option -$OPTARG requires an argument."; usage;;
        *) echo "Invalid option: -$OPTARG"; usage;;
    esac
done

echo
if [ $EUID == 0 ]; then
   echo ${equals}
   echo -n "Running ${prog} at "
   date
else
   echo "${prog} must be run as root, try sudo ${prog}"
   exit 1
fi
echo "Command line: ${prog} $*"

shift $((OPTIND-1)) # skip over command line args (if any)

# check for a current ncidd.blacklist
if [ -f "${blacklist}" ]; then
   echo "Found: ${blacklist}"
else
   echo "Aborting: did not find ${blacklist}"
   exit 1
fi

# Change directory so all the data is placed in the etc/ncid directory.
cd ${ConfigDir}

# download the latest list of phone numbers that have come from FCC complaints: 
# (this list has already been trimmed to the recent complaints and sorted)
wget -nv ${URL} -O /var/tmp/${FCC_list} 2>&1
rc=$?
if [ ${rc} != 0 ]; then
   echo "wget ${URL}: download failed with return code ${rc}"
   rm -f /var/tmp/${FCC_list}
   exit 1
fi

case ${update_blacklist_after_download} in
    a) echo "Successfully downloaded ${FCC_list}."
       updatemsg="Now adding to blacklist."
       ;;
    n) echo "Successfully downloaded ${FCC_list} for hangup-fcc."
       mv /var/tmp/${FCC_list} ${FCC_list}
       exit 0
       ;;
    r) echo "Successfully downloaded ${FCC_list} for hangup-fcc."
       mv /var/tmp/${FCC_list} ${FCC_list}
       updatemsg="Now removing old FCC lines from blacklist."
       ;;
esac

# Overview:
#    1. Backup current blacklist to ${BAKDIR}.
#    2. Remove "FCC bad list" entries from blacklist.
#    3. Read each line of downloaded ${FCC_list} and...
#       a. if it's a comment line, skip it, otherwise...
#       b. add "FCC bad list" to each line
#       c. append line to blacklist.
#    5. Purge old files from ${BAKDIR}.
#    6. Send signal to ncidd to reload new blacklist.

# create the archive directory if it does not exist
if [ \! -d ${BAKDIR} ]; then
    mkdir -p ${BAKDIR}
    echo "Created the archive directory: ${BAKDIR}"
fi

# copy current ncidd.blacklist to BAKDIR
timestamp=`date +"%Y-%m-%d_%H:%M"`
bakfile=${BAKDIR}/ncidd.blacklist.${timestamp}
echo "Backing up ${blacklist}"
echo "        to ${bakfile}"
cp ${blacklist} ${bakfile}

echo "$updatemsg"

# sed can update the blacklist file in place to
# delete blacklist file lines with "FCC bad list"
sed -i.bak '/#= FCC bad list/ d;/#= FCC Bad list/ d' ${blacklist}

if [ ${update_blacklist_after_download} = "a" ]; then
   # get datestamp in human readable format e.g. 2017-06-28
   datestamp=`date +"%Y-%m-%d"`
   echo "New date stamp: ${datestamp}"

   # This processes the FCC list by removing comment lines and 
   # adding "FCC bad list" to each line, appending the results
   # to ncidd.blacklist.
   # Requires NCID version 1.3 or later.
   awk -vblacklist=${blacklist} '{
       if ($1 != "#") {
           print $1 " #= FCC bad list '"${datestamp}"' " >> blacklist
       }
   }' /var/tmp/${FCC_list}

   rm -f /var/tmp/${FCC_list}
fi

# clean up and remove old date stamped backups
rm -f ${blacklist}.bak # created by sed
find ${BAKDIR} -name ncidd.blacklist.fcc-\* -mtime +10 -delete 

# Tell ncidd to reload alias, blacklist and whitelist
pkill -SIGHUP ncidd
echo "NCID server(s) will now use the new FCC data."

exit 0
