# -*- shell-script -*-

# stable Guile releases: guile-1.6.4
# archive=ftp://ftp.gnu.org/pub/gnu

# development releases
# archive=ftp://alpha.gnu.org/pub/gnu


# Guile specific mknetrel overrides
# To use this, do something like:
cat >/dev/null <<EOC

   wget -P/var/tmp ftp://ftp.gnu.org/pub/gnu/guile/guile-1.6.4.tar.gz
   rm -rf /home/netrel/src/guile-*
   mkdir -p /home/netrel/src && cd /home/netrel/src
   ## TODO: look at pimport
   tar xzf /var/tmp/guile-*
   mv guile-* $(echo guile-*)-1
   cd -
   ./bin/mknetrel -xXC guile 

EOC

sover=12
[ "$ABI" != "1.5" ] && sover=12abi13

patch=$mknetrel_root/patch
if iscygwin; then
    true
else    
    uploads=$here/uploads/$base && mkdir -p $uploads/
    [ "$ABI" = "1.5" ] && uploads=$here/uploads-development/$base && mkdir -p $uploads/
    tarstem=$uploads/$package
fi

sub_packages="lib devel doc"


# include missing mknetrel stuff
. $extra/mknetrel.doc
. $extra/mknetrel.patch
. $extra/mknetrel.auto
. $extra/mknetrel.cross
. $extra/mknetrel.split


needdevoflags () {
    return 1
}


preconfig () { # aka libtool_woes ()

    # Add to mknetrel?
    patchsrc 

    cd $src || exit 1
    
    # Fix libtool's -rpath detection
    export CC="${build_cc}"
    export CXX="${build_cxx}"
    export AS="${build_as}"
    export HOST_CC=/usr/bin/gcc
    export allow_undefined="yes"
    export allow_undefined_flag=""
    autoupdate
    
    (cd guile-readline && automake)
    

    # URG, needed for libtool-20030723 CVS
    # mv libtool libtool.orig
    # sed -e 's/^\(allow_undefined_flag=.*\)unsupported/\1/' < libtool.orig > libtool
    # chmod 755 libtool
    mv configure configure.orig
    sed -e 's/\(allow_undefined_flag=.*\)unsupported/\1/' < configure.orig > configure
    chmod 755 configure

    ## debugging is more pleasant without -O2
    ## opt="-g"
	        
    cd $build || exit 1

    # What's this? docme
    # LIBS=`i686-pc-cygwin-gcc --print-file-name=binmode.o`
    # LIBS=`i686-pc-cygwin-gcc --print-file-name=textmode.o`
    # export LIBS

    # Help automake with confusing EXEEXT variable.
    # In our case, we actually have: BUILD_EXEXT= HOST_EXEEXT=.exe
    ### EXTRABUILDARGS="EXEEXT="
    ### EXTRABUILDARGS="\ EXEEXT=\ AS=${build_as}\ "
    ### EXTRAINSTALLARGS="EXEEXT="

    # guile-1.6.4 is fine with EXEEXT=.exe.  However, now (20030717),
    # libtool-CVS needs to be told what AS we're using.
    EXTRABUILDARGS="\ AS=${build_as}\ "

    # Convince libtool that 'SED=' won't work.
    export SED=sed

    # Ugh.  Libtool (20030717-CVS) compile and link needs these extra
    # flags We cannot add these to CPPFLAGS/CFLAGS/LDFLAGS, as they
    # then would end up in libguile.* and thus show in guile-config
    # script.
    libtoolflags="-I$cygwin_prefix/include -L$cygwin_prefix/lib -L$cygwin_prefix/lib/w32api -L$cygwin_prefix/bin"
    
    # Libtool/guile-1.5.6 workaround.
    mkdir -p libguile/.libs
    #guileflags="-L../libguile/.libs -I$cygwin_prefix/include -I$cygwin_prefix/include/w32api"

    #  workaround for guile-1.6.0 libtool
    #mkdir -p libltdl/.libs
    #guileflags="-L../libguile/.libs -I$cygwin_prefix/include -I$cygwin_prefix/include/w32api -L ../libltdl/.libs"
    
    #  workaround for guile-1.6.4's  libguile-ltdl libtool kludge
    mkdir -p libguile-ltdl/.libs
    guileflags="-L../libguile/.libs -I$cygwin_prefix/include -I$cygwin_prefix/include/w32api -L../libguile-ltdl/.libs"
    
    build_cc="${build_cc} $libtoolflags $guileflags"
    build_cxx="${build_cxx} $libtoolflags $guileflags"
    
    # Fix libtool's -rpath detection
    export CC="${build_cc}"
    export CXX="${build_cxx}"
    export AS="${build_as}"
    export HOST_CC=/usr/bin/gcc

    # anyway, Libtool doesn't like it
    # libtool: link: cannot build libtool library `libguile.la' from non-libtool objects on this host: /home/cygwin/cygwin-1.3.10/usr/lib/textmode.o

    # Let's adhere to mkconfig connvention, but why not use CONFIG_SITE?
    # build_config_opts="${build_config_opts} --cache-file=/dev/null"
    # export CONFIG_SITE=$(pwd)/config.site
    #  cat < < E OF > config.site
    
    CONFIGOPTS="--config-cache --with-gnu-ld $CONFIGOPTS"
    iscygwin || crosscache
    iscygwin || cat <<EOF >> config.cache
guile_cv_func_usleep_declared=${guile_cv_func_usleep_declared=yes}
guile_cv_exeext=${guile_cv_exeext=}
libltdl_cv_sys_search_path=${libltdl_cv_sys_search_path="'"$cygwin_prefix/lib"'"}
EOF
}

postconfig () {
    # Here's another lib for you, libtool
    cp -f libtool guile-readline
}

#prepackage () {
postinstall () {
    fixdoc || exit 1
    
    ## rm -f $inst/$prefix/share/guile/1.6.0/ice-9/'and-let*.scm'
    
    cd $src || exit 1
    ## cygwin=CYGWIN-PATCHES
    cygwin=cygwin
    readmes="ANON-CVS AUTHORS BUGS COPYING ChangeLog HACKING INSTALL NEWS README SNAPSHOTS THANKS"

    install -d -m755 $inst/$packagedocdir || exit 1
    install -m644 $readmes $inst/$packagedocdir
    
    install -m644 $cygwin/README $inst/$packagedocdir/README.Cygwin
    install -m644 $cygwin/changelog $inst/$packagedocdir/changelog.Cygwin
    install -d -m755 $inst/$cygdocdir || exit 1
    install -m644 $cygwin/README $inst/$cygdocdir/$package.README

    install -d -m755 $inst/$mandir/man1 || exit 1
    install -m644 doc/guile.1 $inst/$mandir/man1
}
  
prepackage () {
    mv $inst/$prefix/bin/guile-* $inst-devel/$prefix/bin
}
