#! /bin/bash
#
# larch-setup
#
# Author: Michael Towers <gradgrind[at]online[dot]de>
#
# This file is part of the larch project.
#
#    larch is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    larch is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with larch; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#----------------------------------------------------------------------------
# 2010.10.02

REPO="ftp://ftp.berlios.de/pub/larch/larch8/i686"
#REPO=file://$( readlink -f larchrepo )

if [ "$1" = "-h" ]; then
    echo "larch-setup -h"
    echo "        # Display this information."
    echo "larch-setup"
    echo "        # Set up a larch build environment in the current directory."
    echo
    echo "This script unpacks larch, so that it"
    echo "can be run from this directory, without installing."
    echo "It also generates appropriate symlinks."
    echo "Also the 'liblarch' package will be downloaded and unpacked."
    echo
#TODO:
    echo "NEEDS UPDATING:"
    echo "If there is no pacman in the PATH, a package containing a"
    echo "pacman binary and the libraries and configuration files it needs"
    echo "will be downloaded from the larch site and unpacked in the"
    echo" larch/run directory."
    echo "Also repo-add is extracted there and a pacman.conf is generated in"
    echo "the current directory, presenting a dialog for choosing the package"
    echo "server."
    echo "You can use an existing pacman.conf by placing this in the current"
    echo "directory."
    echo
    exit
fi

# Get path to this directory, via the location of this script
fullpath="$( readlink -f $0 )"
scriptdir="$( dirname ${fullpath} )"

# Just in case ...
cd ${scriptdir}

if [ -d larch0 ]; then
    echo "ERROR: larch0 directory exists already"
    exit 1
fi

fetch ()
{
    if [ -n "$( echo ${REPO} | grep "file://" )" ]; then
        base="$( echo ${REPO} | sed "s|file://||" )"
        cp ${base}/$1 .
    else
        wget ${REPO}/$1
    fi
}

rm -f *.pkg.tar.gz
rm -rf db
mkdir db
cd db
fetch larch.db.tar.gz
tar -xzf larch.db.tar.gz
cd ${scriptdir}
d=$( ls db | grep "^larch-8" )
larchpak=$( grep -A 1 -e "%FILENAME%" db/${d}/desc | grep -v "%" )
fetch ${larchpak}
d=$( ls db | grep "^liblarch-" )
larchpak=$( grep -A 1 -e "%FILENAME%" db/${d}/desc | grep -v "%" )
fetch ${larchpak}

rm -rf tmp
mkdir tmp
tar -xzf larch-8*.pkg.tar.gz -C tmp

if [ "$1" != "-p" ]; then
    d=$( ls db | grep "^larch-profiles-" )
    larchpak=$( grep -A 1 -e "%FILENAME%" db/${d}/desc | grep -v "%" )
    fetch ${larchpak}
    tar -xzf larch-profiles-*.pkg.tar.gz -C tmp
fi

rm -rf db
mv tmp/opt/apps/larch larch0
tar -xzf liblarch-*.pkg.tar.gz -C tmp
mv tmp/opt/apps/liblarch .

for s in $( ls tmp/usr/bin ); do
    p=$( readlink tmp/usr/bin/${s} )
    d=$( dirname ${p} )
    ln -s larch0/$( basename ${d} )/$( basename ${p}) ${s}
done
rm -rf tmp

# Check that pacman is available.
if ! which pacman &>/dev/null; then
    # Fetch the pacman package from the larch site
    fetch pacman-allin.tar.gz
    if ! [ -f pacman-allin.tar.gz ]; then
        echo "ERROR: couldn't fetch pacman package"
        exit 1
    fi
   # Extract the package
    tar -xzf pacman-allin.tar.gz
    cp -a pacman-allin/* larch0
    rm -rf pacman-allin
fi