#! /bin/sh

# WARNING: This script is intended to be used to build
#          autoconf-2.59 from original autoconf-2.59.tar.gz
#          (or ....tar.bz2), but not from acnf259s.zip.
#          acnf259s.zip is a generated file for this script
#          in the same way as acnf259b.zip
#
# 1) Put original source archive in the empty directory
#    together with this script
# 2) add file autoconf.djgpp with additional information
#    to be copied to readme.DJGPP
# 3) if needed add also file autoconf.diff fith needed
#    patches (I didn't use any)
# 4) run script and got to do something else

package=autoconf
djname=acnf
version=2.59
pkgdir0=gnu
run_tests=true
conf_opt=
##############################################################################
dver=`echo $version | sed -e 's:\.::2g'`
dver0=`echo $version | sed -e 's:\.::g'`
djsrc=${djname}${dver0}s
djbin=${djname}${dver0}b
pkgdir=$pkgdir0/$package-$dver

top=`pwd`

test -d $pkgdir0 || mkdir $pkgdir0 || exit 1
cd $pkgdir0 || exit 1

UnpackSources () {
    if [ -d $package-$version ] ; then
        rm -rf $package-$version
    fi
    
    if [ -d $package-$dver ] ; then
        rm -rf $package-$dver
    fi
    
    if [ -f $top/$package-$version.tar.gz ] ; then
        tar xzf $top/$package-$version.tar.gz || exit 1
    elif [ -f $top/$package-$version.tar.bz2 ] ; then
        tar xf $top/$package-$version.tar.bz2 --use=bzip2 || exit 1
    else
        echo "Sources archive of $package-$version is not found"
        exit 1
    fi
    
    if test "$version" != "$dver" ; then
       mv -v $package-$version $package-$dver || exit 1
    fi

    if [ -f $top/$package.diff ] ; then
       cd $package-$dver || exit 1
       cp -v $top/$package.diff $package.diff
       patch -p1 <$top/$package.diff  
       aclocal
       automake
       autoconf		
       cd ..
    fi
}

MakeSourcePackage () {
    cp $top/$package.build $top/$pkgdir/
    cp $top/$package.djgpp $top/$pkgdir/readme.DJGPP
    rm -rf $top/manifest
    mkdir $top/manifest
    echo "$djsrc.zip - $package-$version source package for DJGPP" >$top/manifest/$djsrc.ver
    echo "" >$top/manifest/$djsrc.mft
    ( cd $top ; find $pkgdir -type f ; find manifest -name "$djsrc.*" ) |\
       sort >$top/manifest/$djsrc.mft
    ( cd $top && zip -9q@ $djsrc.zip <manifest/$djsrc.mft )
}

########################################################################



UnpackSources || exit 1
MakeSourcePackage 

cd $package-$dver || exit 1

export PATH_SEPARATOR=:

./configure --prefix=/dev/env/DJDIR || exit 1
make || exit 1
if $run_tests ; then
   make check >dj_check.log 2>&1
else
   echo "Testsuite is not run" >dj_check.log 
fi
make install prefix=`pwd`/install.djg || exit 1
cd install.djg || exit 1
mkdir -p $pkgdir0/$package-$dver
mv -v share/doc/$package-$version/* $pkgdir0/$package-$dver
cp -v ../dj_check.log $pkgdir0/$package-$dver/check.log

for x in about-nls authors ChangeLog COPYING INSTALL NEWS README README.DJGPP ; do
   if [ -f ../$x ] ; then
      cp -v ../$x $pkgdir0/$package-$dver/
   fi
done

if [ -d info ] ; then
    if [ -f info/dir ] ; then rm -v info/dir ; fi

    #  Fixes info files generated by makeinfo to fit in
    #  MS-DOS 8+3 filename limits
    #
    for file in info/*.info-*; do
	mv $file `echo $file | sed -e 's,info-,i,'`;  
    done

    for file in info/*.info; do
	echo $file
  	cat $file | \
	awk 'substr($0,1,9)=="Indirect:" { active=1; } \
	     substr($0,1,10)=="Tag Table:" { active=0; } \
	     { if (active) gsub("info-","i"); \
               print $0 }' > $file.new
        mv -f $file.new $file
    done
fi

if [ -d man ] ; then
    echo "Converting man pages ..."
    for x in `find man -type f -and -name '*.[1-9]*'`; do
        case $x in 
	   man/man*)
	       echo `basename $x`
	       x1=`echo $x | sed -e 's,^man/,,g'`
               c1=`echo $x1 | sed -e 's,man\([1-9]\)/,cat\1/,g'`
               ginstall -d man/`dirname $c1`
	       ( cd man && groff -man -Tascii $x1 >$c1 )
	       rm $x	
               ;;
        esac
    done
fi
  
strip bin/*.exe
strip -g lib/*.a
mkdir -p manifest
echo "$djbin.zip - $package-$version binary archive for DJGPP" >manifest/$djbin.ver
echo ''  >manifest/$djbin.mft
find . -type f | sed -e 's#^\./##' | sort >manifest/$djbin.mft
cat manifest/$djbin.mft | zip -9@ $top/$djbin.zip

