# vim: set filetype=python :

import os

Import('env')
RunInstall = env['_RunInstall']
RunUninstall = env['_RunUninstall']

install_subdir = 'share/galsim'

files = [ 'vega.txt', 'acs_I_unrot_sci_20_cf.fits' ]

if 'install' in COMMAND_LINE_TARGETS:
    install_dir = env['FINAL_PREFIX']
    share_dir = os.path.join(install_dir,install_subdir)
else:
    install_dir = Dir('#.').abspath
    share_dir = Dir('#share').abspath

# Write file share_dir.py to have the correct 
meta_data_file = os.path.join('..','galsim','meta_data.py')
try:
    f = open(meta_data_file,'w')
except IOError:
    # Probably the user ran sudo scons install without first running plain old scons
    # (without sudo), so the meta_data.py file is owned by root now.
    # However, it should still be removable, since the directory should be owned
    # by the user.  So remove it and then retry opening it.
    os.remove(meta_data_file)
    f = open(meta_data_file,'w')

f.write('# This file is automatically generated by SCons when building GalSim.\n')
f.write('# Do not edit.  Any edits will be lost the next time SCons is run.\n')
f.write('\n')
f.write('install_dir = "%s"\n'%install_dir)
f.write('share_dir = "%s"\n'%share_dir)
f.close()

if 'install' in COMMAND_LINE_TARGETS:
    RunInstall(env, files, install_subdir)

if 'uninstall' in COMMAND_LINE_TARGETS:
    RunUninstall(env, files, install_subdir)


