FROM tomcat:7-jre7

MAINTAINER "Boro Jakimovski <boro.jakimovski@finki.ukim.mk>"

ENV SHIBCDS_VERSION=${SHIBCDS_VERSION:-1.2.1}
ENV JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
ENV TTP_PASSWORD=ttpPASS
RUN apt-get update && apt-get install -y mysql-client
RUN cd /opt/
RUN wget http://shibboleth.net/downloads/centralized-discovery-service/latest/shibboleth-discovery-service-$SHIBCDS_VERSION-bin.zip
RUN unzip shibboleth-discovery-service-$SHIBCDS_VERSION-bin.zip -d /opt/
RUN mv /opt/shibboleth-discovery-service-$SHIBCDS_VERSION/lib/endorsed/* $CATALINA_HOME/lib
RUN cd /opt/
RUN wget https://svn.geant.net/GEANT/TrustBroker/releases/shib-ttp-dame-cds-extension-0.1.2-src.zip
RUN unzip shib-ttp-dame-cds-extension-0.1.2-src.zip -d /opt/
RUN cd /opt/
RUN mv /opt/shib-ttp*/ /opt/ttp-extension
RUN cd /opt/shibboleth-discovery-service-$SHIBCDS_VERSION
RUN cp /opt/ttp-extension/lib/* /opt/shibboleth-discovery-service-$SHIBCDS_VERSION/lib/
RUN mkdir /opt/shibboleth-discovery-service-$SHIBCDS_VERSION/src/main/webapp/ttp/
RUN cp -r /opt/ttp-extension/src/main/webapp/ttp/* /opt/shibboleth-discovery-service-$SHIBCDS_VERSION/src/main/webapp/ttp
RUN cp -r /opt/ttp-extension/src/main/webapp/WEB-INF/* /opt/shibboleth-discovery-service-$SHIBCDS_VERSION/src/main/webapp/WEB-INF
RUN cp /opt/shibboleth-discovery-service-$SHIBCDS_VERSION/src/main/webapp/WEB-INF/web.xml /opt/shibboleth-discovery-service-$SHIBCDS_VERSION/src/main/webapp/WEB-INF/web.xml.backup
RUN mkdir -p /opt/shibboleth-ds/metadata/ttp
RUN mkdir -p /opt/shibboleth-ds/conversionRules
RUN cd /opt/shibboleth-discovery-service-$SHIBCDS_VERSION

RUN sed -i 's/<input message="Where /<!-- <input message="Where /' /opt/shibboleth-discovery-service-$SHIBCDS_VERSION/src/installer/resources/build.xml
RUN sed -i 's/defaultvalue="${ds.home}"\/>/defaultvalue="${ds.home}"\/> --> <var name="ds.home.input" value="\/opt\/shibboleth-ds"\/>/' /opt/shibboleth-discovery-service-$SHIBCDS_VERSION/src/installer/resources/build.xml
RUN perl -0777 -i.original -pe 's/<if>.*?<available.*?if>/<var name="install.config" value="yes"\/>/igs' /opt/shibboleth-discovery-service-$SHIBCDS_VERSION/src/installer/resources/build.xml
RUN sed -i 's/<\/WayfConfig>/<TTPServerMetadataSyncHandler location=".+\/TTP" default="true" \/> <TTPServerConversionRuleSyncHandler location=".+\/TTPCRS" default="true" \/> <\/WayfConfig>/'  /opt/shibboleth-discovery-service-$SHIBCDS_VERSION/src/installer/resources/wayfconfig.xml

ADD web.xml /opt/shibboleth-discovery-service-$SHIBCDS_VERSION/src/main/webapp/WEB-INF/web.xml
ADD ttp.sql /opt/ttp-extension/ttp.sql
ADD ttp-cds-entrypoint.sh /opt/ttp-extension/ttp-cds-entrypoint.sh

RUN chmod +x /opt/ttp-extension/ttp-cds-entrypoint.sh

ENTRYPOINT ["/opt/ttp-extension/ttp-cds-entrypoint.sh"]
