#!/usr/bin/env python3
#
# generate `~/.MacOSX/environment.plist` from symbols in current env

import os

# complete path for the current user's "environment.plist" file
dirName = os.path.join(os.environ['HOME'], '.MacOSX')
fileName = os.path.join(dirName, 'environment.plist')

# make sure directory exists
if not os.path.exists(dirName):
  os.mkdir(dirName)

# XML template for the overall structure of the file "environment.plist"
fileTemplate = """\
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
%s
</dict>
</plist>
"""

# XML template for a single name/value pair
itemTemplate = """\
<key>%s</key>
<string>%s</string>
"""

# build body: text containing the xml for all symbols in the environment
body = ""
for key in os.environ.keys():
  body += itemTemplate % (key, os.environ[key])

# create new environment.plist file for all the environment symbols
file = open(fileName, 'w')
file.write(fileTemplate % body)
file.close()
