#!/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 = '>> '
print "Welcome to Nsof's HTTPie setup"

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

print "Please enter your Nsof login username (email address) or API key ID:"
username = raw_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
