#!/usr/bin/python
import getpass
import json
import sys
import os


is_windows = 'win32' in str(sys.platform).lower()
conf_dir = str(os.getenv(
    'HTTPIE_CONFIG_DIR',
    os.path.expanduser('~/.httpie' if not is_windows else r'%APPDATA%\\httpie')
))
conf_path = os.path.join(conf_dir, "config.json")

prompt = '>> '
user_input = input if sys.version_info > (3, 0) else raw_input
print("Welcome to Nsof's HTTPie setup")

print("Please enter your organization's shortname")
org = user_input(prompt)

print("Please enter your Nsof login username (email address) or API key ID:")
username = user_input(prompt)
if '@' not in username and 'key-' not in username:
    print("httpie-nsof error: invalid username format or "
          "invalid API key ID format")
    exit(1)

print("Please enter your Nsof login password or API key secret:")
password = getpass.getpass(prompt)

default_options = ["--auth=%s/%s:%s" % (org, username, password)]
conf = {"default_options": default_options}
if not os.path.exists(conf_dir):
    os.makedirs(conf_dir)
with open(conf_path, 'w') as f:
    json.dump(conf, f, indent=4)
    f.write('\n')

print("Updated %s successfully" % conf_path)
