#!/bin/bash

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

. setenv 
if [ "$VERLIHUB_DB_DATA" != "" ]; then
	dbdata=$VERLIHUB_DB_DATA
else
	dbdata=verlihub
fi
if [ "$VERLIHUB_DB_PASS" != "" ]; then
	dbpass=$VERLIHUB_DB_PASS
else
	dbpass=verlihub
fi
if [ "$VERLIHUB_DB_USER" != "" ]; then
	dbuser=$VERLIHUB_DB_USER
else
	dbuser=verlihub
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
		;; 
		*)
			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'"

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;

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

dbcmd="mysql -u $dbuser -D $dbdata -p$dbpass -e \"$query\""
echo Use following command
echo $dbcmd

