# -*- shell-script -*-
# mknetrel.split -- split subpackages

extras_preinstall () {
    :;
}

extras_presplit () {
    :;
}

presplit () {
    :;
}

devel_name () {
    if ! libdev 2> /dev/null ; then
	echo $base-devel
    else
	if expr "$base" : "\(lib\)" > /dev/null 2>&1; then
	    echo $base-dev
	else
	    echo lib$base-dev
	fi
    fi
}

doc_name () {
    echo $base-doc
}

lib_name () {
    if expr "$base" : "\(lib\)" > /dev/null 2>&1; then
	echo $base$sover
    else
	echo lib$base$sover
    fi
}

extras_lib_split () {
    mkdir -p ./$prefix/bin || exit 1
    mv $inst/$prefix/bin/*.dll ./$prefix/bin
    # better move dlls to bin, see gmp
    mkdir -p ./$prefix/lib || exit 1
    mv $inst/$prefix/lib/*.dll ./$prefix/lib
    mkdir -p ./$prefix/lib || exit 1
    mv $inst/$prefix/lib/lib*.la ./$prefix/lib
    mkdir -p ./$prefix/share || exit 1
    mv $inst/$prefix/share/$base ./$prefix/share
    rmdir $inst/$prefix/bin || true
    rmdir $inst/$prefix/lib || true
    rmdir $inst/$prefix/share || true
    true
}
   
extras_devel_split () {
    mkdir -p ./$prefix/bin || exit 1
    mv $inst/bin/*-config ./$prefix/bin
    tar -C $inst -cf - ./$prefix/include | tar -xf -
    rm -rf $inst/$prefix/include
    # Only static .a libs in devel, load time .la files go in LIB or
    # BIN package.
    mkdir -p ./$prefix/lib || exit 1
    mv $inst/$prefix/lib/*.a ./$prefix/lib
    mv $inst/$prefix/lib/pkgconfig ./$prefix/lib
    mkdir -p ./$prefix/share || exit 1
    tar -C $inst -cf - ./$prefix/share/aclocal | tar -xf -
    rm -rf $inst/$prefix/share/aclocal
    tar -C $inst -cf - ./$prefix/share/libtool | tar -xf -
    rm -rf $inst/$prefix/share/libtool
    rmdir $inst/$prefix/lib || true
    rmdir $inst/$prefix/share || true
    true
}

extras_doc_split () {
    mkdir -p ./$docprefix || exit 1
    mkdir -p ./$infodir || exit 1
    tar -C $inst -cf - ./$infodir | tar -xf -
    rm -rf $inst/$infodir
    if [ "$cygwin" == "mingw" ]; then
	mkdir -p ./$mandir || exit 1
	tar -C $inst -cf - ./$mandir | tar -xf -
	rm -rf $inst/$mandir
    fi
    true
}

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

extras_postinstall () {
    fixdoc
    fix_libtool_la
    
    mkdir -p $inst-doc/$docprefix
    cd $build || exit 1

    cd $src || exit 1
    install -d -m755 $inst/$packagedocdir

    readmes="$(find . -maxdepth 1 -type f -a -name '[A-Z]*' | egrep -v 'Makefile|GNUmakefile')"
    install -m644 $readmes $inst/$packagedocdir
    install -m644 $cygwin/changelog $inst/$packagedocdir/changelog.$Cygwin

    ## Cygwin readmes
    cd $src || exit 1
    install -d -m755 $inst/$cygdocdir

    cd $src || exit 1
    install -m644 $cygwin/README $inst/$packagedocdir/README.$Cygwin
    install -m644 $cygwin/README $inst/$cygdocdir/$package.README
    cd $build || exit 1
    install -m644 $cygwin/out/README $inst/$packagedocdir/README.$Cygwin
    install -m644 $cygwin/out/README $inst/$cygdocdir/$package.README
}

auto_requires () {
    dlls=
    requires=
    dlls=$(find . -name '*dll' -o -name '*.exe' \
	| xargs $cygcheck 2>&1 | grep '^  [^ ]' \
	| tr -d '[ \r]' | sed 's@.*\\@@g' | sort -u)
    for x in $(ls -1 bin/* sbin/*); do
	case $x in
	    *.exeXXX|*.dllXXX)
		dlls="$($cygcheck $x 2>&1 | grep '^  [^ ]' | tr -d '[ \r]' | sed 's@.*bin\\@@g') $dlls"
		;;
	esac
	case "$(file -b $x)" in
	    "*Bourne shell script*")
		requires="bash\n$requires"
		;;
	    "*guile*script*")
		requires="guile\n$requires"
		;;
	    "*perl*script*")
		requires="perl\n$requires"
		;;
	    "*python*script*")
		requires="python\n$requires"
		;;
	esac
    done
    for x in $dlls; do
	requires="$(cyg-apt find $x | sed 's/:.*//')\n$requires"
    done
    requires=$(echo -e "$requires" | sort -u)
    echo $requires
}

hint () {
    if [ -r $shint ]; then
	cp $shint $hint
    else
	cat > $hint <<EOF
sdesc: "$name"
ldesc: "$name - no description (missing $package/$cygwin/$(basename $shint) file)"
EOF
    fi
    grep ^requires: $hint || echo "requires: $depends" >> $hint
    if [ "$cygwin" == "cygwin" ]; then
	if grep ^build-requires: $hint; then
	    mv $hint $hint~
	    grep -v build-requires $hint~ > $hint
	fi
	if iscygwin; then
	    cygcheck=cygcheck.exe
	else
	    cygcheck="wine $cygwin_prefix/bin/cygcheck.exe"
	fi
	echo "#auto-requires: $(cd usr; auto_requires)" >> $hint
    else
	grep ^build-requires: $hint || echo "build-requires: $build_depends" >> $hint
    fi
}

domkdist () {
    #
    # Fix up installation slightly
    #
    cd $inst || exit 1
    chmod -R u+w,og-w,a+rX .
    cd usr 2>/dev/null && dousrstuff

    # Split off any sub-packages.  The actual splitting is handled by
    # a package's specific code in extra/.

    # To package `foo' in foo, foo-doc and foo-devel, set
    # sub_packages='doc devel'.

    # Then provide functions doc_name and devel_name, that return the
    # full name of the sub-package.  Also provide functions doc_split
    # and devel_split, that mv part of foo's installation in $inst to
    # the sub-packages' installation roots $inst-doc and $inst-devel.

    cd $inst || exit 1
    presplit

    for i in $sub_packages; do
	subname=$(${i}_name)
  	cd $inst-$i || exit 1
	${i}_split || exit 1
    done
    
    cd $inst || exit 1
    prepackage

    cd $inst || exit 1
    hard2soft

    #
    # Make tar balls
    #

    # The base package
    echo creating $tarstem.tar.bz2
    cd $inst || exit 1

    name=$base
    shint=$src/$cygwin/$name.hint
    hint=$uploads/setup.hint
    hint
    find * -print | egrep -v '\.cvsignore|\.bak$|\.orig$|\.o$|\.rej$|~$|^.#|CVS|/tags$' | sort | tar -T - --no-recursion --owner=0 --group=0 -cjf $tarstem.tar.bz2

    # Any sub-packages
    for i in $sub_packages; do
	subname=$(${i}_name)
	subload=$uploads/$subname
	subtarstem=$subload/$subname-$ver
	shint=$src/$cygwin/$subname.hint
	hint=$subload/setup.hint
	depends=$(eval echo '$'${i}_depends)
	mkdir -p $subload || exit 1
	echo creating $subtarstem.tar.bz2
	name=$subname
  	cd $inst-$i
	hint
	grep ^external-source: $hint || echo "external-source: $base" >> $hint
	hard2soft
	find * -print | egrep -v '\.cvsignore|\.bak$|\.orig$|\.o$|\.rej$|~$|^.#|CVS|/tags$' | sort | tar -T - --no-recursion --owner=0 --group=0 -cjf $subtarstem.tar.bz2
    done

    # The source package
    cd $src/.. || exit 1
    echo creating "$tarstem"-src.tar.bz2
    ##find $package_src/* -print -follow | egrep -v '\.cvsignore|\.bak$|\.orig$|\.o$|~$|^.#|CVS|%redact|/tags$' | egrep -v "$src_exclude" | sort | tar -T - --no-recursion --owner=0 --group=0 -cjf "$tarstem"-src.tar.bz2
    find $package_src/* -print | egrep -v '\.cvsignore|\.bak$|\.orig$|\.o$|\.rej$|~$|^.#|CVS|%redact|/tags$|/TAGS$|autom4te.cache$' | egrep -v "$src_exclude" | sort | tar -T - --no-recursion --owner=0 --group=0 -cjf "$tarstem"-src.tar.bz2
    ##cd $package_src && tar -cj --exclude='\.cvsignore' --exclude='\.bak$' --exclude='\.orig$' --exclude='\.o$' --exclude='~$' --exclude='^.#' --exclude='CVS' --exclude='%redact' --exclude='/tags$' --exclude='/TAGS$' --exclude='autom4te.cache' --owner=0 --group=0 -f "$tarstem"-src.tar.bz2 .
    postpackage
}

devel_split () {
    extras_devel_split
}

doc_split () {
    extras_doc_split
}

lib_split () {
    extras_lib_split
}

prebuild () {
    extras_prebuild
}

postinstall () {
    extras_postinstall
}
