#!/usr/bin/python
#  This script will build platform-specific dotfiles and create the appropriate symlinks in ~

import platform
import os
import io
import sys
sysName = ''
homeDir = ''
destDir = ''
def init():
  global homeDir
  global destDir

  homeDir = os.path.expanduser('~') + '/'
  destDir = os.path.dirname(os.path.abspath(__file__)) + '/'
  os.chdir(destDir)

def identifySystem():
  global sysName
  sysName = platform.system()
  if sysName != 'Linux' and sysName != 'Darwin':
    print "System not supported!"
    exit(1)
  else:
    print "System identified as " + sysName

def cleanUp():
  print "Cleaning up files in " + destDir + " ..."
  for file in ['bashrc','.bashrc']:
    if os.path.isfile(file):
      print "\tRemoving " + file
      os.remove(file)

def handleBashrcFileWrite(contents, isPrivateFile):
  with open('.bashrc','a') as bashrc:
    bashrc.write(contents.getvalue())
  if not isPrivateFile:
    with open('bashrc','a') as bashrc:
      bashrc.write(contents.getvalue())

def addBashrcFileHeader():
  print "Writing bashrc file header..."
  with io.StringIO() as bashrc:
    bashrc.write(unicode("#!/bin/bash\n"))
    if sysName == 'Linux':
      bashrc.write(unicode("# ~/.bashrc: executed by bash(1) for non-login shells.\n"))
    elif sysName == 'Darwin':
      bashrc.write(unicode("# ~/.bash_profile: executed by bash(1) for lon-login shells.\n"))
    bashrc.write(unicode("# This file was generated by a script. Do not edit manually!\n"))
    handleBashrcFileWrite(bashrc, False)

def addInputFileContents(inputFile, allowComments, isPrivateFile):
  print "\t" + inputFile.name
  with io.StringIO() as bashrc:
    for line in inputFile:
      if line.startswith('#'):
        if allowComments:
          bashrc.write(line)
      else:
        bashrc.write(unicode(line))
    handleBashrcFileWrite(bashrc, isPrivateFile)

def createSymlink(targetName, linkName)	:
  target = destDir + targetName
  link = homeDir + linkName
  print "Symlink " + link + " -> " + target
  if os.path.islink(link):
    print "\tLink exists. Checking..."
    try:
      os.stat(link)
    except OSError, e:
      if e.errno == 2:
        print "\tLink is broken. Removing..."
        os.remove(link)
      else:
        raise
    else:
      print "\tLink is valid."
      return
  else:
    print "\tSymlink does not exist. Creating..."
  os.symlink(target, link)
  print "\tLink created."

def install():
  print "Compiling sections..."
  with open('bash_common','r') as bashCommon:
      addInputFileContents(bashCommon,False,False)
  if os.path.isfile('bash_private'):
    with open('bash_private','r') as bashPrivate:
      addInputFileContents(bashPrivate,False,True)
  if sysName == 'Linux':
    with open('bash_linux','r') as bashLinux:
      addInputFileContents(bashLinux,True,False)
      createSymlink('.bashrc','.bashrc')
  elif sysName == 'Darwin':
    with open('bash_mac','r') as bashMac:
      addInputFileContents(bashMac,True,False)
      createSymlink('.bashrc','.bash_profile')

  createSymlink('vimrc','.vimrc')

  print "Done."

if __name__ == '__main__':
  init()
  identifySystem()
  cleanUp()
  addBashrcFileHeader()
  install()

@@ -192,16 +192,60 @@
 ir = ''%0A
+bashOutputFile = ''%0AbashOutputDotFile = ''%0A%0A
 def init
@@ -446,16 +446,68 @@
 sysName%0A
+  global bashOutputFile%0A  global bashOutputDotFile%0A%0A
   sysNam
@@ -674,16 +674,158 @@
  sysName
+%0A  if sysName == 'Linux':%0A    bashOutputFile = 'bashrc'%0A  else:%0A    bashOutputFile = 'bash_profile'%0A  bashOutputDotFile = '.' + bashOutputFile
 %0A%0Adef cl
@@ -854,16 +854,23 @@
 ning up 
+output 
 files in
@@ -910,26 +910,40 @@
 in %5B
-'
 bash
-rc','.bashrc'
+OutputFile,bashOutputDotFile
 %5D:%0A 
@@ -1090,25 +1090,33 @@
 th open(
-'.
 bash
-rc'
+OutputDotFile
 ,'a') as
@@ -1200,24 +1200,30 @@
 th open(
-'
 bash
-rc'
+OutputFile
 ,'a') as
@@ -1316,22 +1316,38 @@
 Writing 
-bashrc
+%22 + bashOutputFile + %22
  file he
@@ -1870,16 +1870,51 @@
 eFile):%0A
+  if hasattr(inputFile, 'name'):%0A  
   print 
@@ -3133,49 +3133,8 @@
 se)%0A
-      createSymlink('.bashrc','.bashrc')%0A
   el
@@ -3246,20 +3246,17 @@
 ,False)%0A
-    
+%0A
   create
@@ -3267,35 +3267,45 @@
 ink(
-'.
 bash
-rc','.bash_profile')%0A
+OutputDotFile, bashOutputDotFile)
 %0A  c

use variables in favor of hard-coded output file names.