#!/bin/sh -
#
# build script for 1.4
#
# 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.
#

JDK14PATH=/opt/j2sdk1.4.2_04   # JDK 1.4 location

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"

if [ "$base" = "" ]
then
   echo "please provide the release number/name you want to create as argument!"
   echo "you may also need to adapt JDK14PATH"
   exit
fi

echo Java version to use - JDK14PATH set to $JDK14PATH

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

    mkdir lcrypto-jdk14-$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-jdk14-$base; tar xf -)

    (
        cd lcrypto-jdk14-$base;

        mkdir classes; mkdir docs;

        PATH=$JDK14PATH/bin:$PATH
        export PATH

        echo "compiling"

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

        javadoc -windowtitle "$WINDOWTITLE" -doctitle "$DOCTITLE" \
            -header "$HEADER" \
            -group "Lightweight Crypto Packages" "org.bouncycastle.crypto*" \
            -group "ASN.1 Support Packages" "org.bouncycastle.asn1*" \
            -group "Utility Packages" "org.bouncycastle.util*:org.bouncycastle.math*" \
            -classpath classes \
            -d docs -sourcepath src -breakiterator \
            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-jdk14-$base -name CVS -exec rm -rf \{\} \; ) > /dev/null
fi

if test "$base" != "" -a ! -d bcprov-jdk14-$base
then
    echo "making JCE provider"

    mkdir bcprov-jdk14-$base
    tar cf - index.html LICENSE.html CONTRIBUTORS.html releasenotes.html specifications.html src | (cd bcprov-jdk14-$base; tar xf -)
    (cd jdk1.4; tar cf -  org | (cd ../bcprov-jdk14-$base/src; tar xf -))
    (
    cd bcprov-jdk14-$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/jce/ECPointUtil.java
    rm -rf src/org/bouncycastle/jce/provider/JCEEC5*.java
    rm -rf src/org/bouncycastle/jce/provider/test/ECDSA5Test.java

    PATH=$JDK14PATH/bin:$PATH
    export PATH

    javadoc -windowtitle "$WINDOWTITLE" -doctitle "$DOCTITLE" \
        -header "$HEADER"\
        -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*" \
        -classpath classes \
        -d docs -sourcepath src -breakiterator \
        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.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 \
        org.bouncycastle.jce.interfaces \
        org.bouncycastle.jce.spec \
        org.bouncycastle.jce.examples \
        org.bouncycastle.jce.provider \
        org.bouncycastle.jce.provider.test \
        org.bouncycastle.ocsp \
        org.bouncycastle.ocsp.test \
        org.bouncycastle.openssl \
        org.bouncycastle.openssl.test \
        org.bouncycastle.math.ec \
        org.bouncycastle.mozilla \
        org.bouncycastle.mozilla.test \
        org.bouncycastle.x509 \
        org.bouncycastle.x509.examples \
        org.bouncycastle.util.encoders \
        org.bouncycastle.util.test > /dev/null \

    echo "compiling"

    rm -rf src/org/bouncycastle/mail;
    rm -rf src/org/bouncycastle/openpgp;
    rm -rf src/org/bouncycastle/bcpg;

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

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

    #
    # create test jar
    #
    cat > classes/details.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-jdk14-$base.jar details.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

    #
    # add the manifest
    #
    cat > classes/details.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
%

    (cd classes; jar cfm ../../bcprov-jdk14-$base.jar details.mf org)

    rm -rf classes
    )

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

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

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

    PATH=$JDK14PATH/bin:$PATH
    export PATH

    javadoc -windowtitle "$WINDOWTITLE" -doctitle "$DOCTITLE" \
        -header "$HEADER" \
        -group "CMS Packages" "org.bouncycastle.cms*" \
        -group "S/MIME Packages" "org.bouncycastle.mail.smime*" \
        -classpath classes:../jce-jdk14-$base/classes \
        -d docs -sourcepath src -breakiterator \
        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-jdk14-$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-jdk14-$base.jar org)
    )

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

if test "$base" != ""
then
    echo "making OpenPGP release"

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

    PATH=$JDK14PATH/bin:$PATH
    export PATH

    javadoc -windowtitle "$WINDOWTITLE" -doctitle "$DOCTITLE" \
        -header "$HEADER" \
        -group "BCPG Support Packages" "org.bouncycastle.bcpg*" \
        -group "OpenPGP Packages" "org.bouncycastle.openpgp*" \
        -classpath classes \
        -d docs -sourcepath src -breakiterator \
        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 */*.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-jdk14-$base.jar org)
    )

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

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

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

    PATH=$JDK14PATH/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:../bcmail-jdk14-$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:../../../../bcmail-jdk14-$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-jdk14-$base.jar org)
    java -classpath ../bcmail-jdk14-$base/classes:classes org.bouncycastle.tsp.test.RegressionTest
    )

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

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

    javadoc -windowtitle "$WINDOWTITLE" -doctitle "$DOCTITLE" \
        -header "$HEADER" -group "Core Packages" "org.bouncycastle*" \
        -group "Example Packages" "example*" -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 \

    echo "compiling"
    (cd src/org/bouncycastle; javac -d ../../../classes -classpath ../../../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-jdk14-$base.jar org)
    )

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