#!/usr/bin/env python

from rauth.service import OAuth1Service
import requests
import httplib
import httplib2
import hashlib
import urllib
import time
import sys
import os
import json
import ConfigParser
import re
import shutil

from smuploader import SmugMug

def write_config(configfile, params):
    config = ConfigParser.SafeConfigParser()
    config.add_section('SMUGMUG')
    for key, value in params:
        config.set('SMUGMUG', key, value)
    with open(SmugMug.smugmug_config, 'wb') as f:
        config.write(f)


if __name__ == '__main__':
    print("\n\n\n#######################################################")
    print("## Welcome! ")
    print("## We are going to go through some steps to set up this SmugMug photo manager and make it connect to the API.")
    print("## Step 1: What is your SmugMug username?")
    username = raw_input("Username: ")

    print("## Step 2: Go to https://api.smugmug.com/api/developer/apply and apply for an API key.")
    print("## This gives you unique identifiers for connecting to SmugMug.")
    print("## When done, you can find the API keys in your SmugMug profile.")
    print("## Account Settings -> Me -> API Keys")
    print("## Enter them here and they will be saved to the config file (" + SmugMug.smugmug_config + ") for later use.")
    consumer_key = raw_input("Key: ")
    consumer_secret = raw_input("Secret: ")

    write_config(SmugMug.smugmug_config, [("username", username), ("consumer_key", consumer_key), ("consumer_secret", consumer_secret), ("access_token", ''), ("access_token_secret", '')])

    smugmug = SmugMug()
    authorize_url = smugmug.get_authorize_url()
    print("## Step 2: Visit this address in your browser to authenticate your new keys for access your SmugMug account: \n## " + authorize_url)
    print("## After that, enter the 6-digit key that SmugMug provided")
    verifier = raw_input("6-digit key: ")

    access_token, access_token_secret = smugmug.get_access_token(verifier)

    write_config(SmugMug.smugmug_config, [("username", username), ("consumer_key", consumer_key), ("consumer_secret", consumer_secret), ("access_token", access_token), ("access_token_secret", access_token_secret)])

    print("## Great! All done!")
