#!/bin/sh
#
# Quick fix script to patch FPC for FreeBSD 5 (hopefully avoids 
# the need for COMPAT_4 
#
# Can be run as part of the port, or after the port to fix an existing
# install
#
# First, optional parameter is the prefix where FPC was installed or
# /usr/local otherwise.
#
# This simple fix can't fix the fact that libc_r now also needs libc linked,
# however this is more a problem for the 1.9 series, since the 1.0 series
# don't support native threading. 
#
# If that problem occurs, it might be possible to circumvent this by adding 
# {$linklib c_r} 
# {$linklib c} 
# to the main program above the uses clause.

PREFIX=${1:-/usr/local}
ASMFILE=/tmp/cprt0.as
DETECTVERSION=`fpc -iV`
VERSION=${DETECTVERSION:-2.1.1}
# Note _must_ be cprt0.o 
OFILE=/tmp/cprt0.o

echo Patch FPC 2.0.x/2.1.1 for FreeBSD 5.4+ by Marco van de Voort v1
echo " "
echo "FPC Version :" $VERSION
echo "Prefix      :" $PREFIX   \( override by passing it as 1st parameter \)
echo "Action 1    :" write new startup code to $ASMFILE and then assemble it to $OFILE
echo "Action 2    :" Writing  $OFILE to $PREFIX/lib/fpc/$VERSION/units/i386-freebsd/rtl

echo " "
echo If you don\'t like this, press ctrl-c now!
read key


echo "creating  " $ASMFILE

cat << ::FPCEOF  > $ASMFILE
#
#   \$Id: cprt0.as,v 1.3 2000/11/21 19:03:23 marco Exp \$
#   This file is part of the Free Pascal run time library.
#   Copyright (c) 1999-2000 by Marco van de Voort, Michael Van Canneyt
#                                                  and Peter Vreman
#   members of the Free Pascal development team.
#
#   See the file COPYING.FPC, included in this distribution,
#   for details about the copyright.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY;without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
#**********************************************************************}
#
# FreeBSD  ELF startup code for Free Pascal for dynamical linking to libc.
#

        .file   "cprt0.as"
        .ident  "FreePascal 2.0.x series with FreeBSD 5/6 patch"
.section        .note.ABI-tag,"a",@progbits
        .p2align 2
        .type   abitag, @object
        .size   abitag, 24
abitag:
        .long   8
        .long   4
        .long   1
        .string "FreeBSD"
        .long   504000
        .section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
        .string ""
.globl __progname
	.data
        .p2align 2
        .type    __progname,@object
        .size    __progname,4
__progname:
        .long .LC0
        .text
        .p2align  2,,3
___fpucw:
        .long   0x1332
        .globl  ___fpc_brk_addr         /* heap management */
        .type   ___fpc_brk_addr,@object
        .size   ___fpc_brk_addr,4
___fpc_brk_addr:
        .long   0

	.text
        .p2align 4,,15
.globl _start
        .type    _start,@function
_start:
        pushl %ebp
        movl %esp,%ebp
	subl \$40,%esp
	call get_rtld_cleanup
	movl %eax,%edx

        pushl %edi
        pushl %esi
        pushl %ebx
#APP
        movl %edx,%edx
#NO_APP
        leal 8(%ebp),%edi
        movl %edi,operatingsystem_parameter_argv
        mov -4(%edi),%eax
        movl %eax,operatingsystem_parameter_argc
        movl 4(%ebp),%ebx
        leal 12(%ebp,%ebx,4),%esi
        movl %esi,operatingsystem_parameter_envp
        movl %esi,environ
        testl %ebx,%ebx
        jle .L2
        movl 8(%ebp),%eax
        testl %eax,%eax
        je .L2
        movl %eax,__progname
        cmpb \$0,(%eax)
        je .L2
        .p2align 2,0x90
.L6:
        cmpb \$47,(%eax)
        jne .L5
        leal 1(%eax),%ecx
        movl %ecx,__progname
.L5:
        incl %eax
        cmpb \$0,(%eax)
        jne .L6
.L2:
        movl \$_DYNAMIC,%eax
        testl %eax,%eax
        je .LTLS
        pushl %edx
        call atexit
        addl \$4,%esp
.L9:
        pushl \$_fini
        call atexit
        call _init
#       pushl %esi
#       pushl %edi
#       pushl %ebx
#       call main
#       pushl %eax
#       call exit


        finit                           /* initialize fpu */
        fwait
        fldcw   ___fpucw

        xorl    %ebp,%ebp

        call main
        pushl %eax
        jmp   _haltproc
.LTLS:  
	call _init_tls
	jmp .L9
        .p2align 2,0x90
       
 
.globl _haltproc
.type _haltproc,@function

_haltproc:
           mov \$1,%eax  
           movzwl operatingsystem_result,%ebx
           pushl %ebx
           call .Lactualsyscall
           addl  \$4,%esp
           jmp   _haltproc

.Lactualsyscall:
         int \$0x80
         jb .LErrorcode
         xor %ebx,%ebx
         ret
.LErrorcode:
         mov %eax,%ebx
         mov \$-1,%eax
         ret
        .p2align 2,0x90
.Lfe1:
        .size    _start,.Lfe1-_start
        .comm   environ,4,4
        .p2align 4,,15
        .type   get_rtld_cleanup, @function
get_rtld_cleanup:
        pushl   %ebp
        movl    %esp, %ebp
        subl    \$4, %esp
#APP
        movl %edx,-4(%ebp)
#NO_APP
        movl    -4(%ebp), %eax
        leave
        ret

        .weak   _DYNAMIC
        .ident  "GCC: (GNU) 3.4.2 - FPC: 2.0.2"
.bss
        .comm operatingsystem_parameter_envp,4
        .comm operatingsystem_parameter_argc,4
        .comm operatingsystem_parameter_argv,4        
        
::FPCEOF

echo assembling $ASMFILE to $OFILE
as $ASMFILE -o $OFILE

echo "moving    " $OFILE to $PREFIX/lib/fpc/$VERSION/units/i386-freebsd/rtl

mv $OFILE $PREFIX/lib/fpc/$VERSION/units/i386-freebsd/rtl
