#!/bin/sh -
#
# build script for 1.3
#
# If it's given a buildname it creates a subdirectory and places a build in it,
# otherwise it just creates the docs and class files.
#

base=$1
version=`echo $base | sed -e "s/\([0-9]\)\([0-9a-z]*\)/\1.\2/"`

WINDOWTITLE="Bouncy Castle Cryptography $version API Specification"
HEADER="<b>Bouncy Castle Cryptography $version</b>"
DOCTITLE="Bouncy Castle $version API Specification"

JDK13PATH=/opt/jdk1.3

if test "$base" != "" -a ! -d lcrypto-jdk13-$base
then
    echo "making lightweight release"

    mkdir lcrypto-jdk13-$base
    tar cf - index.html LICENSE.html CONTRIBUTORS.html releasenotes.html specifications.html src/org/bouncycastle/LICENSE.java src/org/bouncycastle/math src/org/bouncycastle/crypto src/org/bouncycastle/util src/org/bouncycastle/asn1 | (cd lcrypto-jdk13-$base; tar xf -)

    (
        cd lcrypto-jdk13-$base;

        mkdir classes; mkdir docs;
        rm -rf src/org/bouncycastle/jce
        rm -rf src/org/bouncycastle/openpgp

        PATH=$JDK13PATH/bin:$PATH
        export PATH

        echo "compiling"

        (cd src/org/bouncycastle; javac -d ../../../classes -classpath ../../../classes:../../../src *.java */*.java */*/*.java )

        echo "lightweight regression test"
        java -classpath classes org.bouncycastle.crypto.test.RegressionTest

        javadoc -windowtitle "$WINDOWTITLE" -doctitle "$DOCTITLE" \
            -header "$HEADER"\
            -group "Lightweight Crypto Packages" "org.bouncycastle.crypto*" \
            -group "ASN.1 Support Packages" "org.bouncycastle.asn1*" \
            -group "Math Support Packages" "org.bouncycastle.math*" \
            -group "Utility Packages" "org.bouncycastle.util*" \
            -classpath classes \
            -d docs -sourcepath src \
            org.bouncycastle.asn1 \
            org.bouncycastle.asn1.cmp \
            org.bouncycastle.asn1.cms \
            org.bouncycastle.asn1.ess \
            org.bouncycastle.asn1.misc \
            org.bouncycastle.asn1.mozilla \
            org.bouncycastle.asn1.nist \
            org.bouncycastle.asn1.ocsp \
            org.bouncycastle.asn1.oiw \
            org.bouncycastle.asn1.pkcs \
            org.bouncycastle.asn1.sec \
            org.bouncycastle.asn1.smime \
            org.bouncycastle.asn1.teletrust \
            org.bouncycastle.asn1.test \
            org.bouncycastle.asn1.tsp \
            org.bouncycastle.asn1.util \
            org.bouncycastle.asn1.x509 \
            org.bouncycastle.asn1.x9 \
            org.bouncycastle.math.ec \
            org.bouncycastle.crypto \
            org.bouncycastle.crypto.agreement \
            org.bouncycastle.crypto.digests \
            org.bouncycastle.crypto.encodings \
            org.bouncycastle.crypto.engines \
            org.bouncycastle.crypto.generators \
            org.bouncycastle.crypto.io \
            org.bouncycastle.crypto.macs \
            org.bouncycastle.crypto.modes \
            org.bouncycastle.crypto.paddings \
            org.bouncycastle.crypto.params \
            org.bouncycastle.crypto.signers \
            org.bouncycastle.crypto.test \
            org.bouncycastle.crypto.examples \
            org.bouncycastle.util.encoders \
            org.bouncycastle.util.test > /dev/null \

    )
    (2>&1 find lcrypto-jdk13-$base -name CVS -exec rm -rf \{\} \; ) > /dev/null
fi

if test "$base" != "" -a ! -d jce-jdk13-$base
then
    echo "making JCE release"

    mkdir jce-jdk13-$base
    tar cf - index.html LICENSE.html CONTRIBUTORS.html releasenotes.html specifications.html src | (cd jce-jdk13-$base; tar xf -)
    (cd jdk1.4; tar cf - * | (cd ../jce-jdk13-$base/src; tar xf -))
    (cd jdk1.3; tar cf - * | (cd ../jce-jdk13-$base/src; tar xf -))
    (
    cd jce-jdk13-$base; mkdir classes; mkdir docs;

    rm -rf src/org/bouncycastle/mail
    rm -rf src/org/bouncycastle/cms
    rm -rf src/org/bouncycastle/tsp
    rm -rf src/org/bouncycastle/bcpg
    rm -rf src/org/bouncycastle/openpgp
    rm -rf src/org/bouncycastle/voms
    rm -rf src/org/bouncycastle/jce/ECPointUtil.java
    rm -rf src/org/bouncycastle/jce/provider/JCEEC5*.java
    rm -rf src/org/bouncycastle/jce/provider/test/ECDSA5Test.java

    PATH=$JDK13PATH/bin:$PATH
    export PATH

    javadoc -windowtitle "$WINDOWTITLE" -doctitle "$DOCTITLE" \
        -header "$HEADER"\
        -group "Cleanroom JCE" "javax.crypto*" \
        -group "JCE Utility and Extension Packages" "org.bouncycastle.jce*" \
        -group "OCSP and OpenSSL PEM Support Packages" "org.bouncycastle.ocsp*:org.bouncycastle.openssl*" \
        -group "ASN.1 Support Packages" "org.bouncycastle.asn1*" \
        -group "Lightweight Crypto Packages" "org.bouncycastle.crypto*" \
        -group "Utility Packages" "org.bouncycastle.util*:org.bouncycastle.math*" \
        -group "JCE Provider and Test Classes" "org.bouncycastle.jce.provider*" \
        -d docs -sourcepath src \
        javax.crypto  \
        javax.crypto.interfaces \
        javax.crypto.spec \
        org.bouncycastle.jce \
        org.bouncycastle.asn1 \
        org.bouncycastle.asn1.cmp \
        org.bouncycastle.asn1.cms \
        org.bouncycastle.asn1.ess \
        org.bouncycastle.asn1.misc \
        org.bouncycastle.asn1.mozilla \
        org.bouncycastle.asn1.nist \
        org.bouncycastle.asn1.ocsp \
        org.bouncycastle.asn1.oiw \
        org.bouncycastle.asn1.pkcs \
        org.bouncycastle.asn1.sec \
        org.bouncycastle.asn1.smime \
        org.bouncycastle.asn1.teletrust \
        org.bouncycastle.asn1.test \
        org.bouncycastle.asn1.tsp \
        org.bouncycastle.asn1.util \
        org.bouncycastle.asn1.x509 \
        org.bouncycastle.asn1.x9 \
        org.bouncycastle.math.ec \
        org.bouncycastle.crypto \
        org.bouncycastle.crypto.agreement \
        org.bouncycastle.crypto.digests \
        org.bouncycastle.crypto.encodings \
        org.bouncycastle.crypto.engines \
        org.bouncycastle.crypto.generators \
        org.bouncycastle.crypto.io \
        org.bouncycastle.crypto.macs \
        org.bouncycastle.crypto.modes \
        org.bouncycastle.crypto.paddings \
        org.bouncycastle.crypto.params \
        org.bouncycastle.crypto.signers \
        org.bouncycastle.crypto.test \
        org.bouncycastle.crypto.examples \
        org.bouncycastle.jce.cert \
        org.bouncycastle.jce.interfaces \
        org.bouncycastle.jce.spec \
        org.bouncycastle.jce.examples \
        org.bouncycastle.jce.provider \
        org.bouncycastle.jce.provider.test \
        org.bouncycastle.mozilla \
        org.bouncycastle.mozilla.test \
        org.bouncycastle.ocsp \
        org.bouncycastle.ocsp.test \
        org.bouncycastle.openssl \
        org.bouncycastle.openssl.test \
        org.bouncycastle.x509 \
        org.bouncycastle.x509.examples \
        org.bouncycastle.util.encoders \
        org.bouncycastle.util.test > /dev/null \

    echo "compiling"

    (cd src/org/bouncycastle; javac -d ../../../classes -classpath ../../../classes:../../../src *.java */*.java */*/*.java */*/*/*.java)

    (cd src/javax/crypto; javac -d ../../../classes -classpath ../../../classes:../../../src *.java */*.java)

    cp src/org/bouncycastle/openssl/test/*.pem classes/org/bouncycastle/openssl/test

    echo "provider regression test"
    java -classpath classes org.bouncycastle.jce.provider.test.RegressionTest
    java -classpath classes org.bouncycastle.ocsp.test.OCSPTest
    java -classpath classes org.bouncycastle.mozilla.test.SPKACTest
    java -classpath classes org.bouncycastle.asn1.test.RegressionTest
    java -classpath classes org.bouncycastle.openssl.test.ReaderTest
    java -classpath classes org.bouncycastle.util.encoders.test.EncoderTestSuite

    #
    # create the test jar
    #
    cat > classes/test.mf <<%
Manifest-Version: 1.0
Extension-Name: org.bouncycastle.bctest
Specification-Vendor: BouncyCastle.org
Specification-Version: 1.1
Implementation-Vendor-Id: org.bouncycastle
Implementation-Vendor: BouncyCastle.org
Implementation-Version: $version.0
%

    (cd classes; jar cfm ../../bctest-jdk13-$base.jar test.mf org/bouncycastle/asn1/test org/bouncycastle/crypto/test org/bouncycastle/jce/provider/test)

    rm -rf classes/org/bouncycastle/asn1/test
    rm -rf classes/org/bouncycastle/crypto/test
    rm -rf classes/org/bouncycastle/jce/provider/test
    rm -rf classes/org/bouncycastle/ocsp/test


    #
    # add the manifest
    #
    cat > classes/provider.mf <<%
Manifest-Version: 1.0
Extension-Name: org.bouncycastle.bcprovider
Specification-Vendor: BouncyCastle.org
Specification-Version: 1.1
Implementation-Vendor-Id: org.bouncycastle
Implementation-Vendor: BouncyCastle.org
Implementation-Version: $version.0
%

    cat > classes/jce.mf <<%
Manifest-Version: 1.0
Extension-Name: org.bouncycastle.bcjce
Specification-Vendor: BouncyCastle.org
Specification-Version: 1.1
Implementation-Vendor-Id: org.bouncycastle
Implementation-Vendor: BouncyCastle.org
Implementation-Version: $version.0
%

    rm -f classes/org/bouncycastle/jce/provider/test/NIST*
    (cd classes; jar cfm ../../bcprov-jdk13-$base.jar provider.mf org)
    (cd classes; jar cfm ../../jce-jdk13-$base.jar jce.mf javax org)
    )

    (2>&1 find jce-jdk13-$base -name CVS -exec rm -rf \{\} \;) > /dev/null
fi

if test "$base" != "" -a ! -d bcpg-jdk13-$base
then
    echo "making OpenPGP release"

    mkdir bcpg-jdk13-$base
    tar cf - index.html LICENSE.html CONTRIBUTORS.html releasenotes.html specifications.html src/org/bouncycastle/bcpg src/org/bouncycastle/openpgp | (cd bcpg-jdk13-$base; tar xf -)
    (cd jdk1.3; tar cf - * | (cd ../bcpg-jdk13-$base/src; tar xf -))
    (
    cd bcpg-jdk13-$base; mkdir classes; mkdir docs;

    PATH=$JDK13PATH/bin:$PATH
    export PATH

    rm -rf src/org/bouncycastle/jce
    rm -rf src/org/bouncycastle/ocsp
    rm -rf src/org/bouncycastle/mail
    rm -rf src/org/bouncycastle/cms
    rm -rf src/org/bouncycastle/tsp

    javadoc -windowtitle "$WINDOWTITLE" -doctitle "$DOCTITLE" \
        -header "$HEADER" \
        -group "BCPG Support Packages" "org.bouncycastle.bcpg*" \
        -group "OpenPGP Packages" "org.bouncycastle.openpgp*" \
        -classpath classes:../jce-jdk13-$base/classes \
        -d docs -sourcepath src \
        org.bouncycastle.bcpg \
        org.bouncycastle.bcpg.attr \
        org.bouncycastle.bcpg.sig \
        org.bouncycastle.openpgp \
        org.bouncycastle.openpgp.examples \
        org.bouncycastle.openpgp.test > /dev/null \

    echo "compiling"

    (cd src/org/bouncycastle; javac -d ../../../classes -classpath ../../../classes:../../../src:../../../../jce-jdk13-$base/classes */*.java */*/*.java)
    cat > classes/pg.mf <<%
Manifest-Version: 1.0
Extension-Name: org.bouncycastle.bcpg
Specification-Vendor: BouncyCastle.org
Specification-Version: 1.1
Implementation-Vendor-Id: org.bouncycastle
Implementation-Vendor: BouncyCastle.org
Implementation-Version: $version.0
%

    (cd classes; jar cmf pg.mf ../../bcpg-jdk13-$base.jar org)
    java -classpath ../jce-jdk13-$base/classes:classes org.bouncycastle.openpgp.test.RegressionTest
    )

    (2>&1 find bcpg-jdk13-$base -name CVS -exec rm -rf \{\} \;) > /dev/null
fi

if test "$base" != "" -a ! -d bcmail-jdk13-$base
then
    echo "making Mail release"

    mkdir bcmail-jdk13-$base
    tar cf - index.html LICENSE.html CONTRIBUTORS.html releasenotes.html specifications.html src/org/bouncycastle/mail src/org/bouncycastle/cms | (cd bcmail-jdk13-$base; tar xf -)
    (cd jdk1.3; tar cf - * | (cd ../bcmail-jdk13-$base/src; tar xf -))
    (
    cd bcmail-jdk13-$base; mkdir classes; mkdir docs;

    PATH=$JDK13PATH/bin:$PATH
    export PATH

    rm -rf src/java
    rm -rf src/org/bouncycastle/jce
    rm -rf src/org/bouncycastle/ocsp
    rm -rf src/org/bouncycastle/openpgp
    rm -rf src/org/bouncycastle/tsp

    javadoc -windowtitle "$WINDOWTITLE" -doctitle "$DOCTITLE" \
        -header "$HEADER" \
        -group "CMS Packages" "org.bouncycastle.cms*" \
        -group "S/MIME Packages" "org.bouncycastle.mail.smime*" \
        -classpath classes:../jce-jdk13-$base/classes \
        -d docs -sourcepath src \
        org.bouncycastle.cms \
        org.bouncycastle.cms.test \
        org.bouncycastle.mail.smime \
        org.bouncycastle.mail.smime.handlers \
        org.bouncycastle.mail.smime.examples \
        org.bouncycastle.mail.smime.test > /dev/null \

    echo "compiling"

    (cd src/org/bouncycastle; javac -d ../../../classes -classpath ../../../classes:../../../src:../../../../jce-jdk13-$base/classes */*.java */*/*.java */*/*/*.java)
    cat > classes/mail.mf <<%
Manifest-Version: 1.0
Extension-Name: org.bouncycastle.bcmail
Specification-Vendor: BouncyCastle.org
Specification-Version: 1.1
Implementation-Vendor-Id: org.bouncycastle
Implementation-Vendor: BouncyCastle.org
Implementation-Version: $version.0
%

    (cd classes; jar cmf mail.mf ../../bcmail-jdk13-$base.jar org)
    java -classpath ../jce-jdk13-$base/classes:classes org.bouncycastle.cms.test.AllTests
    java -classpath ../jce-jdk13-$base/classes:classes org.bouncycastle.mail.smime.test.AllTests

    )

    (2>&1 find bcmail-jdk13-$base -name CVS -exec rm -rf \{\} \;) > /dev/null
fi

if test "$base" != "" -a ! -d bctsp-jdk13-$base
then
    echo "making TSP release"

    mkdir bctsp-jdk13-$base
    tar cf - index.html LICENSE.html CONTRIBUTORS.html releasenotes.html specifications.html src/org/bouncycastle/tsp | (cd bctsp-jdk13-$base; tar xf -)
    (cd jdk1.3; tar cf - * | (cd ../bctsp-jdk13-$base/src; tar xf -))
    (
    cd bctsp-jdk13-$base; mkdir classes; mkdir docs;

    PATH=$JDK13PATH/bin:$PATH
    export PATH

    rm -rf src/java
    rm -rf src/org/bouncycastle/jce
    rm -rf src/org/bouncycastle/ocsp
    rm -rf src/org/bouncycastle/openpgp
    rm -rf src/org/bouncycastle/cms
    rm -rf src/org/bouncycastle/mail

    javadoc -windowtitle "$WINDOWTITLE" -doctitle "$DOCTITLE" \
        -header "$HEADER" \
        -group "TSP Packages" "org.bouncycastle.tsp*" \
        -classpath classes:../jce-jdk13-$base/classes:../bcmail-jdk13-$base/classes \
        -d docs -sourcepath src \
        org.bouncycastle.tsp \
        org.bouncycastle.tsp.test > /dev/null \

    echo "compiling"

    (cd src/org/bouncycastle; javac -d ../../../classes -classpath ../../../classes:../../../src:../../../../jce-jdk13-$base/classes:../../../../bcmail-jdk13-$base/classes */*.java */*/*.java)
    cat > classes/tsp.mf <<%
Manifest-Version: 1.0
Extension-Name: org.bouncycastle.bctsp
Specification-Vendor: BouncyCastle.org
Specification-Version: 1.1
Implementation-Vendor-Id: org.bouncycastle
Implementation-Vendor: BouncyCastle.org
Implementation-Version: $version.0
%

    (cd classes; jar cmf tsp.mf ../../bctsp-jdk13-$base.jar org)
    java -classpath ../jce-jdk13-$base/classes:../bcmail-jdk13-$base/classes:classes org.bouncycastle.tsp.test.RegressionTest
    )

    (2>&1 find bctsp-jdk13-$base -name CVS -exec rm -rf \{\} \;) > /dev/null
fi
if false # test "$base" != ""
then
    echo "making tools release"

    mkdir bctools-jdk13-$base
    tar cf - index.html LICENSE.html CONTRIBUTORS.html releasenotes.html specifications.html | (cd bctools-jdk13-$base; tar xf -)
    (cd tools; tar cf - src) | (cd bctools-jdk13-$base; tar xf -)
    (
    cd bctools-jdk13-$base; mkdir classes; mkdir docs;

    javadoc -windowtitle "$WINDOWTITLE" -doctitle "$DOCTITLE" \
        -header "$HEADER" -group "Core Packages" "org.bouncycastle*" \
        -classpath classes \
        -d docs -sourcepath src -breakiterator \
        org.bouncycastle.tools.openpgp \
        org.bouncycastle.tools.openpgp.dump \
        org.bouncycastle.tools.openpgp.rampage \
        org.bouncycastle.tools.openpgp.util \
        > /dev/null \

    PATH=$JDK13PATH/bin:$PATH
    export PATH

    echo "compiling"
    (cd src/org/bouncycastle; javac -d ../../../classes -classpath ../../../classes:../../../../jce-jdk13-$base/classes:../../../../bcpg-jdk13-$base/classes:../../../tools/src */*/*.java */*/*/*.java)
    cat > classes/tools.mf <<%
Manifest-Version: 1.0
Extension-Name: org.bouncycastle.tools
Specification-Vendor: BouncyCastle.org
Specification-Version: 1.1
Implementation-Vendor-Id: org.bouncycastle
Implementation-Vendor: BouncyCastle.org
Implementation-Version: $version.0
%

    (cd classes; jar cmf tools.mf ../../bctools-jdk13-$base.jar org)
    )

    (2>&1 find bctools-jdk13-$base -name CVS -exec rm -rf \{\} \;) > /dev/null
fi
