#!/bin/sh

# Copyright (c) 2002-2005 Michal Trojnara.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above
#    copyright notice, this list of conditions and the following
#    disclaimer in the documentation and/or other materials provided
#    with the distribution.
# 3. The name of the author may not be used to endorse or promote
#    products derived from this software without specific prior
#    written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

PERL=perl
MAKE=make
OPT=no-idea
PREFIX=i586-mingw32msvc-
LIB_CFLAG="-DCURSOR_SHOWING -DZLIB -DZLIB_SHARED"
EX_LIBS="-lzdll -lwsock32 -lgdi32"

echo "Patching util/pl/Mingw32.pl file"
patch -f -p0 << 'EOT'
--- util/pl/Mingw32.pl.orig 2006-09-10 15:56:48.000000000 +0200
+++ util/pl/Mingw32.pl	2006-09-10 15:45:22.000000000 +0200
@@ -6,11 +6,6 @@
 $o='/';
 $cp='cp';
 $rm='rm -f';
-$mkdir='gmkdir';
-
-$o='\\';
-$cp='copy';
-$rm='del';
 $mkdir='mkdir';
 
 # C compiler stuff
@@ -19,7 +14,7 @@
 if ($debug)
 	{ $cflags="-DL_ENDIAN -DDSO_WIN32 -g2 -ggdb"; }
 else
-	{ $cflags="-DL_ENDIAN -DDSO_WIN32 -fomit-frame-pointer -O3 -march=i486 -Wall"; }
+	{ $cflags="-DL_ENDIAN -DDSO_WIN32 -fomit-frame-pointer -O2 -march=pentium -Wall"; }
 
 if ($gaswin and !$no_asm)
 	{
@@ -87,7 +82,7 @@
 	($Name=$name) =~ tr/a-z/A-Z/;
 
 	$ret.="$target: \$(${Name}OBJ)\n";
-	$ret.="\tif exist $target \$(RM) $target\n";
+	$ret.="\t-\$(RM) $target\n";
 	$ret.="\t\$(MKLIB) $target \$(${Name}OBJ)\n";
 	$ret.="\t\$(RANLIB) $target\n\n";
 	}
EOT

echo "Patching util/util/libeay.num file"
patch -f -p0 << 'EOT'
--- util/libeay.num.orig	2006-09-10 16:19:51.000000000 +0200
+++ util/libeay.num	2006-09-10 16:20:23.000000000 +0200
@@ -2996,7 +2996,6 @@
 STORE_meth_get_generate_fn              3426	EXIST:VMS:FUNCTION:
 STORE_method_set_list_end_function      3427	EXIST:!VMS:FUNCTION:
 STORE_meth_set_list_end_fn              3427	EXIST:VMS:FUNCTION:
-pqueue_print                            3428	EXIST::FUNCTION:
 EC_GROUP_have_precompute_mult           3429	EXIST::FUNCTION:EC
 EC_KEY_print_fp                         3430	EXIST::FUNCTION:EC,FP_API
 BN_GF2m_mod_arr                         3431	EXIST::FUNCTION:
EOT

echo "Generating x86 for GNU assember"

echo -n "Bignum"
cd crypto/bn/asm
# ${PERL} x86.pl gaswin > bn-win32.s
${PERL} bn-586.pl gaswin > bn-win32.s
${PERL} co-586.pl gaswin > co-win32.s
cd ../../..

echo -n " DES"
cd crypto/des/asm
${PERL} des-586.pl gaswin > d-win32.s
cd ../../..

echo -n " crypt"
cd crypto/des/asm
${PERL} crypt586.pl gaswin > y-win32.s
cd ../../..

echo -n " Blowfish"
cd crypto/bf/asm
${PERL} bf-586.pl gaswin > b-win32.s
cd ../../..

echo -n " CAST5"
cd crypto/cast/asm
${PERL} cast-586.pl gaswin > c-win32.s
cd ../../..

echo -n " RC4"
cd crypto/rc4/asm
${PERL} rc4-586.pl gaswin > r4-win32.s
cd ../../..

echo -n " MD5"
cd crypto/md5/asm
${PERL} md5-586.pl gaswin > m5-win32.s
cd ../../..

echo -n " SHA1"
cd crypto/sha/asm
${PERL} sha1-586.pl gaswin > s1-win32.s
cd ../../..

echo -n " RIPEMD160"
cd crypto/ripemd/asm
${PERL} rmd-586.pl gaswin > rm-win32.s
cd ../../..

echo -n " RC5/32"
cd crypto/rc5/asm
${PERL} rc5-586.pl gaswin > r5-win32.s
cd ../../..

echo " CPUID"
cd crypto
${PERL} x86cpuid.pl gaswin > cpu-win32.s
cd ..

echo "Creating ms/mingw32.mak"
${PERL} util/mkfiles.pl > MINFO || exit 1
${PERL} util/mk1mf.pl ${OPT} gaswin Mingw32 > ms/mingw32.mak \
    || exit 1

# echo "Building OpenSSL"
${MAKE} -f ms/mingw32.mak LIB_CFLAG="${LIB_CFLAG}" \
    EX_LIBS="${EX_LIBS}" MKDIR=mkdir RM='rm -f' CC=${PREFIX}gcc \
    ASM=${PREFIX}as AR=${PREFIX}ar RANLIB=${PREFIX}ranlib || exit 1
cp out/openssl openssl.exe
${PREFIX}strip openssl.exe

echo "Generating DLL definition files"
${PERL} util/mkdef.pl 32 libeay ${OPT} > ms/libeay32.def || exit 1
${PERL} util/mkdef.pl 32 ssleay ${OPT} > ms/ssleay32.def || exit 1

echo "Building DLLs"
${PREFIX}dllwrap --dllname libeay32.dll --output-lib out/libeay32.a \
    --def ms/libeay32.def out/libcrypto.a -lwsock32 -lgdi32 -lzdll || exit 1
${PREFIX}dllwrap --dllname libssl32.dll --output-lib out/libssl32.a \
    --def ms/ssleay32.def out/libssl.a out/libeay32.a || exit 1

echo "Stripping debug information"
${PREFIX}strip --strip-debug libeay32.dll libssl32.dll

echo "Done"
