#! /usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import argparse
from datetime import date, timedelta
import calendar

def print_echo_date(echo_date, flag_only, format):
    if flag_only.lower() == 's':
        if echo_date.day == 1:
            print(echo_date.strftime(format))
    elif flag_only.lower() == 'e':
        if calendar.monthrange(echo_date.year,
                            echo_date.month)[1] == echo_date.day:
            print(echo_date.strftime(format))
    elif flag_only.lower() in ('se', 'es'):
        if echo_date.day == 1:
            print(echo_date.strftime(format))
        if calendar.monthrange(echo_date.year,
                            echo_date.month)[1] == echo_date.day:
            print(echo_date.strftime(format))
    else:
        print(echo_date.strftime(format))
    return None

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-s", "--start", help="start date(format: yyyymmdd)")
    parser.add_argument("-e", "--end", help="end date(format: yyyymmdd)")
    parser.add_argument("-p",
                        "--periods",
                        help="periods of periods to generate(default: 30)",
                        default='30')
    parser.add_argument("-f",
                        "--format",
                        help="echo date format",
                        default='%Y%m%d')
    parser.add_argument(
        "-o",
        "--only",
        help="echo only month start or month end or all date(s or e or se or all)",
        default='all')
    parser.add_argument("-r",
                        "--reverse",
                        help="reverse echo result(Y/N)",
                        default='N')

    args = parser.parse_args()
    parser = argparse.ArgumentParser()
    start_date = getattr(args, 'start')
    end_date = getattr(args, 'end')
    flag_reverse = getattr(args, 'reverse')
    flag_only = getattr(args, 'only')
    try:
        periods = int(getattr(args, 'periods'))
    except:
        print('Error on periods must be integer')
        exit(1)

    format = getattr(args, 'format')

    if not start_date:
        start_date = date.today().strftime('%Y%m%d')

    try:
        start_date = date(int(start_date[:4]), int(start_date[4:6]),
                        int(start_date[6:]))
    except:
        print('Error start_date format')
        exit(1)

    try:
        start_date.strftime(format)
    except:
        print('Error echo format: {format}'.format(format=format))
        exit(1)

    if flag_only.lower() not in ('s', 'e', 'se', 'es', 'all'):
        print('Error only parameters must be "s" or "se" or "all"')
        exit(1)

    if flag_reverse.upper() not in ('Y', 'N'):
        print('Error reverse parameters must be "Y" or "N"')
        exit(1)

    flag_reverse = flag_reverse.upper()

    if end_date:
        try:
            end_date = date(int(end_date[:4]), int(end_date[4:6]),
                            int(end_date[6:]))
        except:
            print('Error end_date format')
            exit(1)
        if flag_reverse == 'Y':
            for x in reversed(range((end_date - start_date).days + 1)):
                echo_date = (start_date + timedelta(days=x))
                print_echo_date(echo_date, flag_only, format)
        else:
            for x in range((end_date - start_date).days + 1):
                echo_date = (start_date + timedelta(days=x))
                print_echo_date(echo_date, flag_only, format)
        exit()

    if flag_reverse == 'Y':
        for x in reversed(range(periods)):
            echo_date = (start_date + timedelta(days=x))
            print_echo_date(echo_date, flag_only, format)
    else:
        for x in range(periods):
            echo_date = (start_date + timedelta(days=x))
            print_echo_date(echo_date, flag_only, format)

if __name__ == '__main__':
    main()