#!/usr/bin/env python
import json, shutil, os.path
from pprint import pprint

# Todas las preferencias
# http://src.chromium.org/svn/trunk/src/chrome/common/pref_names.cc

if not os.path.exists('/etc/chromium'):
  print "Creando directorio de preferencias por defecto"
  os.makedirs('/etc/chromium')

if not os.path.exists('/etc/chromium/master_preferences'):
  print "Creando preferencias por defecto"
  js_prefs = {}
  js_prefs['distribution'] = {}
  js_prefs['extensions'] = {}
  js_prefs['browser'] = {}
  with open ('/etc/chromium/master_preferences', 'wb') as prefs:
    json.dump(js_prefs, prefs, indent=4)

print "Copia de seguridad del las preferencias anteriores"
shutil.copyfile ('/etc/chromium/master_preferences' ,'/etc/chromium/master_preferences.backup')
print "Cargando preferencias actuales de chromium"
with open ('/etc/chromium/master_preferences', 'rb') as prefs:
  js_prefs = json.load(prefs);
  try:
    js_prefs['homepage'] = 'http://www.conectarigualdad.gob.ar'
    js_prefs['homepage_is_newtabpage'] = False
    js_prefs['distribution']['import_bookmarks_from_file'] = '/etc/huayra/huayra_chromium_bookmarks.html'
    js_prefs['distribution'] = {}
    js_prefs['browser']['custom_chrome_frame'] = True  # Window decoration
    js_prefs['extensions']['theme'] = {}
    js_prefs['extensions']['theme']['use_system'] = True  # System theme
  except Exception as e: 
    print "Error en el diccionario de opciones: " + str(e)
print "Actualizando preferencias de chromium"
with open ('/etc/chromium/master_preferences', 'wb') as prefs:
  json.dump(js_prefs, prefs, indent=4)
