#!/bin/sh
LPM_BASE="/var/lib/lpm" export LPM_BASE
LPM_CONFDIR="/etc" export LPM_CONFDIR
LPM_CONF="/etc/lpm.conf" export LPM_CONF
LPM_BINDIR="/usr/bin" export LPM_BINDIR
LPM_VERSION="1.0.22"
ELPM_VERSION="1.0"
LPM_ARCH="`uname -m`"
lpmlog()
{
	msg="$1"
	logger -t lpm-adm -p info "$msg"
}

lpm_dellock()
{
	plock="$LPM_BASE/lock"
	rm -f "$plock"
}

lpm_getlock()
{
	plock="$LPM_BASE/lock"
	if [ -f "$plock" ]
	then
		return 1
	fi
	touch "$plock"
	if [ -f "$plock" ]
	then
		return 0
	else
		return 2
	fi
}

psname()
{
	iname="$1"
	pbase="$LPM_BASE/installed/$iname"
	if [ ! -d "$pbase" ]
	then
		echo "lpm: Package $iname is not installed" >&2
		return 2
	fi
	pdesc="$pbase/desc.txt"
	pldif="`{ printf "dn: dummy\n";cat "$pdesc";}`"
	pname="`echo "$pldif"|lpm-ldifget -a elpkName`"
	pvariant="`echo "$pldif"|lpm-ldifget -a elpkVariant 2>/dev/null`"
	pvers="`echo "$pldif"|lpm-ldifget -a elpkVersion`"
	if [ "x$pvariant" = "x" ]
	then
		printf "%s-%s\n" "$pname" "$pvers"
	else
		printf "%s-%s-%s\n" "$pname" "$pvariant" "$pvers"
	fi
	return 0
}

puname()
{
	pldif="$1"
	pname="`echo "$pldif"|lpm-ldifget -f -a elpkName`"
	parch="`echo "$pldif"|lpm-ldifget -f -a elpkArchitecture`"
	pvariant="`echo "$pldif"|lpm-ldifget -f -a elpkVariant 2>/dev/null`"
	pvers="`echo "$pldif"|lpm-ldifget -f -a elpkVersion`"
	if [ "x$pvariant" = "x" ]
	then
		printf "%s_%s_%s\n" "$pname" "$parch" "$pvers"
	else
		printf "%s_%s_%s_%s\n" "$pname" "$pvariant" "$parch" "$pvers"
	fi
	return 0
}

boot()
{
	if [ -d "$LPM_BASE" ]
	then
		echo "Repository $LPM_BASE exists, refusing" >&2
		return 1
	fi
	lpmlog "initializing lpm inventory"
	lpmlog "creating initial config file"
	mkdir -p $LPM_CONFDIR
echo "dist_name		= KBL" >  $LPM_CONF
echo "dist_vers		= 1.0" >> $LPM_CONF
echo "lpm_vers		= 1.0" >> $LPM_CONF
echo "arch_name		= $LPM_ARCH" >> $LPM_CONF

	bb_full="`which busybox`"
	bb_path="`dirname "$bb_full"`"
	bb_vers="`busybox  |head -1 |cut -f2 -d ' '|sed -e 's/^v//g'`"
	if [ "$verbose" = "1" ]
	then
		printf "Creating repository structure..."
	fi
	lpmlog "creating directory structure"
	mkdir -p $LPM_BASE
	mkdir -p $LPM_BASE/installed
	mkdir -p $LPM_BASE/stagearea
	mkdir -p $LPM_BASE/certstore
	mkdir -p $LPM_BASE/fileindex
	mkdir -p $LPM_BASE/scriptdir
	if [ "$verbose" = "1" ]
	then
		printf "done\n"
	fi
	if [ "$verbose" = "1" ]
	then
		printf "Registering lpm in  repository..."
	fi
	kronos_files="/kronos/lpm /kronos/lpm-adm /kronos/lpm-key /kronos/busybox /kronos/openssl  /kronos/lpm-ldifget /kronos/lpm-ldifput"
	data_size="`wc -c $kronos_files |tail -1|xargs echo |cut -f1 -d ' '`"
	lpmlog "registering kronos in lpm"
	mkdir -p $LPM_BASE/installed/kronos
	{
		lpm-ldifput -a elpmVersion -s "$ELPM_VERSION"
		lpm-ldifput -a elpkName -s "kronos"
		lpm-ldifput -a elpkVersion -s "$LPM_VERSION"
		lpm-ldifput -a elpkCreator -s "Tim Tassonis <kronos@laconian-linux.org>"
		lpm-ldifput -a elpkArchitecture -s "x86_64"
		lpm-ldifput -a elpkDescription -s "easy lakedonian package manager"
		lpm-ldifput -a elpkDataSize -s "$data_size"
	}  > $LPM_BASE/installed/kronos/desc.txt
	mkdir $LPM_BASE/installed/kronos/hash
	for file in $kronos_files
	do
		hash="`sha1sum < $file |cut -f1 -d ' '`" 
		printf "%s:%s\n" "$hash" "$file" >> $LPM_BASE/installed/kronos/hash/data.txt
	done
	for file in $kronos_files
	do
		printf "%s\n" "$file" >> $LPM_BASE/fileindex/kronos
	done
	lpm-ldifput -a elpkInstallTime -s "`date -u '+%s'`" > $LPM_BASE/installed/kronos/inst.txt
	lpm-ldifput -a elpkInstallType -s "X" >> $LPM_BASE/installed/kronos/inst.txt
	

	pldif="`cat $LPM_BASE/installed/kronos/desc.txt`"
	lpmlog "installed package `puname "$pldif"`"
	if [ "$verbose" = "1" ]
	then
		printf "done.\n"
	fi
	rm -rf $LPM_BASE/scriptdir/*
	/kronos/busybox --install -s $LPM_BASE/scriptdir/
	return 0
}
	



usage()
{
    echo "$0 : laconian package manager administration"
    echo "Usage: "
    echo "$OPTUSG"
    return
}

OPTSTR="BLQ:ovVh"
OPTUSG="
    -h            hlp
    -v            verbose
    -V            version
    -B            bootstrap the inventory
    -L            list current settings
    -Q key        query one setting
    -o options    options for package operations, enter -o hlp for details
"
boot=0
list=0
show=0
verbose=0
options=""

while getopts "$OPTSTR" opt
do
    case "$opt" in
        h) usage $progname; exit 0;;
        B) boot=1 ;;
        L) list=1 ;;
        Q) key="$OPTARG"; show=1 ;;
		V) echo "lpm-adm $LPM_VERSION"; exit 0;;
        v) verbose=1 ;;
        o) options="$OPTARG" ;;
        *) usage; exit 1;;
    esac
done
if [ "x$OPTIND" != "x" ] && [ "$OPTIND" != "0" ]
then
	CURR_OPT="1"
	while ( [ "$CURR_OPT" != "$OPTIND" ] )
	do
		shift
		CURR_OPT="`expr $CURR_OPT + 1`"
	done
fi
if [ "$boot" = "1" ]
then
	boot
	exit $?
fi

if [ "$list" = "1" ]
then
	cat $LPM_CONF
	exit $?
fi

if [ "$show" = "1" ]
then
	if grep "^$key[ =	]" $LPM_CONF >/dev/null
	then
		grep "^$key" $LPM_CONF |xargs echo |cut -f3- -d ' '
		exit $?
	else
		echo "$key not found" >&2
		exit 1
	fi
fi

if [ "$verbose" = "1" ]
then
        verbarg="-v"
else
        verbarg=""
fi

