#!/usr/bin/env python

"""
Script which updates README.md with a list of all the available packs.
"""

import os
import copy
import glob
import json
import argparse

import yaml

CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
PACKS_DIR = os.path.join(CURRENT_DIR, '../packs')
README_PATH = os.path.join(CURRENT_DIR, '../README.md')

PARSER_FUNCS = {
    '.json': json.loads,
    '.yml': yaml.safe_load,
    '.yaml': yaml.safe_load
}

BASE_REPO_URL = 'https://github.com/StackStorm/st2contrib'
BASE_PACKS_URL = 'https://github.com/StackStorm/st2contrib/tree/master/packs'
PACK_ICON_URL = 'https://raw.githubusercontent.com/StackStorm/st2contrib/master/packs/%(name)s/icon.png'
NO_PACK_ICON_URL = 'https://raw.githubusercontent.com/StackStorm/st2contrib/master/packs/st2/icon.png'


def get_pack_list():
    packs = os.listdir(PACKS_DIR)
    packs = sorted(packs)
    return packs


def get_pack_metadata(pack):
    metadata_path = os.path.join(PACKS_DIR, pack, 'pack.yaml')
    with open(metadata_path, 'r') as fp:
        content = fp.read()

    metadata = yaml.safe_load(content)

    icon_path = os.path.join(PACKS_DIR, pack, 'icon.png')
    if os.path.exists(icon_path):