# -*-shell-script-*-

# Database support routines

###
#
# This code is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Copyright 2002 Mike Hearn (mike@theoretic.com)
#
###


# ensures we have a root name. If $1 is a simple name, then
# we translate it by checking first in the local package dbs,
# then if not there we can go on to check on the autopackage network (to be done)
# echos the result - remember you can't echo debug statements here (do it to a file instead)

# $1 = name to resolve
function resolveName() {
    a="$1"
    trace "resolving $1"
    # try the user db first
    escaped_info_dir=`escapeValue $autopackage_user_db_location`
    while true; do
	fileinfo=`file -b $autopackage_user_db_location/$a`
	if [ ! -e $autopackage_user_db_location/$a ]; then
	    # we don't appear to have a reference to it in the user db
	    trace "no reference in user db"
	    break;
	elif [[ "$fileinfo" != "directory" ]]; then
	    if [[ `echo "$fileinfo" | cut -d" " -f 1` == "symbolic" ]]; then
		a=`echo "$fileinfo" | cut -d" " -f 4 | sed "s/$escaped_info_dir\///"`;
		# loop around, tracking the symlinks as we go
	    else
		# we've encountered something, but we don't know what it is :(
		warn "unknown database entry ($fileinfo) in user db"
		return 1;
	    fi
	else
	    echo "$a";
	    return 0;
	fi
    done


    # now attempt to locate in local system db
    trace "searching local system db"  
    escaped_info_dir=`escapeValue $autopackage_global_db_location`
    while true; do
	fileinfo=`file -b $autopackage_global_db_location/$a` # get what the file is
	if [[ ! -e $autopackage_global_db_location/$a ]]; then
	    # we don't appear to have a reference to it in the db
	    break;
	elif [[ "$fileinfo" != "directory" ]]; then
	    if [[ `echo "$fileinfo" | cut -d" " -f 1` == "symbolic" ]]; then
		a=`echo "$fileinfo" | cut -d" " -f 4 | sed "s/$escaped_info_dir\///"`;
		# loop around, tracking the symlinks as we go
	    else
		# we've encountered something, but we don't know what it is :(
		warn "unknown database entry ($fileinfo) in system db"
		return 1;
	    fi
	else
	    echo "$a";
	    return 0;
	fi
    done

    # now check the autopackage network
    # FIXME: write this :)
    warn FIXME
    return 2;
}

# create package info file and db entry
# $1 = rootname of package
function createDBEntry() {
    # tidy this up - why bother splitting the specfile?
    outputStatus "$intl_FUNCLIB_UPDATING_DB"
    _mkdirs "$autopackage_db_location/$1" >/dev/null;
    local inf="$autopackage_db_location/$1/info";
    touch "$inf";
    echo "[Meta]" > "$inf"
    cat "$meta_dir/apkg-meta" >> "$inf"
    echo >> "$inf"
    echo "[Description]" >> "$inf";
    cat "$meta_dir/apkg-description" >> "$inf";
    echo >> "$inf";
    if [ -e "$apkg_logfile" ]; then
	echo "[Log]" >> "$inf";
	cat "$apkg_logfile" | tac >> "$inf";
	echo >> "$inf";
    fi

    echo "[Script-Uninstall]" >> "$inf";
    cat "$meta_dir/apkg-uninstall" >> "$inf";

    # make symlinks
    rm -f "$autopackage_db_location/${SHORTNAME}-${SOFTWAREVERSION}" 2>/dev/null; # remove if there to avoid creating recursive symlinks
    rm -f "$autopackage_db_location/${SHORTNAME}" 2>/dev/null;
    ln -s -f "$autopackage_db_location/$1" "$autopackage_db_location/${SHORTNAME}-${SOFTWAREVERSION}"
    ln -s -f "$autopackage_db_location/${SHORTNAME}-${SOFTWAREVERSION}" "$autopackage_db_location/${SHORTNAME}"

    # make prefixes file
    echo "prefix=$prefix" > "$autopackage_db_location/$1/prefixes"

    # write the file list
    if [ -e "$apkg_filelist" ]; then
	sed 's|//|/|g' < "$apkg_filelist" > "$autopackage_db_location/$1/files"
	rm -f "$apkg_filelist"
    fi
}


# Figures out which database to use (global or user) and sets the autopackage_db_location variable
function initDB() {
  if ! haveWriteAccess "$autopackage_global_db_location"; then
    if [[ "$autopackage_user_db_prefix" != "" ]]; then
      export autopackage_db_location=`eval echo $autopackage_user_db_location`
    else
      # default if none is set in config file
      export autopackage_db_location="$HOME/.packages" 
    fi
  else
    export autopackage_db_location="$autopackage_global_db_location"
  fi
}

# Is the given root name in the database?
function isNameKnown() {
    if [ -e "$autopackage_global_db_location/$1/info" ] || [ -e "$autopackage_user_db_location/$1/info" ]; then
	return 0;
    fi
    return 1;
}
