#!/usr/local/bin/bash

prefix=/usr/local
exec_prefix=${prefix}
bindir=${exec_prefix}/bin

if [ "$1" == "" ]; then
	echo Usage $0 -c command -f filename -a send_as_nick -h help_text -m flags
	exit 1
fi;

while [ "$1" != "" ]; do
	case "$1" in
		-c)
			shift
			command=$1
			echo command is $command
		;; 
		-f)
			shift
			filename=$1
			echo filename is $filename
		;; 
		-a)
			shift
			sendas=$1
			echo sendas-nick is $sendas
		;; 
		-h)
			shift
			descr=$1
			echo help description is $descr
		;;
		-m)
			shift
			flags=$1
			echo flags are $flags
		;; 
		*)
			echo unknown parameter $1
		;;
	esac;
	shift;
done;

if [ "$command" == "" ]; then
	echo Specify command with -c command
	exit 1
fi;

if [ `echo $command|sed -e "s/^\(.\).*/\1/"` != "+" ]; then
	echo "Warning the command does not start with the + sign"
fi;

fields="command"
values="'$command'"

function AddField
{
	if [ "$2" != "" ]; then
		fields="$fields,$1"
		values="$values,$2"
	fi;
}

AddField "send_as" $sendas
#if [ "$sendas" != "" ]; then
#	fields="$fields,send_as"
#	values="$values,$sendas"
#fi;

if [ "$descr" != "" ]; then
	fields="$fields,descr"
	values="$values,'$descr'"
fi;

if [ "$filename" != "" ]; then
	fields="$fields,def"
	values="$values,'$filename'"
fi;

if [ "$flags" != "" ]; then
	fields="$fields,flags"
	values="$values,'$flags'"
fi;

query="INSERT INTO file_trigger ($fields) VALUES ($values)";

$bindir/vh_getdb --query "$query"

