# -*- shell-script -*-
# cygwin/mknetrel - GUILE mknetrel overrides

# This script should be used to package GUILE for Cygwin.
# It can be used to build GUILE on Cygwin, but also on GNU/Linux.
#
# See http://lilypond.org/web/devel/packaging.html for more information,
# report bugs to bug-lilypond@gnu.org.

sover=16
sub_packages="lib devel doc"
old_path=$PATH

patch=$mknetrel_root/patch

# for cross-compiling
if ! iscygwin; then
    uploads=$here/uploads/$base && mkdir -p $uploads/
    tarstem=$uploads/$package
fi

# copy and include extra mknetrel stuff
extras="doc patch auto cross split"
for i in $extras; do
    f=mknetrel.$i
    [ -r $extra/$f ] && cp -pv $extra/$f $src/cygwin/$f
    . $src/cygwin/$f
done


needdevoflags () {
    false
}

fix_libtool_script () {
    cd $build || exit 1

    # Fix libtool's -rpath detection
    export CC="${build_cc}"
    export CXX="${build_cxx}"
    export DLLTOOL=${build_dlltool}
    export OBJDUMP=${build_objdump}
    export AS="${build_as}"
    export HOST_CC=/usr/bin/gcc
    export allow_undefined="yes"
    export allow_undefined_flag=""
    
    # fix libtool's dlltool usage
    mv libtool libtool.orig
    sed -e 's/^DLLTOOL="dlltool"/DLLTOOL="i686-cygwin-dlltool"/' \
        -e 's/^DLLTOOL=/HOST_CC=gcc\nDLLTOOL=/' \
        -e 's/^HOST_CC=/SED=sed\nHOST_CC=/' < libtool.orig > libtool
    chmod +x libtool
}

fix_libtool_la () {
    # fix libtool's .la dependency_libs output for dlopen
    for i in $inst/lib $inst/$prefix/lib; do
	if [ -d $i ]; then
	    cd $i || exit 1
	    for j in $(ls -1 *.la); do
		mv $j .libtool-$j
		sed -e 's/ *-L *[^ ][^ ]*//g' .libtool-$j > $j
		chmod +x $j
	    done
	fi
    done
}

preconfig () {

    patchsrc 
    rm -f config.cache

    cd $src || exit 1

    export allow_undefined="yes"
    export allow_undefined_flag=""
    mv configure configure.orig
    sed -e 's/\(allow_undefined_flag=.*\)unsupported/\1/' < configure.orig > configure
    chmod 755 configure

    # mv libtool fix    
    autoupdate

    # 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}\ "

    # 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. -I$build -I$src -I$build/libguile -I$src/libguile -I$cygwin_prefix/include -L$cygwin_prefix/lib -L$cygwin_prefix/lib/w32api -L$cygwin_prefix/bin"
    
    # Libtool/guile-1.5.6 workaround.
    cd $build || exit 1
    mkdir -p libguile/.libs
    #guileflags="-L../libguile/.libs -I$cygwin_prefix/include -I$cygwin_prefix/include/w32api"

    #  workaround for guile-1.6.4's  libguile-ltdl libtool kludge
    cd $build || exit 1
    mkdir -p libguile-ltdl/.libs
    guileflags="-L../libguile/.libs -I$build -I$src -I$build/libguile -I. -I$src/libguile -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

        

    cd $build || exit 1

    #CONFIGOPTS="--config-cache --disable-shared --enable-static $CONFIGOPTS"
    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 () {
    fix_libtool_script

    # Here's another lib for you, libtool
    cp -f libtool guile-readline

    # And another two.
    cd $build || exit 1
    cd test-suite
    ln -sf ../libguile ../libguile-ltdl .
}

prebuild () {
    for i in $sub_packages; do
	rm -rf $inst-$i
	mkdir -p $inst-$i || exit 1
    done
}

preinstall () {
    :
}

postinstall () {
    fixdoc
    fix_libtool_la
    
    mkdir -p $inst-doc/$docprefix
    cd $build || exit 1
    
    cd $src || exit 1
    install -d -m755 $inst/$packagedocdir

    readmes="ANON-CVS AUTHORS BUGS COPYING ChangeLog HACKING INSTALL NEWS README SNAPSHOTS THANKS"
    install -m644 $readmes $inst/$packagedocdir

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

    install -d -m755 $inst/$mandir/man/man1 || exit 1
    install -m644 doc/guile.1 $inst/$mandir/man/man1
}

prepackage () {
    mv $inst/$prefix/bin/guile-* $inst-devel/$prefix/bin
}
