#!/usr/bin/env bash

ipv4_list_file="files/ban_ipv4.list"

if [[ ! -f "$ipv4_list_file" ]] ; then
   echo ""
   echo "   File '$ipv4_list_file' not found'"
   echo ""
   exit 0
fi

declare -a octets
declare -i index

while IFS='' read -r _line || [[ -n $_line ]] ; do

	is_valid_ipv4=true
	is_valid_mask=true
	ipv4=""
   mask=""
	
   # Ignore comment lines
	#
   [[ $_line =~ ^[[:space:]]{0,}# ]] && continue

	# Ignore blank lines
	#
	[[ $_line =~ ^[[:space:]]*$ ]] && continue

	# Remove leading whitespace characters
	#
	_line="${_line#"${_line%%[![:space:]]*}"}"


	# Catch IPv4 Address
   #
	given_ipv4="$(echo  $_line | cut -d ' ' -f1)"


	# Splitt Ipv4 address from possible given CIDR number
   #
	IFS='/' read -ra _addr <<< "$given_ipv4"
	_ipv4="${_addr[0]}"

	if [[ -n "${_addr[1]}" ]] ; then
		_mask="${_addr[1]}"
      test_netmask=false

      # Is 'mask' a valid CIDR number? If not, test agains a valid netmask
      #
      if $(test -z "${_mask##*[!0-9]*}" > /dev/null 2>&1) ; then

         # Its not a vaild mask number, but naybe a valit netmask.
         # 
         test_netmask=true
      else
         if [[ $_mask -gt 32 ]]; then

            # Its not a vaild cidr number, but naybe a valit netmask.
            # 
            test_netmask=true
         else

            # OK, we have a vaild cidr number between '0' and '32'
            #
            mask=$_mask
         fi
      fi

      # Test if given '_mask' is a valid netmask.
      #
      if $test_netmask ; then
         octets=( ${_mask//\./ } )

         # Complete netmask if necessary
         #
         while [[ ${#octets[@]} -lt 4 ]]; do
            octets+=(0)
         done

         [[ ${#octets[@]} -gt 4 ]] && is_valid_mask=false

         index=0
         for octet in ${octets[@]} ; do
            if [[ ${octet} =~ ^[0-9]{1,3}$ ]] ; then
               if [[ $octet -gt 255 ]] ; then
                  is_valid_mask=false
               fi
               if [[ $index -gt 0 ]] ; then
                  mask="${mask}.${octet}"
               else
                  mask="${octet}"
               fi
               
            else
               is_valid_mask=false
            fi

            ((index++))
         done
      fi

		adjust_mask=false
	else
      mask=32
		adjust_mask=true
	fi

	# Splitt given address into their octets
   #
	octets=( ${_ipv4//\./ } )

	# Complete IPv4 address if necessary
   #
	while [[ ${#octets[@]} -lt 4 ]]; do
		octets+=(0)

		# Only adjust CIDR number if not given
      #
		if $adjust_mask ; then
			mask="$(expr $mask - 8)"
		fi
	done

	# Pre-check if given IPv4 Address seems to be a valid address
   #
	[[ ${#octets[@]} -gt 4 ]] && is_valid_ipv4=false

	# Check if given IPv4 Address is a valid address
   #
	if $is_valid_ipv4 ; then
		index=0
		for octet in ${octets[@]} ; do
			if [[ ${octet} =~ ^[0-9]{1,3}$ ]] ; then
				if [[ $octet -gt 255 ]] ; then
					is_valid_ipv4=false
				fi
				if [[ $index -gt 0 ]] ; then
					ipv4="${ipv4}.${octet}"
				else
					ipv4="${octet}"
				fi
				
			else
				is_valid_ipv4=false
			fi

			((index++))
		done
	fi

	if $is_valid_ipv4 && $is_valid_mask; then
   	echo "   ${ipv4}/${mask}"
	else
		echo "   '$given_ipv4' isn't a valid IPv4 address"
	fi

done < ban_ipv4.list 
