#!/bin/bash


lastday()
{
  year=10#$1
  month=10#$2

if [[ $month -eq 1 || $month -eq 3 || $month -eq 5 || $month -eq 7 || $month -eq 8 || $month -eq 10 || $month -eq 12 ]]
then 
  edate=31
else
  if [[ $month -ne 2 ]]
  then
    edate=30
  else
    d4=$((year%4))
    if [[ $d4 -ne 0 ]]
    then 
      edate=28
    else
      d100=$((year%100))
      if [[ $d100 -ne 0 ]]
      then
        edate=29
      else
	d400=$((year%400))
	if [[ $d400 -eq 0 ]]
	then 
          edate=29
	else
          edate=28
	fi
      fi
    fi
  fi
fi
echo $edate
}

# Run gendate.sh y0 m0 d0 y1 m1 d1

y0=$1
m0=$2
d0=$3
y1=$4
m1=$5
d1=$6


if [[ $# -eq 0 ]]
then
  echo gendate.sh y0 m0 d0 y1 m1 d1
  exit
fi

for year in `seq -w $y0 $y1`
do
  for month in 01 02 03 04 05 06 07 08 09 10 11 12;
  do
    # Is before first month?
    if [[ $year -eq $y0 ]]
    then
      if [[ 10#$month -lt 10#$m0 ]]
      then
	continue
      fi
    fi

    # Is after last month?
    if [[ 10#$year -eq 10#$y1 ]]
    then
      if [[ 10#$month -gt 10#$m1 ]]
      then 
	continue
      fi
    fi

    # Go over days    
    ldm=`lastday $year $month`
    for day in `seq -w 1 $ldm`
    do
      # Is before first day?
      if [[ $year -eq $y0 ]]
      then
	if [[ 10#$month -lt 10#$m0 ]]
	then
	  continue
	elif [[ 10#$month -eq 10#$m0 ]]
	then
	  if [[ 10#$day -lt 10#$d0 ]]
	  then 
	    continue
	  fi
	fi
      fi

      # Is after last day?
      if [[ $year -eq $y1 ]]
      then
	if [[ 10#$month -gt 10#$m1 ]]
	then
	  continue
	elif [[ 10#$month -eq 10#$m1 ]]
	then
	  if [[ 10#$day -gt 10#$d1 ]]
	  then 
	    continue
	  fi
	fi
      fi

      echo $year $month $day 
    done
  done
done




  

