#!/usr/bin/env python3

import sys, requests, glob, time

conf_template = 'https://raw.githubusercontent.com/{}/master/{}'

names = [
  'install.conf.yaml',
  'install.conf.json',
  'install.conf.yml',
  'install.yaml',
  'install.yml',
  'install.json',
  'install.conf',
  '.install.conf.yaml',
  'config.yaml',
  '.install.yaml',
  'dotbot.yml',
  'dotbot.yaml',
  '.install.conf.json',
  'install.conf.ymal',
  'install.conf.yaml',
  '.dotbot.conf.yaml',
]

for line in sys.stdin:
  repo = line.strip()
  config_prefix = 'configs/{}_'.format(repo.replace('/', '-'))
  if glob.glob(config_prefix + '*'):
    sys.stderr.write("already fetched config for {}, skipping\n".format(repo))
    continue
  for name in names:
    conf_url = conf_template.format(repo, name)
    sys.stderr.write("getting: {}\n".format(conf_url))
    r = requests.get(conf_url)
    time.sleep(5)
    sys.stderr.write("status code: {}\n".format(r.status_code))
    if r.status_code == 200:
      f = open(config_prefix + name, 'w+')
      f.write(r.text)
      f.close()
      break
  else:
    sys.stderr.write("failed to find config in {}\n".format(repo))

