# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
# $Id: Portfile 89810 2012-02-11 08:55:39Z and.damore@macports.org $

PortSystem 1.0

name                    tomcat6
version                 6.0.32

categories              java www
maintainers             blair openmaintainer
platforms               darwin

description             Tomcat 6 HTTP Server and Java Servlet Container.
long_description        Tomcat is the servlet container that is used in \
                        the official Reference Implementation for the \
                        Java Servlet and JavaServer Pages technologies.
homepage                http://tomcat.apache.org/

distname                apache-tomcat-${version}-src
master_sites            apache:tomcat/tomcat-6/v${version}/src/

checksums               md5    19a1eaa9c9938b520d3c360d8cf4af22 \
                        sha1   ec94c511e54da2b02dd2ba45b9fd1703d5771a45 \
                        rmd160 e6203645e6570564b25154018436d908c5b1a71a

patchfiles              patch-bin-catalina.sh \
                        patch-build.xml.diff

depends_build           bin:ant:apache-ant
depends_lib             bin:java:kaffe \
                        bin:jsvc:commons-daemon \
                        port:jakarta-taglibs-standard-11 \
                        port:commons-beanutils \
                        port:commons-collections \
                        port:commons-launcher \
                        port:commons-daemon \
                        port:commons-digester \
                        port:commons-el \
                        port:commons-logging \
                        port:commons-modeler \
                        port:commons-httpclient \
                        port:commons-pool \
                        port:commons-dbcp \
                        port:commons-fileupload \
                        port:commons-io \
                        port:jakarta-regexp \
                        port:xercesj \
                        port:jakarta-log4j \
                        port:mx4j \
                        port:junit \
                        port:saxpath

##                      port:jaxen \

set javaLibs            ${prefix}/share/java
set home                share/java/tomcat6
set tomcatuser          www
set tomcatgroup         www

use_configure           no

build.cmd               ant
build.target            download
set antArgs             [join "
                               -Dversion=${version}
                               -Dversion.build=[lindex [split ${version} .] 2]
                               -Dbase.path=${worksrcpath}/Repository
                               -Dnsis.exe=${worksrcpath}/.bogus
                               -Dcommons-beanutils.jar=${javaLibs}/commons-beanutils.jar
                               -Dcommons-collections.jar=${javaLibs}/commons-collections.jar
                               -Dcommons-launcher.jar=${javaLibs}/commons-launcher.jar
                               -Dcommons-daemon.jar=${javaLibs}/commons-daemon.jar
                               -Dcommons-digester.jar=${javaLibs}/commons-digester.jar
                               -Dcommons-el.jar=${javaLibs}/commons-el.jar
                               -Dcommons-logging.jar=${javaLibs}/commons-logging.jar
                               -Dcommons-logging-api.jar=${javaLibs}/commons-logging-api.jar
                               -Dcommons-modeler.jar=${javaLibs}/commons-modeler.jar
                               -Dcommons-httpclient.jar=${javaLibs}/commons-httpclient.jar
                               -Dcommons-pool.jar=${javaLibs}/commons-pool.jar
                               -Dcommons-dbcp.jar=${javaLibs}/commons-dbcp.jar
                               -Dcommons-fileupload.jar=${javaLibs}/commons-fileupload.jar
                               -Dregexp.jar=${javaLibs}/jakarta-regexp.jar
                               -DxercesImpl.jar=${javaLibs}/xercesImpl.jar
                               -Dxml-apis.jar=${javaLibs}/xml-apis.jar
                               -Dlog4j.jar=${javaLibs}/jakarta-log4j.jar
                               -Djmx.lib=${javaLibs}
                               -Djunit.jar=${javaLibs}/junit.jar
                               -Dsaxpath.jar=${javaLibs}/saxpath.jar
                               -Djasper.home=${worksrcpath}/../jasper
                              "]
#                              -Dstruts.lib=${javaLibs}/struts/lib
##                             -Djaxen.jar=${javaLibs}/jaxen.jar

build.args              $antArgs

pre-build {
        #
        # Tomcat in this version requires ant 1.6.5.
        # If the current version of ant is not at least 1.6.5 then
        # tell the user and fail.
        #
        if { [string length [auto_execok ant]] } {
                set verOutput [exec ant -version]
                regexp {version (\d+(\.\d+)*)} $verOutput -> antVersion
                if { [vercmp $antVersion "1.6.5"] < 0 } {
                        return -code error "This version of tomcat requires ant version 1.6.5 or better. Please install a newer ant before proceeding."
                }
        }

        # Make a bogus file to prevent nsis.exe from downloading
        system "touch ${worksrcpath}/.bogus"
}

destroot {
        set target ${destroot}${prefix}/${home}
        xinstall -m 755 -d ${target}

        # Ensure we've got needed directories
        ui_msg "# setting up needed dirs"
        xinstall -m 755 -d \
                ${destroot}${prefix}/share/java \
                ${destroot}${prefix}/share/doc \
                ${destroot}${prefix}/etc/rc.d

        # Create the tomcat user
        addgroup ${tomcatgroup}
        set gid [existsgroup ${tomcatgroup}]
        adduser ${tomcatuser} gid=${gid} realname=Web Server

        # Install Tomcat
        ui_msg "# installing tomcat..."
        system "cd ${worksrcpath} && ant $antArgs deploy"
        ## build space:
        file copy ${worksrcpath}/output/build/bin ${target}
        file copy ${worksrcpath}/output/build/conf ${target}
        file copy ${worksrcpath}/output/build/lib ${target}
        file copy ${worksrcpath}/output/build/temp ${target}
        file copy ${worksrcpath}/output/build/webapps ${target}
        ## and classes:
        file copy ${worksrcpath}/output/classes ${target}/shared/

        # Add the jstl libs to tomcat6/shared/lib so that they're
        # available to all
        ui_msg "# adding the jstl libs "
        xinstall -m 755 -d ${target}/shared/lib
        xinstall -m 644 \
                ${prefix}/share/java/jstl.jar \
                ${prefix}/share/java/standard.jar \
                ${target}/shared/lib

        # Remove unwanted files
        foreach f [glob -directory ${target}/bin *.bat] { file delete $f }

        # Maintain empty directories
        ui_msg "# maint. empty dirs... "
        xinstall -m 755 -d ${target}/work
        xinstall -m 755 -d ${target}/logs
        destroot.keepdirs-append \
                ${target}/logs \
                ${target}/temp \
                ${target}/work \
                ${target}/shared/classes \
                ${target}/common/classes

        # Install our scripts
        ui_msg "# adding our scripts "
        xinstall -m 755 \
                ${filespath}/tomcatctl \
                ${filespath}/conf_setup.sh \
                ${filespath}/setenv.sh \
                ${target}/bin
        ui_msg "# adding our scripts 2"
        xinstall -m 755 \
                ${filespath}/setenv.local \
                ${target}/conf

        # Add a symlink from bin directory to tomcatctl
        system "cd ${destroot}${prefix}/bin && ln -s ../${home}/bin/tomcatctl"

        # Configure the scripts
        reinplace s|PREFIX=$|PREFIX=${prefix}| ${target}/bin/tomcatctl
        reinplace s|TOMCAT_USER=$|TOMCAT_USER=${tomcatuser}| ${target}/bin/tomcatctl

        # Rename certain files as .sample to avoid replacing them on update
        # These will be "repaired" by conf_setup.sh on first run
        foreach f "
                   catalina.policy
                   catalina.properties
                   server.xml
                   tomcat-users.xml
                   web.xml
                   setenv.local
                  " {
                file rename ${target}/conf/${f} ${target}/conf/${f}.sample
        }

        # Fix ownership of some directories tomcat really needs to write to
        system "chown -R ${tomcatuser}:${tomcatgroup} \
                ${target}/conf \
                ${target}/logs \
                ${target}/temp \
                ${target}/webapps \
                ${target}/work"

        # Install the docs
        file copy ${worksrcpath}/webapps/docs \
                ${destroot}${prefix}/share/doc/${name}
}


post-install {
        ui_msg "#"
        ui_msg "# Configuration files in ${prefix}/${home}/conf have been"
        ui_msg "# installed with a .sample extension so that they won't be replaced by a"
        ui_msg "# port update. Each will be copied to its proper location during the"
        ui_msg "# first startup of tomcat by catalina.sh or tomcatctl if you don't"
        ui_msg "# do this yourself first. If this _is_ an update, you might do a diff to"
        ui_msg "# verify that you're not missing anything in the newer .sample versions"
        ui_msg "# of these files."
        ui_msg "#"
        ui_msg "# The script ${prefix}/${home}/bin/tomcatctl has been installed to"
        ui_msg "# facilitate start and stop of tomcat using the jsvc utility, which allows you to"
        ui_msg "# start tomcat as a true daemon process. Once tomcat has acquired its TCP socket,"
        ui_msg "# jsvc will drop its root privileges and run as user ${tomcatuser}."
        ui_msg "#"
        ui_msg "# Under Apple Java, control over the Java version used to launch ${name} is"
        ui_msg "# through the shell variable JAVA_JVM_VERSION. This variable should be set"
        ui_msg "# to a java version such as 1.5, 1.6, or CurrentJDK (for the default version)."
        ui_msg "# You may set JAVA_JVM_VERSION in ${prefix}/${home}/bin/setenv.sh."
        ui_msg "#"
}

startupitem.create      yes
startupitem.executable  "${prefix}/bin/tomcatctl" start
startupitem.pidfile     clean "${prefix}/share/java/tomcat6/logs/${name}.pid"


livecheck.type          regex
livecheck.url           ${homepage}/download-60.cgi
livecheck.regex         "apache-tomcat-(\\d+\\.\\d+(\\.\\d+)?)-src.tar.gz"
