#!/usr/bin/env python

import os

print "Making script relocatable"
for F in os.listdir('.'):
    if F == 'sage-sage':
        continue
    if not os.path.isfile(F):
        continue
    try:
        X = open(F,'r')
    except IOError:
        continue
    L = X.readline()
    if L.find("python") != -1 and L.find("#!") != -1:
        Y = X.read()
        X.close()
        #print "Making interpreter for script %s relative"%F
        try:
            O = open(F,'w')
        except IOError:
            os.system('chmod u+w "%s"'%F)
            O = open(F,'w')
        O.write("#!/usr/bin/env python\n"+Y)
        O.close()

    
        
