#! /bin/bash
#---------------------------------------------------------------
# Project         : Linux-Mandrake
# Module          : bin
# File            : configure
# Author          : Camille Bgnis
# Created On      : April 2003
# Copyright       : Camille Bgnis <camille@mandrakesoft.com>,
#                    MandrakeSoft under the GPL license
#---------------------------------------------------------------
# synopsis: configure [</path/to/new/project>] [[<language code>] [<initial release number>]]
# This script initiates a new Borges project when called with a
# directory argument in it. If called alone it simply make sure 
# Makefile and conf/Makefile are up to date.
#---------------------------------------------------------------

# get possible custom Borges path
if [ "$BORGESDIR" == "" ]; then
    if [ -s conf/author.xml ]; then 
	DISTDIR=$(grep borgesdir conf/author.xml | \
	    sed -e "s,.*<borgesdir>,,; s,</borgesdir>.*,,");
    else
	DISTDIR=/usr/share/Borges;
    fi; 
else 
    DISTDIR=$BORGESDIR;
fi

export DISTDIR

# if we cannot find Borges install, abort
if [ ! -e $DISTDIR/bin/configure ]; then
    echo "ERROR: looks like there is no Borges in $DISTDIR"
    echo "Provide your own Borges path with BORGESDIR environment variable"
    exit 1
fi

# Update myself if needed
if [ $DISTDIR/bin/configure -nt $0 ]; then
   cp -u $DISTDIR/bin/configure $0
fi

path=$1
lang=$2
rel=$3

# create new repository skeleton
if [ ! "$path" == '' ]; then 
    if [ -e $path ]; then
	echo "$path already exists... exiting."
	exit 1
    else
	mkdir $path || exit 1
	cd $path
	cp $DISTDIR/bin/configure .
	cp -a $DISTDIR/template/* .
	echo -n 1.0 > VERSION
	echo "New Borges project created in $(pwd)"
    fi
fi

# make sure basic Makefiles are up to date
cp -u $DISTDIR/template/Makefile .
cp -u $DISTDIR/template/conf/Makefile conf/

# change default language if needed
if [ ! "$lang" == '' ]; then 
    perl -pi -e "s,<lang>en</lang>,<lang>$lang</lang>," conf/{repository.xml,author.xml.in}
    perl -pi -e "s, lang=\"en\", lang=\"$lang\"," conf/repository.xml
    perl -pi -e "s,<language lang=\"en\">,<language lang=\"$lang\">," conf/manual-default.xml
fi

# change default release if needed
if [ ! "$rel" == '' ]; then 
    make release NEWREL=$rel
fi

# fill skeleton
rm -f Makefile.*
make configure DISTDIR=$DISTDIR

if [ "$BORGESDIR" != "" ]; then
# If I provide a path on command line, update author preferences
    perl -pi -e "s,<borgesdir>.*</borgesdir>,<borgesdir>$BORGESDIR</borgesdir>," conf/author.xml;
fi
