#!./python
import sys
if '.' not in sys.path:
        sys.path.insert (0, '.')

MGI_LIBS = '/usr/local/mgi/live/lib/python'
if MGI_LIBS not in sys.path:
        sys.path.insert (0, MGI_LIBS)

import Configuration
config = Configuration.get_Configuration ('Configuration', 1)

import homelib
import webshare_lib

import os
import pg_db
db = pg_db              # setup alias to minimize code changes

def listToFile (str_list, filename):
    print("writing %s" % filename)
    fp = open (filename, 'w')
    for item in str_list:
        fp.write ('%s\n' % item)
    fp.close()
    return

def captchaInclude ():
    form = """<input type="hidden" name="%s" id="%s" value="default">
        <input type="text" name="%s" id="%s" value="default">""" % \
        (config['CAPTCHA_ELEMENT'], config['CAPTCHA_ELEMENT'], \
        config['CAPTCHA_HIDE'], config['CAPTCHA_HIDE'])
    js = """<script type="text/javascript" language="JavaScript"> 
            list_row = document.getElementById("%s");
            list_row.parentNode.removeChild(list_row);
        </script>""" % config['CAPTCHA_HIDE']

        
    writeStringAsIncludeFile (form, '../www/include/captchaform.html')
    writeStringAsIncludeFile (js, '../www/include/captchajs.html')

def writeStringAsIncludeFile (s, filename):
    """
    Write an include file that can be use to 
      include the given text "s" into an shtml page
    """
    fp = open (filename, 'w')
    fp.write ('%s' % s)
    fp.close()


INCLUDEDIR = os.path.join(config['MGIHOME_PATH'], 'www/include/')

def createIncludeLink (includeName, fileLoc ) :
# 
# INPUTS: includeName - name of the soft link to be created
#         fileLoc - full path to source file
    filePath = os.path.join(INCLUDEDIR, includeName)
    if os.path.exists(filePath):
        os.system("rm " + filePath)
    os.system("ln -s " + fileLoc + " " + filePath)

def main ():
    captchaInclude()
    writeStringAsIncludeFile (config['WI_URL'], '../www/include/wi_url.html')
    writeStringAsIncludeFile (config['FTP_URL'], '../www/include/ftp_url.html')
    writeStringAsIncludeFile (config['MTB_URL'], '../www/include/mtb_url.html')
    writeStringAsIncludeFile (config['MTB_PATHOLOGY_URL'], '../www/include/mtb_pathology_url.html')
    writeStringAsIncludeFile (config['MPD_URL'], '../www/include/mpd_url.html')
    writeStringAsIncludeFile (config['MGIHOME_URL'], '../www/include/mgihome_url.html')
    writeStringAsIncludeFile (config['GB_URL'], '../www/include/gb_url.html')
    writeStringAsIncludeFile (config['GBROWSE_URL'], '../www/include/gbrowse_url.html')
    writeStringAsIncludeFile (config['JBROWSE_URL'], '../www/include/jbrowse_url.html')
    writeStringAsIncludeFile (config['GO_GRAPH_URL'], '../www/include/gograph_url.html')
    writeStringAsIncludeFile (config['GO_TOOLS_URL'], '../www/include/gotools_url.html')
    writeStringAsIncludeFile (config['NCBI_BLAST_URL'], '../www/include/ncbi_blast_url.html')
    writeStringAsIncludeFile (config['LOCUSLINK_URL'], \
        '../www/include/locuslink_url.html')
    writeStringAsIncludeFile (config['RGD_URL'], \
        '../www/include/rgd_url.html')
    writeStringAsIncludeFile (config['ENTREZ_URL'], \
        '../www/include/entrez_url.html')
    writeStringAsIncludeFile (config['GLOSSARY_URL'], \
        '../www/include/glossary_url.html')
    writeStringAsIncludeFile (config['USERHELP_URL'], \
        '../www/include/userhelp_url.html')
    writeStringAsIncludeFile (config['SCHEMA'],\
        '../www/include/schema_url.html')
    writeStringAsIncludeFile (config['NAME'],\
        '../www/include/mgiversion.html')
    writeStringAsIncludeFile (config['FEWI_URL'], \
        '../www/include/fewi_url.html')
    writeStringAsIncludeFile (config['FEWI_URL'], \
        '../www/include/fewi_url.html')
    writeStringAsIncludeFile (config['WKSILVERS_URL'], \
            '../www/include/wksilvers_url.html')        
    writeStringAsIncludeFile (config['COOKBOOK_URL'], \
            '../www/include/cookbook_url.html')        
    writeStringAsIncludeFile (config['MORSEBOOK_URL'], \
            '../www/include/morsebook_url.html') 
    writeStringAsIncludeFile (config['SILVER_URL'], \
            '../www/include/silverbook_url.html') 
    writeStringAsIncludeFile (config['FRITHBOOK_URL'], \
            '../www/include/frithbook_url.html')    
    writeStringAsIncludeFile (config['GREENBOOK_URL'], \
            '../www/include/greenbook_url.html')    
    writeStringAsIncludeFile (config['THEILERBOOK_URL'], \
            '../www/include/theilerbook_url.html')    
    writeStringAsIncludeFile (config['MGIWS_URL'], \
            '../www/include/mgiws_url.html')    
    writeStringAsIncludeFile (config['WEBSHARE_URL'], \
            '../www/include/webshare_url.html')      
    writeStringAsIncludeFile (webshare_lib.webshareLookup('mgi_logo'), \
        '../www/include/mgi_logo.html')
    writeStringAsIncludeFile (webshare_lib.webshareLookup('help'), \
        '../www/include/help_large.html')
    writeStringAsIncludeFile (webshare_lib.webshareLookup('jax_logo'), \
        '../www/include/jax_logo.html')
    writeStringAsIncludeFile (webshare_lib.webshareLookup('mgi_logo_small'), \
        '../www/include/mgi_logo_small.html')
    writeStringAsIncludeFile (webshare_lib.webshareLookup('new'), \
        '../www/include/new.html')
    writeStringAsIncludeFile (config['WEBSHARE_URL'] + 'css/' + config['MGIHOME_CSS_FILE'], \
        '../www/include/stylesheet.html')
    writeStringAsIncludeFile (config['WEBSHARE_URL'] + 'js/' + config['MGIHOME_JS_FILE'], \
        '../www/include/javascript.html')

    # new additions for KOMP

    writeStringAsIncludeFile (config['GENBANK_HOME'],
        '../www/include/genbank_home_url.html')
    writeStringAsIncludeFile (config['ENSEMBL_HOME'],
        '../www/include/ensembl_home_url.html')
    writeStringAsIncludeFile (config['ENTREZGENE_HOME'],
        '../www/include/entrezgene_home_url.html')
    writeStringAsIncludeFile (config['KOMP_HOME'],
        '../www/include/komp_home_url.html')
    writeStringAsIncludeFile (config['KOMP_NIH'],
        '../www/include/komp_nih_url.html')
    writeStringAsIncludeFile (config['IMSRURL'],
        '../www/include/imsr_home_url.html')

    # new include file for the faq's
    writeStringAsIncludeFile (config['FAQ_URL'],
        '../www/include/faq_url.html')

    # new include files for apache aliased menu and homepages
    writeStringAsIncludeFile (config['HOMEPAGES_URL'],
        '../www/include/homepages_url.html')
    writeStringAsIncludeFile (config['MENUS_URL'],
        '../www/include/menus_url.html')

    #####----- Template Include Files --------#####
    createIncludeLink('templateHead.html', config['MGICONFIG_PATH'] + 'web/templateHead.html')
    createIncludeLink('templateHeadNoReset.html', config['MGICONFIG_PATH'] + 'web/templateHeadNoReset.html')
    createIncludeLink('templateBodyStart.html', config['MGICONFIG_PATH'] + 'web/templateBodyStart.html')
    createIncludeLink('templateHdpBodyStart.html', config['MGICONFIG_PATH'] + 'web/templateHdpBodyStart.html')
    createIncludeLink('templateBodyStop.html', config['MGICONFIG_PATH'] + 'web/templateBodyStop.html')

    createIncludeLink('templateBlankBodyStart.html', config['MGICONFIG_PATH'] + 'web/templateBlankBodyStart.html')
    createIncludeLink('templateBlankBodyStop.html', config['MGICONFIG_PATH'] + 'web/templateBlankBodyStop.html')

    createIncludeLink('templateHomePageBodyStart.html', config['MGICONFIG_PATH'] + 'web/templateHomePageBodyStart.html')
    createIncludeLink('templateHomePageBodyStop.html', config['MGICONFIG_PATH'] + 'web/templateHomePageBodyStop.html')


    return

if __name__ == '__main__':
    main()

