#!/bin/bash
# May I suggest Ruby?  It has awesome date & time string processing, and features
# regular expressions a la Perl.
# Python has crap for date & time processing, as far as I remember.

userfile=users.list
groupfile=groups.list
# currtime=`date +%R`
iptables=/sbin/iptables
ipgroup=TESTFORWARD
GREEN=eth0

function runip() {

  if [ `echo $gline | awk '{print $2}'` = "ALL" ] ; then
    echo $iptables -A $ipgroup -p ALL -i $GREEN -s `echo $userline | awk '{print $2}'` -j ACCEPT
  else
    # instead of this... {
    z=1
    portnumb=`echo $gline | awk '{print $2}' | sed /,/s//"\n"/g | wc -l`
    if [ $portnumb -gt 1 ] ; then
      while [ $z -le $portnumb ]
      do
        echo $iptables -A $ipgroup -p ALL -i $GREEN -s `echo $userline | awk '{print $2}'` --dport `echo $gline | awk '{print $2}' | sed /,/s//"\n"/g | head -n $z | tail -n 1` -j ACCEPT
        let z+=1
      done
    else
      echo $iptables -A $ipgroup -p ALL -i $GREEN -s `echo $userline | awk '{print $2}'` --dport `echo $gline | awk '{print $2}'` -j ACCEPT
    fi
    # }

    # Do this: {
    portlist=$(echo $gline | awk '{print $2}' | sed -e 's:,: :g')
    ipaddress=$(echo $userline | awk '{print $2}')
    for port in $portlist; do
        echo $iptables -A $ipgroup -p udp -i $GREEN -s $ipaddress --dport $port -j ACCEPT
        echo $iptables -A $ipgroup -p tcp -i $GREEN -s $ipaddress --dport $port -j ACCEPT
    done
    # }
  fi

#  echo $iptables -A $ipgroup -p ALL -I $GREEN -s `echo $userline | awk '{print $2}'` --dport `echo $gline | awk '{print $2
#  echo bla
}

function hourmin2min() {
    hour="$1"; min="$2";
    expr $min '+' $hour '*' 60
}

# See how many lines are in the userfile
usernumb=`wc -l $userfile | awk '{print $1}'`

# Setup LOOP! :)
x=1

# LOOP TIME - WHILE WE HAVE USER'S
while [ $x -le $usernumb ]
do

  ### LOAD USER DATA ###
  # Set Line number that we want to read
  userline=`head -n $x $userfile | tail -n 1`

  ### LOAD GROUP DATA ###
  # Extract Group Field
  groupname=`echo $userline | awk '{print $3}'`
  # get group info and load in to line
  gline=`grep $groupname $groupfile`
  # Get start time and load to var
  starttime=`echo $gline | awk '{print $3}'`
  # Get end time and load to var
  endtime=`echo $gline | awk '{print $4}'`


  ### Break down time in to hours and minutes ###
  currhour=`date +%R | awk -F: '{print $1}'`
  currmin=`date +%R | awk -F: '{print $2}'`
  starthour=`echo $gline | awk '{print $3}' | awk -F: '{print $1}'`
  startmin=`echo $gline | awk '{print $3}' | awk -F: '{print $2}'`
  endhour=`echo $gline | awk '{print $4}' | awk -F: '{print $1}'`
  endmin=`echo $gline | awk '{print $4}' | awk -F: '{print $2}'`

  ### Start time compare ###
  if [ $starttime = "notime" ]; then
    runip
  else
    if [ $starthour -lt $currhour ]; then
      if [ $endhour -gt $currhour ]; then
        runip
      fi
      if [ $endhour -eq $currhour ]; then
        if [ $endmin -gt $currmin ]; then
          runip
        fi
      fi
    fi
    if [ $starthour -eq $currhour ]; then
      if [ $startmin -lt $currmin ] ; then
        # Wouldn't you need to check end minutes here if endhour -eq currhour?
        runip
      fi
    fi
  fi

  # Better idea: convert everything to minutes
  currmin2=$(hourmin2min $currhour $currmin)
  startmin2=$(hourmin2min $starthour $startmin)
  endmin2=$(hourmin2min $endhour $endmin)

  if [ $startmin2 -gte $currmin2 ]; then
      if [ $endmin2 -lte $currmin2 ]; then
          runip
      fi
  fi

  let x+=1
done


