#!/bin/sh.before
echo "export CMS_LOCAL_SITE=/etc/cms/SITECONF/T2_UK_London_IC" > /etc/cvmfs/config.d/cms.cern.ch.local
chmod 0755 /etc/cvmfs/config.d/cms.cern.ch.local
echo "export SCRAM_ARCH=slc5_amd64_gcc434" > /etc/profile.d/cms.sh
echo "source /cvmfs/cms.cern.ch/cmsset_default.sh" >> /etc/profile.d/cms.sh
chmod 0755 /etc/profile.d/cms.sh

# Hack for CMSSW bug (?)
ln -s /cvmfs/cms.cern.ch/SITECONF /etc/cvmfs/SITECONF

# Avoid xroot client warnings
touch /etc/profile.d/xrootd-protocol.sh
echo '
export XrdSecPROTOCOL=unix
' > /etc/profile.d/xrootd-protocol.sh


# Manual SITECONF, ideally at some point part of /cvmfs/cms.cern.ch
mkdir -p /etc/cms/SITECONF/T2_UK_London_IC/{JobConfig,PhEDEx}
ln -s T2_UK_London_IC /etc/cms/SITECONF/local
echo '
<storage-mapping>
  <lfn-to-pfn protocol="root" destination-match=".*"
    path-match="(.*)" result="root://eospublic.cern.ch/$1"/>
  <lfn-to-pfn protocol="xrootd" destination-match=".*" chain="root"
    path-match="(.*)" result="$1"/>

  <!-- fallback xrootd rules -->
  <lfn-to-pfn protocol="xrootd-uk2" destination-match=".*"
    path-match="(.*)" result="root://xrootd-uk-cms.gridpp.rl.ac.uk/$1"/>
  <lfn-to-pfn protocol="xrootd-eu" destination-match=".*"
    path-match="(.*)" result="root://xrootd.ba.infn.it/$1"/>
  <!-- combine all fallbacks into 1 rule, cmssw can only have 1 fallback -->
  <!-- server chain stops when one server responds - even if it doesn't have
the file -->
  <lfn-to-pfn protocol="fallbacks" destination-match=".*"
   path-match="(.*)"
result="root://cms-xrd-global.cern.ch,xrootd.ba.infn.it,xrootd.unl.edu/$1?tried=gfe02.grid.hep.ph.ic.ac.uk"/>

  <pfn-to-lfn protocol="direct" destination-match=".*"
    path-match=".*(/store/.*)" result="$1"/>
  <pfn-to-lfn protocol="root" destination-match=".*" chain="direct"
    path-match="(.*)" result="$1"/>
  <pfn-to-lfn protocol="xrootd" destination-match=".*" chain="root"
    path-match="(.*)" result="$1"/>
</storage-mapping>
' > /etc/cms/SITECONF/T2_UK_London_IC/PhEDEx/storage.xml
echo '
<site-local-config>
        <site name="T2_UK_London_IC">
        <event-data>
                <catalog
url="trivialcatalog_file:/etc/cvmfs/SITECONF/local/PhEDEx/storage.xml?protocol=root"/>
                <catalog
url="trivialcatalog_file:/etc/cvmfs/SITECONF/local/PhEDEx/storage.xml?protocol=fallbacks"/>
        </event-data>
                <calib-data>
                        <frontier-connect>
                                <proxy
url="http://squid02.gridpp.rl.ac.uk:3128"/>
                                <proxy
url="http://squid03.gridpp.rl.ac.uk:3128"/>
                                <proxy
url="http://squid04.gridpp.rl.ac.uk:3128"/>
                                <proxy
url="http://squid05.gridpp.rl.ac.uk:3128"/>
                                <server
url="http://cmsfrontier.cern.ch:8000/FrontierInt"/>
                                <server
url="http://cmsfrontier1.cern.ch:8000/FrontierInt"/>
                                <server
url="http://cmsfrontier2.cern.ch:8000/FrontierInt"/>
                               <server
url="http://cmsfrontier3.cern.ch:8000/FrontierInt"/>
                        </frontier-connect>
        </calib-data>
        </site>
</site-local-config>
' > /etc/cms/SITECONF/T2_UK_London_IC/JobConfig/site-local-config.xml

exit 0

[cernvm]
repositories=cms.cern.ch,sft.cern.ch
shell=/bin/bash
config_url=http://cernvm.cern.ch/config
users=cms-opendata:cms-opendata:password
edition=Desktop
screenRes=1280x700
keyboard=us
startXDM=on
auto_login=on
desktop_icons=CMS~OpenData,W0Rlc2t0b3AgRW50cnldClZlcnNpb249MS4wCkVuY29kaW5nPVVURi04ClR5cGU9TGluawpOYW1lPVJlYWQgTWUKQ29tbWVudD0KSWNvbj0vdXNyL3NoYXJlL2ljb25zL2Ntcy5qcGcKVVJMPWh0dHA6Ly9vcGVuZGF0YS5jZXJuLmNoL1ZNL0NNUwpHZW5lcmljTmFtZT0K