#!/bin/bash -ex
#
# Download the free blocklists from iblocklis.com
# requires wget

usage()
{
    MY_ID=`basename "${0}"`;
    echo -e "Usage: \e[0;32m${MY_ID}\e[0m bhi
            -b: srm downloaded lists
            -h: Show this message.
            -i: Omit the descirptions; only the IP ranges will remain: x.x.x.x-y.y.y.y

            Note: ${MY_ID} will by default write everything to /tmp/.
            Example: ${MY_ID} -b \n"
    exit 1;
}

generate_list()
{
  LIST=${1};
  BLOCKLIST_DIR=${2};
  IPS_ONLY=${5};

  BLOCKLIST_HOST="http://list.iblocklist.com";
  PARAMS="fileformat=p2p&archiveformat=gz"
  COUNT=0;

  # Where we store the downloaded files.
  if [[ ! -d /tmp/blocklists ]]; then
    mkdir /tmp/blocklists;
  fi

  > ${LIST};

  for ID in `curl -A 'n0tcur1' -s https://www.iblocklist.com/lists.php \
	     | grep ${BLOCKLIST_HOST} \
	     | awk '{print $8}' \
	     | awk -F \= '{print $2}' \
	     | sed 's/.//;s/.$//' \
	     | awk -F \' '{print $2}'`; do

    ((COUNT++));

    wget -q "${BLOCKLIST_HOST}/?list=${ID}&${PARAMS}" -O ${BLOCKLIST_DIR}/${ID}.gz;
    
    if [[ ${IPS_ONLY} ==  true ]]; then
      zcat ${BLOCKLIST_DIR}/${ID}.gz | \
      grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\-[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' >> ${LIST};
    else
      zcat ${BLOCKLIST_DIR}/${ID}.gz >> ${LIST};
    fi
  done

  cat ${LIST} | uniq > ${LIST}.tmp;
  mv ${LIST}.tmp ${LIST};

  LIST_COUNT=$(wc -l ${LIST} | awk '{print $1}');
  echo "Downloaded ${COUNT} lists" >> ${LOG};
  echo "Generated ${LIST_COUNT} line blocklist" >> ${LOG};

  echo 0;
}

cleanup()
{
  BLOCKLIST_DIR=${1};

  if [[ -d ${BLOCKLIST_DIR} ]] ; then
    $RM_CMD -r ${BLOCKLIST_DIR};
  fi

  echo 0;
}

BLOCKLIST_DIR=/tmp/blocklists;
BURNORDER=false;
IPS_ONLY=false;
LIST=/tmp/blocklist.list
LOG=/tmp/blocklist.log

#main
while getopts "bhi" OPT; do
  case ${OPT} in
    b)
      BURNORDER=true;
      which srm
      if [[ $? == 0 ]]; then
        RM_CMD=$(which srm);
      else
        RM_CMD=$(which rm);
      fi
      ;;
    h)
      usage;
      ;;
    i)
      IPS_ONLY=true;
      ;;
    *)
      usage;
      ;;
    \?)
      echo "Invalid option: -${OPTARG}";
      exit 1;
      ;;
  esac
done


if [[ ! -e ${LOG} ]]; then
  touch ${LOG};
  if [[ $? != 0 ]]; then
    echo "Unable to create log!";
    exit 1;
  fi
fi

if [[ ! -e ${LIST} ]]; then
  touch ${LIST};
  if [[ $? != 0 ]]; then
    echo "Check permissions on the list file: ${LIST}" >> ${LOG};
    exit 1;
  fi
  > ${LIST};
fi

if [[ $(generate_list ${LIST} ${BLOCKLIST_DIR} ${IPS_ONLY}) != 0 ]]; then
  echo "Failed generating list." >> ${LOG};
  exit 1;
fi

if [[ ${BURNORDER} == true ]]; then
  if [[ $(cleanup ${BLOCKLIST_DIR}) != 0 ]]; then
    echo "There may be remnants of the blocklist download..." >> ${LOG};
    exit 1
  fi
fi

exit 0;