error()
{
    echo -e $@ 1>&2
    exit 1
}

run()
{
    echo $*
    $* || error "$1 failed!"
}

getyes()
{
    echo -n "y or n? "
    read ans
    if [ "$ans" != "y" ] ; then
        error "NO ?! Then do that first!"
    fi
}

if [ "$1" = "" ] ; then 
    error 'Usage: .makedist VERSION-NUMBER\nUse with "make dist".'
fi

echo "Making YODL distribution.."

echo "Did you adapt VER in the Makefile?"
getyes

echo "Did you adapt the ChangeLog file?"
getyes

echo "Did you make the htmldocs by \"make htmldoc\"?"
getyes

echo "Creating yodl.lsm.."
sed "s/__VER__/$1/" < .yodl.lsm > yodl.lsm || \
    error "Version change failed!"

echo "Creating INSTALL file from doc/ files."
awk '{
    if (! active)
    {
        if ($2 == "installstart")
            active = 1;
    }
    else
    {
        if ($2 == "installend")
            active = 0;
        else
            print;
    }
}' doc/yodl.yo > /tmp/yodl-install.yo || \
    error "Install-prefile creation failed!"

run yodl2txt -DINSTALL -P"SUBST(XXVERSIONXX)($1)" /tmp/yodl-install
run cp /tmp/yodl-install.txt INSTALL

echo "Creating README.yodl from .README.yodl and the ChangeLog.."
./.makeyodlreadme

echo "Putting the HTML docs to our WWW site."
./.uploadhtml || exit 1

echo "Cleaning up stale files."
make clean

echo "Ok, making archive.."
cd ..
run tar cvzf yodl-$1.tar.gz yodl-$1
cd yodl-$1

echo "Uploading to ftp site.."
ftp ftp << ENDFTP || error "Cannot upload to ftp!!"
    cd upload/unix
    bin
    put ../yodl-$1.tar.gz yodl-$1.tar.gz
    put README.yodl
    put yodl.lsm
    quit
ENDFTP

echo "All done."
