--- Portfile.orig	2011-07-31 15:05:16.000000000 +0900
+++ Portfile	2011-09-07 00:29:54.000000000 +0900
@@ -5,48 +5,92 @@
 
 name                apache-solr
 version             3.3.0
-categories          java search
+revision            1
+categories          java textproc
 platforms           darwin
-maintainers         gmail.com:haya10.ito openmaintainer
+maintainers         gmail.com:haya10.ito hum openmaintainer
 license             Apache-2.0
 
-description         The open source enterprise search platform
-
+homepage            http://lucene.apache.org/solr/
+description         An open source enterprise search platform.
 long_description    Solr is the popular, blazing fast open source enterprise \
                     search platform from the Apache Lucene project.
 
-homepage            http://lucene.apache.org/solr/
 master_sites        apache:lucene/solr/${version}/
-
-checksums           sha1    e6017419051e5eda9cb222e3b17006eeed682db9 \
+extract.suffix      .tgz
+checksums           ${distname}${extract.suffix} \
+                    sha1    e6017419051e5eda9cb222e3b17006eeed682db9 \
                     rmd160  ef2c2e58d44a9d8786c2fc5b2f19f59bf9e1f70c
 
-extract.suffix      .tgz
+# set the destination path.
+set solr_basepath   ${prefix}/share/java
+set solr_destpath   ${solr_basepath}/${distname}
+set solr_path       ${solr_destpath}/example
+
+post-patch {
+    # expand relative pathes into abusolute ones.
+    reinplace "s|\"\.\./\.\./|\"${solr_destpath}/|g" \
+        ${worksrcpath}/example/solr/conf/solrconfig.xml
+}
 
 use_configure       no
 supported_archs     noarch
 
 build {}
 
-set java_home       /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home
+destroot {
+    # copy the distribution.
+    xinstall -d         ${destroot}${solr_basepath}
+    copy ${worksrcpath} ${destroot}${solr_basepath}
+    # install the solr script.
+    xinstall -m 755 ${filespath}/solr.in     ${destroot}${prefix}/bin/solr
+    reinplace "s|@solr_path@|${solr_path}|g" ${destroot}${prefix}/bin/solr
+}
 
-pre-configure {
-    if {![file exists ${java_home}]} {
-        ui_error "Java 1.6 is required, but not located at ${java_home}"
-        return -code error "Java 1.6 missing"
+notes "
+To try apache-solr, run 'sudo solr' and open http://localhost:8983/solr/browse.
+To store sample documents, 'cd ${solr_destpath}/example/exampledocs && ./post.sh *.xml'."
+
+# lucene-gosen is a Japanese analyzer.
+set gosen_distfile   lucene-gosen-1.1.1-ipadic.jar
+
+variant ja description {Add Japanese settings and lucene-gosen} {
+    homepage-append      http://code.google.com/p/lucene-gosen/
+
+    # fetch lucene-gosen.
+    distfiles-append     ${gosen_distfile}:gosen
+    master_sites-append  http://lucene-gosen.googlecode.com/files/:gosen
+    checksums-append     ${gosen_distfile} \
+                         sha1    2637bf3c4bdb4cb0efd095f4d423e5efb37e4543 \
+                         rmd160  3feacc9c2ac81e2c15f3b8d6a377a85dd6fb6d01
+    extract.only-delete  ${gosen_distfile}
+
+    # create solr-ja, solr home for Japanase texts.
+    post-extract {
+        copy ${worksrcpath}/example/solr ${worksrcpath}/example/solr-ja
     }
-}
+    
+    patchfiles-append    patch-solr-ja.diff
 
-set target          ${prefix}/share/java/${name}-${version}
+    post-destroot {
+        # install lucene-gosen jar and config files.
+        xinstall -d ${destroot}${solr_destpath}/contrib/lucene-gosen/lib
+        copy ${distpath}/${gosen_distfile} \
+             ${destroot}${solr_destpath}/contrib/lucene-gosen/lib
+        xinstall -m 644 -W ${filespath} \
+            mapping-japanese.txt \
+            stoptags_ja.txt \
+            stopwords_ja.txt \
+            velocity.properties \
+            ${destroot}${solr_destpath}/example/solr-ja/conf
+        # copy a sample Japanese doc for testing.
+        copy ${filespath}/solr-ja.xml \
+             ${destroot}${solr_destpath}/example/exampledocs
+    }
 
-destroot {
-    xinstall -d ${destroot}[file dirname ${target}]
-    copy ${worksrcpath}/example ${destroot}${target}
-    xinstall -m 755 ${filespath}/solr.in ${destroot}${prefix}/bin/solr
-    reinplace "s|@TARGET@|${target}|g" ${destroot}${prefix}/bin/solr
-    reinplace "s|@JAVA_HOME@|${java_home}|g" ${destroot}${prefix}/bin/solr
+    notes-append "Please run 'sudo solr -ja' to treat Japanase texts properly."
 }
 
 livecheck.type      regex
-livecheck.url       http://mirrors.ibiblio.org/pub/mirrors/apache/lucene/solr/
-livecheck.regex     {href="([0-9.]+)"}
+livecheck.url       http://www.apache.org/dist/lucene/solr/
+livecheck.regex     (\[0-9.\]+)\/
