#!/bin/bash

MAINHELP="\

#===============================================================================
# db_manipulator for TRECVID 2010 SIN task Evaluation
#
# usage described below! (at each switch)
#===============================================================================
"

CURRENT_DIR=`dirname \`readlink -f $0\``
export LD_LIBRARY_PATH=$CURRENT_DIR/ut
PL=$CURRENT_DIR/ut/psql_
JOIN=$CURRENT_DIR/ut/join.pl
ANNTOOL=$CURRENT_DIR/ut/ann_corr.pl
CONN="-h athena2.fit.vutbr.cz -p 5432 -d trecvid -U trecvid -v sslmode=require  -t -A";
SCHEMA="sin_2010";

case $1
in


#====================================================================================
    listcl)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip listcl
 results:
  list of classifiers currently stored in database
	";
  
	exit;
    fi

    echo "select name, description from $SCHEMA.classifier" | $PL $CONN
    ;;
    
#====================================================================================
    delcl)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip delcl <classifier_name>
 results:
  deletes the classifier, all its instances and results from the DB
	";
  
	exit;
    fi
    
    CL_NAME=$2
    
    if [ -z "$CL_NAME" ]
    then
	echo "Classifier name missing. For more info use: db_manip command help"
	exit;
    fi

    #does the classifier exist
    if [[ $(echo "SELECT COUNT(*) from $SCHEMA.classifier WHERE classifier.name = '$CL_NAME'" | $PL $CONN) != 1 ]]
    then
	echo "The classifier '$CL_NAME' does not exist."
	exit;
    fi

    #get number of cl instances and results
    CL_INS=$( echo "SELECT COUNT(*) from $SCHEMA.classifier, $SCHEMA.classifier_storage WHERE classifier.name = '$CL_NAME' AND classifier_storage.id_classifier = classifier.id" | $PL $CONN)
    CL_RES=$( echo "SELECT COUNT(*) from $SCHEMA.classifier, $SCHEMA.classifier_storage, $SCHEMA.classifier_results \
		 WHERE classifier.name = '$CL_NAME' AND classifier_storage.id_classifier = classifier.id AND classifier_results.id_classifier_storage = classifier_storage.id" | $PL $CONN)

    if [[ $CL_INS != 0 ]]
    then
	a=''
	while [[ !( ( "$a" = y) || ( "$a" = n ) ) ]];
	do
    	    echo "The classifier '$CL_NAME' has $CL_INS instances and $CL_RES results. Do you want to delete them all? y[es]/n[o]: "
    	    read a
	done
	if [ $a = n ]; then exit; fi
    fi
    
    #delete all
    echo "DELETE FROM $SCHEMA.classifier_results \
		 WHERE classifier_results.id_classifier_storage IN (SELECT classifier_storage.id from $SCHEMA.classifier, $SCHEMA.classifier_storage WHERE classifier.name = '$CL_NAME' AND classifier_storage.id_classifier = classifier.id)" | $PL $CONN
    echo "DELETE from $SCHEMA.classifier_storage WHERE classifier_storage.id_classifier IN (SELECT id from $SCHEMA.classifier WHERE classifier.name = '$CL_NAME')" | $PL $CONN		 
    echo "DELETE from $SCHEMA.classifier WHERE classifier.name = '$CL_NAME'" | $PL $CONN
    ;;
    
    
#====================================================================================
    inscl)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip inscl classifierName description
 results:
  add new classifier into database
	";
  
	exit;
    fi

    if [[ -z "$2" ]]
    then
	echo "Missing classifier name. For more information use: $0 command help"
	exit;
    fi

    echo "INSERT INTO $SCHEMA.classifier ( name, description) VALUES ( '$2', '$3')"  | $PL $CONN
    ;;

#====================================================================================
    listclclass)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip listclclass classname
 results:
  list all classifier for given class
  ";
  
	exit;
    fi

    if [[ -z "$2" ]]
    then
	echo "Missing class name. For more information use: $0 command help"
	exit;
    fi

    echo "SELECT classifier.name, bbox.name \
	  FROM $SCHEMA.classifier, $SCHEMA.class, $SCHEMA.classifier_storage, $SCHEMA.bbox \
	  WHERE classifier.id = classifier_storage.id_classifier \
	  AND classifier_storage.id_class = class.id \
	  AND class.name = '$2' \
	  AND bbox.id = classifier_storage.id_bbox" | $PL $CONN
    ;;
    
#====================================================================================
    insclinst)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip insclinst classifiername classname bboxname classifierfile logfile  
 results:
  Insert trained classifier for certain class, black box and dataset.
  logname is optional name of text log file which was created with the classifier
  The input files must not contain \'.
  ";
  
	exit;
    fi

    if [[ -z "$5" ]]
    then
	echo "Missing classifier file name. For more information use: $0 command help"
	exit;
    fi
    
    
    echo "INSERT INTO $SCHEMA.classifier_storage ( id_classifier, id_class, id_bbox, classifier, log) \
    VALUES( (SELECT id FROM $SCHEMA.classifier WHERE name = '$2'), (SELECT id FROM $SCHEMA.class WHERE name='$3'), \
    (SELECT id FROM $SCHEMA.bbox WHERE name = '$4'), " > db_insert.temp
    printf "'" >> db_insert.temp
    cat "$5" >> db_insert.temp
    printf "', '" >> db_insert.temp
    if [ -f "$6" ]; then cat "$6" >> db_insert.temp; fi
    echo "')" >> db_insert.temp
    $PL $CONN < db_insert.temp
    
    rm -f db_insert.temp
    ;;
    
#====================================================================================
    insclres)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip insclres classifiername classname bboxname datasetname < classifier.results  
 results:
  Insert classifier results into the database. The results are for single class, classifier, black box and dataset.
  The format is textual with numbers separated by whitespace.
  ";
  
	exit;
    fi

    if [[ -z "$5" ]]
    then
	echo "Missing dataset name. For more information use: $0 command help"
	exit;
    fi


    #read the responses from stdin to array - each number in separate index
    read -d $'\0' -a res 
    str=$( echo ${res[*]/#/,} | sed 's/,//' )
    
    echo "INSERT INTO $SCHEMA.classifier_results ( id_classifier_storage, id_dataset, response) VALUES ( \
    (SELECT classifier_storage.id \
    FROM $SCHEMA.classifier_storage, $SCHEMA.classifier, $SCHEMA.class, $SCHEMA.bbox \
    WHERE \
	classifier_storage.id_classifier = classifier.id AND classifier.name = '$2' \
    AND classifier_storage.id_class = class.id AND class.name = '$3' \
    AND classifier_storage.id_bbox = bbox.id AND bbox.name = '$4'), \
    (SELECT id FROM $SCHEMA.dataset WHERE dataset.name = '$5'), \
    '{$str}') "| $PL $CONN
    ;;
    
#====================================================================================
    clres)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip clres classifiername bboxname datasetname class1 [class2 class3 ...]
 results:
  Retrieves classifier results from the database. The results are for single classifier, black box and dataset.
  The format is textual with numbers separated by whitespace.
  ";
  
	exit;
    fi
    
    CL_NAME=$2
    BB_NAME=$3
    DS_NAME=$4

    if [[ -z "$5" ]]
    then
	echo "Missing class name. For more information use: $0 command help"
	exit;
    fi

    while [ "$5" ]
    do 
	echo "\
        SELECT classifier_results.response \
	FROM $SCHEMA.classifier_results, $SCHEMA.classifier_storage, $SCHEMA.classifier, $SCHEMA.class, $SCHEMA.bbox, $SCHEMA.dataset \
        WHERE \
	    classifier_storage.id_classifier = classifier.id AND classifier.name = '$CL_NAME' \
        AND classifier_storage.id_class = class.id AND class.name = '${5}' \
	AND classifier_storage.id_bbox = bbox.id AND bbox.name = '$BB_NAME' \
        AND classifier_results.id_dataset = dataset.id AND dataset.name = '$DS_NAME' \
        AND classifier_results.id_classifier_storage = classifier_storage.id;"
	shift 1
    done | $PL $CONN | sed 's/{//;s/}//;s/,/ /g' 
    ;;
    

#====================================================================================
    cl)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip cl classifiername bboxname class1
 results:
  Retrieves classifier from the database. 
  ";
  
	exit;
    fi
    
    CL_NAME=$2
    BB_NAME=$3
    CLASS_NAME=$4

    if [[ -z "$4" ]]
    then
	echo "Missing class name. For more information use: $0 command help"
	exit;
    fi

    echo "\
    SELECT classifier_storage.classifier \
    FROM  $SCHEMA.classifier_storage, $SCHEMA.classifier, $SCHEMA.class, $SCHEMA.bbox \
    WHERE \
        classifier_storage.id_classifier = classifier.id AND classifier.name = '$CL_NAME' \
    AND classifier_storage.id_class = class.id AND class.name = '${CLASS_NAME}' \
    AND classifier_storage.id_bbox = bbox.id AND bbox.name = '$BB_NAME' \
    "| $PL $CONN
    ;;



#====================================================================================
    listds)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip listds
 results:
  list of dataset currently stored in database
	";
  
	exit;
    fi

    echo "select name from $SCHEMA.dataset" | $PL $CONN
    ;;
    

#====================================================================================
    listbb)
    if [ "aa${2}bb" == "aahelpbb" ] ; then
	echo "$MAINHELP
 usage:
  db_manip listbb
 results:
   list of blackboxes currently stored in database
    ";
	exit;
    fi

    echo "select name, path from $SCHEMA.bbox" | $PL $CONN | sed 's/|/ /'
    ;;
    

#====================================================================================
    dscont)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip dscont <dataset_name>
 results:
  proper sorted list of files in dataset
    ";
	exit;
    fi

    if [ -z "$2" ]; then echo "Missing dataset. For more info use: db_handler command help"; exit; fi
    
    echo "select f.filename from $SCHEMA.files f , $SCHEMA.dataset d where f.id_dataset = d.id and d.name='$2' order by f.id" | $PL $CONN   
    ;;


#====================================================================================
    dscontex)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip dscontex <dataset_name>
 results:
  proper sorted list of files plus additional information  in dataset

    ";
	exit;
    fi
    
    if [ -z "$2" ]; then echo "Missing dataset. For more info use: db_handler command help"; exit; fi
    
    echo "select f.filename, f.id, f.original_filename, f.vpl_id from $SCHEMA.files f , $SCHEMA.dataset d where f.id_dataset = d.id and d.name='$2' order by f.id" | $PL $CONN | sed "s/|/\ /g"
    ;;


#====================================================================================
    classann)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip classann  <dataset_name> <class_name>
 results:
  - Vector of real numbers corresponding to class ocupancy for each file in the dataset.
  - If either the dataset or the class does not exist, the output is empty.
    ";
	exit;
    fi

    DATASET="$2"
    CLASS="$3"
    
    if [[ ( -z "$DATASET" ) || ( -z "$CLASS" ) ]]; then
	echo "Error: Dataset and class names must be specified. For more info: db_manip command help" >&2
	exit
    fi

    echo "SELECT ann.data \
    FROM $SCHEMA.ann, $SCHEMA.class, $SCHEMA.dataset \
    WHERE ann.id_dataset = dataset.id \
    AND   ann.id_class   = class.id \
    AND   class.name     = '$CLASS' \
    AND   dataset.name   = '$DATASET' \
    " | $PL $CONN | sed 's/[{}]//g' | sed 's/,/ /g'
    
    ;;

#====================================================================================
    dsclass)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip classann  <dataset_name>
 results:
  - returns all classes for which are annotations available for the dataset
  - If the dataset does not exist, the output is empty.
    ";
	exit;
    fi

    DATASET="$2"
    
    if [[ ( -z "$DATASET" )]]; then
	echo "Error: Dataset names must be specified. For more info: db_manip command help" >&2
	exit
    fi

    echo "SELECT class.name \
    FROM $SCHEMA.class, $SCHEMA.dataset, $SCHEMA.ann \
    WHERE ann.id_dataset = dataset.id \
    AND   ann.id_class   = class.id \
    AND   dataset.name   = '$DATASET' \
    ORDER BY class.id \
    " | $PL $CONN 
    
    ;;

#====================================================================================
    ann)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip ann <dataset_name>
 results:
  matrix with annotations for the dataset 
  Each line corresponds to single file from the dataset.
  Each column corresponds to single class defined for the dataset.
     ";
	exit;
    fi
    
    DATASET="$2"
    if [[ -z "$DATASET" ]]; then
	echo "Error: Dataset name must be specified. For more info: db_manip command help" >&2
	exit
    fi

    echo " \
    SELECT ann.data \
    FROM \
	$SCHEMA.ann, \
	$SCHEMA.dataset \
    WHERE \
	ann.id_dataset = dataset.id \
	AND dataset.name = '$DATASET' \
    ORDER BY ann.id_class ASC \
    " | $PL $CONN | sed 's/[{}]//g' | gawk '
    BEGIN{ FS=","}
    { for ( f = 1; f <= NF; f++) a[ NR, f] = $f }
    NF > nf { nf = NF }
    END{
	ORS=""; FS=" "
	for( f = 1; f <= nf; f++)
	    for( r = 1; r <= NR; r++)
		print a[ r, f] ( r==NR ? RS : FS)
    }'
    
    

    # transposition is missing    
	
    ;;


#====================================================================================
    insbb)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip insbb <bbox_name> <path>
   - bbox_name have to be unique across whole system
   - path where bbox could be found

 results:
  OK | ERROR
    ";
	exit;
    fi
    
    BBOXNAME=$2;
    PATH_=$3;

    echo "INSERT INTO $SCHEMA.bbox (name, path) VALUES ( '$BBOXNAME', '$PATH_')" | $PL $CONN    

    ;;

#====================================================================================
    insdesc)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip insdesc <dataset> <bbox_name> <path>
   - dataset - name of dataset
   - bbox_name
   - path - absolute path and file name of the descriptor

 results:
  OK | ERROR
	#";
	exit;
    fi

    DATASET=$2
    BBOXNAME=$3;
    PATH_=$4;
    
    if [[ -z "$PATH" ]]; then
	echo "Dataset name, black box name and path must be specified. For more info use: db_handler command help" >&2
	exit
    fi
    
    DS_ID=$( echo "SELECT dataset.id FROM $SCHEMA.dataset WHERE name='$DATASET'" | $PL $CONN)
    
    if [ -z "$DS_ID" ]; then 
	echo "ERROR: Dataset '${DATASET}' does not exist" >&2;
	exit
    fi

    BB_ID=$( echo "SELECT bbox.id FROM $SCHEMA.bbox WHERE name='$BBOXNAME'" | $PL $CONN)
    
    if [ -z "$BB_ID" ]; then
	echo "ERROR: Black box '${BBOXNAME}' does not exist" >&2;
	exit;    
    fi
    
    DESC_ID=$( echo "SELECT id FROM $SCHEMA.descriptors WHERE id_dataset=$DS_ID AND id_bbox=$BB_ID" | $PL $CONN)
    if [ -n "$DESC_ID" ]; then
	echo "ERROR: Descriptor already present." >&2
	exit;    
    fi
    
    echo "INSERT INTO $SCHEMA.descriptors (id_dataset,id_bbox,path) VALUES( $DS_ID, $BB_ID, '$PATH_')" | $PL $CONN

    ;;


#====================================================================================
    deldesc)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip deldesc <dataset> <bbox_name>
   - dataset - name of dataset
   - bbox_name

 results:
  Deletes descriptor for given dataset and black box
    ";
	exit;
    fi

    DATASET=$2
    BBOXNAME=$3;
    
    if [[ -z "$DATASET" || -z "$BBOXNAME" ]]; then
	echo "ERROR: deldesc needs dataset and bbox_name" >&2
	exit
    fi

    echo "delete FROM $SCHEMA.descriptors WHERE descriptors.id_bbox = ( select id from $SCHEMA.bbox where name='$BBOXNAME') AND descriptors.id_dataset= ( select id from $SCHEMA.dataset WHERE name='$DATASET')" | $PL $CONN
    
    ;;


#====================================================================================
    delbb)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip deldesc <bbox_name>
   - bbox_name

 results:
  Deletes specified black box and all descriptor files corresponding to this black box.
    ";
	exit;
    fi

    BBOXNAME=$2;
    
    if [[ -z "$BBOXNAME" ]]; then
	echo "ERROR: deldesc needs dataset and bbox_name" >&2
	exit
    fi
    
    
    BBOXES=$( echo "SELECT name FROM  $SCHEMA.bbox WHERE bbox.name LIKE '$BBOXNAME'" | $PL $CONN )
    BBOX_COUNT=$( echo $BBOXES | wc -w)

    if [[ $BBOX_COUNT > 0 ]]; then 

        if [[ ( $BBOX_COUNT > 1 ) && ! "$FOCE_DELETE" ]]; then 
	    echo "Matching black boxes:" 
    	    echo "$BBOXES" |sed "s/ /\n/g"
    	    echo 
	    a=''
	    while [[ !( ( "$a" = y) || ( "$a" = n ) ) ]];
	    do
    		echo "Do you want to remove all these black boxes? y[es]/n[o]: "
    		read a
	    done
	    if [ $a = n ]; then exit; fi
        fi
    
        DESCRIPTORS=$( echo "SELECT count(*) FROM $SCHEMA.bbox, $SCHEMA.descriptors WHERE bbox.id = descriptors.id_bbox AND bbox.name LIKE '$BBOXNAME'" | $PL $CONN)
        
        if [[ ( $DESCRIPTORS > 0 ) && !( "$FORCE_DELETE" ) ]]; then
	    a=''
	    while [[ !( ( "$a" = y) || ( "$a" = n ) ) ]]
	    do
    		echo "The matching black box(es) have $DESCRIPTORS descriptor files associated to them. Delete all? y[es]/n[o]: "
    		read a
	    done
	    if [ $a = n ]; then exit; fi
        fi
        

	# remove descriptors        
        if [[ $DESCRIPTORS > 0 ]]; then
	    echo "DELETE FROM sin_2010.descriptors WHERE descriptors.id_bbox IN ( SELECT id FROM sin_2010.bbox WHERE name LIKE '$BBOXNAME')" | $PL $CONN
	fi
	
	# remove bboxes
	echo "DELETE FROM $SCHEMA.bbox WHERE bbox.name LIKE '$BBOXNAME'" | $PL $CONN
    fi
    
    ;;


#====================================================================================
    listdsbb)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip listdsbb <dataset>
   - dataset - name of dataset
 results:
  list of blackboxes which have some results for given dataset
    ";
	exit;
    fi

    DATASET=$2
    
    if [ -z "$DATASET" ]; then
	echo "Dataset name must be specified. For more info use: db_handler command help" >&2
	exit
    fi
        
    echo "SELECT bbox.name, bbox.path \
	FROM $SCHEMA.descriptors, $SCHEMA.bbox, $SCHEMA.dataset \
	WHERE bbox.id = descriptors.id_bbox \
	AND descriptors.id_dataset= dataset.id \
	AND dataset.name='$DATASET'\
	ORDER BY descriptors.id" | $PL $CONN | sed "s/|/\ /g"
    
    ;;
    
    
#====================================================================================
    listdsdesc)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip listdsdesc <dataset>
   - dataset - name of dataset
 results:
  list of descriptors for particular dataset:
    dataset_name bbox_name descriptor_path
    dataset_name bbox_name descriptor_path
    ...
    ";
	exit;
    fi

    DATASET=$2
    
    if [ -z "$DATASET" ]; then
	echo "Dataset name must be specified. For more info use: db_handler command help" >&2
	exit
    fi
        
    echo "SELECT dataset.name, bbox.name, descriptors.path \
	FROM $SCHEMA.descriptors, $SCHEMA.dataset, $SCHEMA.bbox \
	WHERE descriptors.id_dataset=dataset.id \
	AND descriptors.id_bbox=bbox.id \
	AND dataset.name='$DATASET' \
	ORDER BY descriptors.id" | $PL $CONN | sed "s/|/\ /g"
    ;;

#====================================================================================
    listdsseldesc)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip listdsseldesc <dataset> <bbox>
   - dataset - name of dataset
 results:
  list of descriptors for particular dataset and black box:
    dataset_name bbox_name descriptor_path
    dataset_name bbox_name descriptor_path
    ...
	";
	exit;
    fi

    DATASET=$2
    BBOX=$3
    if [ -z "$BBOX" ]; then
	echo "Dataset name and black box name must be specified. For more info use: db_handler command help" >&2
	exit
    fi
    
    echo "SELECT dataset.name, bbox.name, descriptors.path \
	FROM $SCHEMA.descriptors, $SCHEMA.bbox, $SCHEMA.dataset \
	WHERE bbox.id = descriptors.id_bbox \
	AND descriptors.id_dataset = dataset.id \
	AND dataset.name='$DATASET'\
	AND bbox.name='$BBOX' " | $PL $CONN | sed "s/|/\ /g"
    
    ;;


#====================================================================================
    insann)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip insann <dataset> <annfile> <classfile>
   - dataset - name of dataset
   - annfile - file contains all annotations
   - classfile - file with description of classes
   
 Format of the a class file:
   class1_name [description string]
   class2_name [description string]
   ...
   
    ";
	exit;
    fi

    DATASET=$2
    ANNFILE=$3
    CLFILE=$4
    if [[ ( -z "$DATASET" ) || !( -f "$ANNFILE") || !( -f "$CLFILE") ]]; then
	echo "Error: Dataset name and annoation file must be specified. For more info use: db_handler command help" >&2
	exit
    fi
    
    # check if the dataset is present    
    DS_ID=$( echo "SELECT dataset.id FROM $SCHEMA.dataset WHERE name = '$DATASET' " | $PL $CONN)
    if [ -z "$DS_ID" ]; then 
	echo "ERROR: The dataset '$DATASET' does not exist." >&2
	exit
    fi
    
    declare -a CLASSES

    shopt -s extglob #to be able to use extended patterns in matching

    # add classes    
    if [ -f "$CLFILE" ]; then #if the file exists
	while read line
	do
	    line=$(sed "s/'/ /g" <<< ${line})
	    CLASSES[ ${#CLASSES[*]}]=${line%%+([[:space:]])*}
	    echo "INSERT INTO $SCHEMA.class ( name, description) VALUES ( '${line%%+([[:space:]])*}', '${line##*([[:graph:]])+([[:space:]])}');" | $PL $CONN
	done < $CLFILE
    fi
    
    #get the number of classes from the first line of the dataset file
    CLASS_COUNT=$( head -n 1 $DSFILE | wc -w) 
    
    #check if all classes have names
    if [[ ${#CLASSES[*]} < $CLASS_COUNT ]]; then
	echo "ERROR: Not all classes were defined in '$ANNFILE'" >&2
	exit;
    fi
    
    #add annotations into DB
    echo Adding annoations
    echo Number of added class annotations $({
	for (( i = 0; i < $CLASS_COUNT; i++ ))
	do
	    temp='{'$( cut -f $(( $i + 1 )) -d ' ' $DSFILE | gawk '{ORS=""; print "," $0}' | sed 's/,//' )'}'  # get column, make a line from it and put ',' there to separate the values
    	    echo "INSERT INTO $SCHEMA.ann ( id_dataset, id_class, data) VALUES ( $DS_ID, (SELECT class.id FROM $SCHEMA.class WHERE class.name = '${CLASSES[$i]}'), '$temp');"
	done
    } |$PL $CONN | wc -l ) 
    
    ;;
    

#=====================================================================================
    delann)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP
 usage:
  db_manip delann <dataset>
   - dataset - name of dataset
 results:
   -deletes annotations for specific dataset
  
	";
	exit;
    fi

    DATASET=$2

    echo "DELETE from $SCHEMA.ann WHERE ann.id_dataset = ( SELECT id FROM $SCHEMA.dataset WHERE name='$DATASET')" | $PL $CONN   

    ;;


#=====================================================================================
    insds)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP

 usage:
  db_manip insds <dataset> <dsfile> [<classfile>]
   - dataset - name of dataset
   - dsfile - file containing all information about the dataset
   - classfile - file containing names of classes and their description
 
 Format of the dataset file:
   path/filename1 no1 no2 [<annclass1> <annclass2>...<annclassN> ]
   path/filename2 no1 no2 [<annclass1> <annclass2>...<annclassN> ]
   ...
   
   - no1 is usually used for vpl_id
   - no2 is usually used for frame_no
   
 Format of the a class file:
   class1_name [description string]
   class2_name [description string]
   ...
   
    ";
	exit;
    fi

    DATASET=$2
    DSFILE=$3
    CLFILE=$4
    if [[ ( -z "$DATASET" ) || !( -f "$DSFILE") ]]; then
	echo "Error: Dataset name and dataset description file must be specifierd. For more info use: db_handler command help"
	exit
    fi
    
    # check if the dataset is not present    
    DS_ID=$( echo "SELECT dataset.id FROM $SCHEMA.dataset WHERE name = '$DATASET' " | $PL $CONN)
    if [ -n "$DS_ID" ]; then 
	echo "ERROR: The dataset is already present in the database. "
	exit
    fi
    
    # ADD the dataset and get its ID
    echo "INSERT INTO $SCHEMA.dataset (name) VALUES ('$DATASET') " | $PL $CONN
    DS_ID=$( echo "SELECT dataset.id FROM $SCHEMA.dataset WHERE dataset.name='$DATASET'" | $PL $CONN )
    if [ -z "$DS_ID" ]; then 
	echo "ERROR: Could not add new dataset into the database" >&2
	exit
    fi
    
    
    #get filenames
    IFS=$'\n' read -a FILES -d $'\0' <<< "$( cut -d ' ' -f 1 $DSFILE )"
    
    #get val1
    IFS=$'\n' read -a VAL1 -d $'\0' <<< "$( cut -d ' ' -f 2 $DSFILE )"
    
    #get val2
    IFS=$'\n' read -a VAL2 -d $'\0' <<< "$( cut -d ' ' -f 3 $DSFILE )"

    #add the files
    echo Adding files ${#FILES[*]}
    if [[ ${#FILES[*]} > 0 ]]
    then 
	echo Number of added files $( 
	{
	    echo "INSERT INTO $SCHEMA.files ( id_dataset, filename, frame, vpl_id) VALUES "
	    echo "( $DS_ID, '${FILES[0]}', '${VAL2[0]}', '${VAL1[0]}')"
	    for (( i = 1; i < ${#FILES[*]}; i++))
	    do
	        echo ",( $DS_ID, '${FILES[$i]}', '${VAL2[$i]}', '${VAL1[$i]}')"
	    done
	
	} | $PL $CONN | wc -l )
    fi

    
    declare -a CLASSES

    shopt -s extglob #to be able to use extended patterns in matching

    # add classes    
    if [ -f "$CLFILE" ]; then #if the file exists
	while read line
	do
	    line=$(sed "s/'/ /g" <<< ${line})
	    CLASSES[ ${#CLASSES[*]}]=${line%%+([[:space:]])*}
	    echo "INSERT INTO $SCHEMA.class ( name, description) VALUES ( '${line%%+([[:space:]])*}', '${line##*([[:graph:]])+([[:space:]])}');" | $PL $CONN
	done < $CLFILE 
    fi

    #get the number of classes from the first line of the dataset file
    CLASS_COUNT=$(( $( head -n 1 $DSFILE | wc -w) - 3 )) 
    
    #add generic classes if some or all classes were not specified
    echo Adding classes
    for (( i = ${#CLASSES[*]}; i < $CLASS_COUNT; i++ )) # create generic names for all the remaining classes
    do
	CLASSES[ ${#CLASSES[*]}]=$DATASET.$i
	echo "INSERT INTO $SCHEMA.class ( name) VALUES ( '$DATASET.$i');" | $PL $CONN
    done 
    
    #add annotations into DB
    echo Adding annotations
    echo Number of added class annotations $({
	for (( i = 0; i < $CLASS_COUNT; i++ ))
	do
	    temp='{'$( cut -f $(( $i + 4 )) -d ' ' $DSFILE | gawk '{ORS=""; print "," $0}' | sed 's/,//' )'}'  # get column, make a line from it and put , there to separate the values
    	    echo "INSERT INTO $SCHEMA.ann ( id_dataset, id_class, data) VALUES ( $DS_ID, (SELECT class.id FROM $SCHEMA.class WHERE class.name = '${CLASSES[$i]}'), '$temp');"
	done
    } |$PL $CONN | wc -l ) 
    ;;


#================================================================================================
    delds)
    if [ "${2}" == "help" ] ; then
	echo "$MAINHELP

 usage:
  db_manip delds <dataset>
   - dataset - name of dataset
 notes:
   - removes the dataset, all corresponding files, annotations, 
   - dataset have to exists
   - annotations are first deleted
   - then all files from dataset are deleted, then dataset is erased
";
	exit;
    fi

    DATASET=$2
    
    if [ -z "$DATASET" ]; then
	echo "ERROR: Dataset was not specified. For more info use: db_manip command help" >&2
	exit
    fi

    #get the dataset id	
    DATASETID=$( echo "SELECT id from $SCHEMA.dataset where name='$DATASET'" |$PL $CONN)
    if [ -z "$DATASETID" ]; then 
	echo "The dataset $DATASET does not exist." >&2
	exit
    fi
    
    #are there any classifier results 
    CL_RESULTS=$( echo "SELECT classifier_results.id FROM $SCHEMA.classifier_results WHERE classifier_results.id_dataset = $DATASETID" | $PL $CONN | wc -l)
    CL_DESC=$( echo "SELECT descriptors.id FROM $SCHEMA.descriptors WHERE descriptors.id_dataset = $DATASETID" | $PL $CONN | wc -l)
    
    if [[ $CL_DESC > 0 ]]; then
	a=''
	while [[ "$a" != 'y' && "$a" != 'n' ]]; 
	do
	    echo "Some descriptors files ($CL_DESC) and classifier results ($CL_RESULTS) are tight to the dataset. Do you want to delete them all? y[es]/n[o]:"
	    read a
	done
	
	if [[ "$a" == "n" ]]; then exit; fi
	    
	
	# delete classifier results
	if [[ "$CL_RESULTS" > 0 ]]; then 
	    echo "Classifier results " $(echo "DELETE FROM $SCHEMA.classifier_results WHERE classifier_results.id_dataset = $DATASETID" | $PL $CONN)
	fi
	
	#delete descriptors
	if [[ "$CL_DESC" > 0 ]]; then 
	    echo "Descriptors" $(echo "DELETE FROM $SCHEMA.descriptors WHERE descriptors.id_dataset = $DATASETID" | $PL $CONN)
	fi
	
    fi

    #delete annotations
    echo "Annotations " $(echo "DELETE FROM $SCHEMA.ann WHERE ann.id_dataset = $DATASETID" | $PL $CONN)
    
    #delete files
    echo "Files " $(echo "DELETE FROM $SCHEMA.files WHERE files.id_dataset = $DATASETID" | $PL $CONN)
    
    #delete dataset
    echo "Dataset " $(echo "DELETE FROM $SCHEMA.dataset WHERE dataset.id = $DATASETID" | $PL $CONN)

    ;;


    #===========================================================================
    terminal)
    if [ "aa${2}bb" == "aahelpbb" ] ; then
	echo "$MAINHELP
    #raw terminal only!
    #";
	exit;
    fi
    
    
    $PL $CONN
    ;;

    
    #===========================================================================
    # no valid command entered, help & usage warning
    #===========================================================================
    *)
    
	echo "==================================================================" >&2; 
	echo " DB handler for TRECVID 2010 SIN task" >&2; 
	echo "==================================================================" >&2; 
	echo "" >&2
	echo "Wrong command or no command entered...." >&2
	echo "" >&2
	echo "commands list:" >&2
	echo " listds listbb dscont dscontex ann classann insbb delbb insdesc deldesc listbb listdsdesc listdsseldesc insann delann insds delds" >&2
	echo "" >&2
	echo "for additional information type: <applicationname> <command> help" >&2
	echo "" >&2
	echo "" >&2
	echo "Bye!" >&2
	echo "" >&2
    ;;

esac

