#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2016 Clay L. McLeod <clay.l.mcleod@gmail.com>
#
# Distributed under terms of the MIT license.

from __future__ import print_function

import os, sys, json
from celcius.utils import file_utils

dot_celcius_folder = file_utils.get_dot_celcius_path()

if not os.path.isdir(dot_celcius_folder):
    os.makedirs(dot_celcius_folder)

    print('Configuring system')
    retrieve_command = ''

    sys.stdout.write("-- Searching for retrieve command...")
    if file_utils.command_exists('curl'):
        retrieve_command = 'curl'
        print(" found curl!")
    elif file_utils.command_exists('wget'):
        retrieve_command = 'wget'
        print(" found wget!")
    else:
        sys.stderr.write("FAILED\n")
        sys.stderr.flush()
        sys.exit(1)

    configuration = {
        'retrieve_command': retrieve_command
    }

    config_path = os.path.join(dot_celcius_folder, 'config.json')
    with open(config_path, 'w') as f:
        json.dump(configuration, f, sort_keys=True, indent=4, separators=(',', ': '))
else:
    print(dot_celcius_folder+" already exists!")
    print("Run 'rm -rf "+dot_celcius_folder+"' and rerun celcius-init!")
