# $Id: Portfile 97894 2012-09-19 02:07:44Z rmstonecipher@macports.org $

PortSystem 1.0

name            tomcat5
version         5.5.25
revision        3
license         Apache-2

categories      java www
maintainers     nomaintainer
platforms       darwin

description     Tomcat 5 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-5/v${version}/src/
checksums       md5 2d6cc9a16aceeef4ec3610cd782d5c9d \
                sha1 81ef41e25f805831aa63353f52e5b498c74a3414 
                                        
patchfiles      patch-catalina.sh

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:jaxen \
                port:saxpath

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

use_configure   no

build.dir       ${worksrcpath}/build
build.cmd       ant
build.target    download dist compat
build.args      [join "
                        -Dbase.path=${build.dir}/Repository
                        -Dnsis.exe=${build.dir}/.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
                        -Djaxen.jar=${javaLibs}/jaxen.jar
                        -Dsaxpath.jar=${javaLibs}/saxpath.jar
                        -Djasper.home=${worksrcpath}/jasper
                        "]
#                       -Dstruts.lib=${javaLibs}/struts/lib

# Remove compatibility for pre-JRE 1.5 systems
variant nocompat description {do not build compat target} {
    build.target-delete compat
}

pre-build {
    #
    # Tomcat in this version requires ant 1.6.2.
    # If the current version of ant is not at least 1.6.2 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.2"] < 0 } {
            return -code error "This version of tomcat requires ant version 1.6.2 or better. Please install a newer ant before proceeding."
        }
    }

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

destroot {
    set target ${destroot}${prefix}/${home}

    # Ensure we've got needed directories
    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}]
    add_users ${tomcatuser} gid=${gid} realname={Web Server}
        
    # Install Tomcat
    file copy ${destroot.dir}/dist ${target}
        
    # Add compat files as needed
    if { ! [variant_isset nocompat] } {
        system "tar -cf - -C ${destroot.dir}/compat . | tar xpf - -C ${target}"
    }
        
    # Add the jstl libs to tomcat5/shared/lib so that they're available to all
    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
    destroot.keepdirs-append \
        ${target}/logs \
        ${target}/temp \
        ${target}/work \
        ${target}/shared/classes \
        ${target}/common/classes
                
    # Install our scripts
    xinstall -m 755 \
        ${filespath}/tomcatctl \
        ${filespath}/conf_setup.sh \
        ${filespath}/setenv.sh \
        ${target}/bin
    xinstall -m 755 \
        ${filespath}/setenv.local \
        ${target}/conf
        
    # Don't install startup script, as we're now generating a startupitem
    #xinstall -m 755 ${filespath}/tomcat5.sh \
    #        ${destroot}${prefix}/etc/rc.d
    #reinplace s|TOMCATPREFIX=$|TOMCATPREFIX=${prefix}/${home}| ${destroot}${prefix}/etc/rc.d/tomcat5.sh
                
    # 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 ${destroot.dir}/build/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.4, 1.5, 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/tomcat5/logs/${name}.pid"

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