#!/usr/bin/env tclsh
#
#   Dionysus is a search engine for scientific constants and 
#   engineering parameters.
#
#   Copyright (C) 2009 Jean Michel Sellier
#   <jeanmichel.sellier@gmail.com>
# 
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 3, or (at your option)
#   any later version.
#
#   This program 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 General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# created : 08 sep.2009, Jean Michel Sellier, West Lafayette, IN, USA
# last modified : 22 sep.2009, JM Sellier, Cassibile (SR), Italy
#
# how to use this command
# > ./dionysus_query DB.ddb value_name option

# help option
if {[lindex $argv 0]=="--help" || [lindex $argv 0]=="--h"} {
 puts "Usage:"
 puts "> dionysus DB.bb value_name option"
 puts ""
 puts "Example:"
 puts "> dionysus databases/universal.ddb electron_mass value"
 puts ""
 exit 1
}

# version option
if {[lindex $argv 0]=="--version" || [lindex $argv 0]=="--v"} {
 puts "Dionysus version 1.0.0"
 puts ""
 puts "Copyright (C) 2009 Sellier Jean Michel."
 puts "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A"
 puts "PARTICULAR PURPOSE."
 puts "You may redistribute copies of GNU Dionysus under the terms"
 puts "of the GNU General Public License."
 puts "For more information about these matters, see the file named COPYING."
 puts ""
 exit 1
}

# check the number of specified arguments
set num [llength $argv]
if {$num!=3} {
 puts "the number of arguments is wrong."
 puts "please use --help option for more informations."
 exit 1
}

# check and open the specified ASCII DB file
if { [catch {set fp [open [lindex $argv 0] "r"]}]} {
 puts "the specified DB file does not exist or cannot be open"
 exit 1
}

# once we are sure the file exists we scan it
# value name and property
set specname [string tolower [lindex $argv 1]]
set property [string tolower [lindex $argv 2]]

# read the entire file in a string
set data [read $fp]
if {$data==""} {
 puts "the specified DB file is empty!"
 exit 1
}

# split the entire variable "data" into single rows
set rows [split $data "\n"]

# scan every single row and look for what is required
set found 0
set namef 0
for {set i 1} {$i<=[llength $rows]} {incr i} {
 global found
 global namef
 set row [lindex $rows [expr $i-1]]
 # search for an eventual definition
 # parse the row only if it's not a comment
 if {[lindex [split $row " "] 0]!="#"} {
  foreach dum [split $row " "] {
   if {[string tolower $dum]=="def"} {
    # if there is a "def" which does not finish by a "end" then error
    if {$found==1} {
     puts "The DB is not defined correctly. Row $i is \"def\" while it should be \"end\"."
     exit 1
    } else {
     # if everything is all right then set the flag to 1
     set found 1
    }
   }
  }

  # the following row is needed by both "property" and "value"
  set commands [split $row "="]

  # if the word is $property
  if {$namef==1} {
   set comand [split [lindex $commands 0] " "]
   if {[string tolower [lindex $comand 2]]==[string tolower $property]} {
    set ret [lindex $commands 1]
    set primo  [expr [string first "\"" $ret]+1]
    set ultimo [expr [string last "\"" $ret]-1]
    puts [string range $ret $primo $ultimo]
    set $namef 0
    exit 0
   }
  }
  # look for the name of the variable
  if {[string first "name" [string tolower [lindex $commands 0]]]!=-1 && $namef==0} {
   set names [lindex [split $row "="] 1]
   foreach name [split $names "||"] {
    set name [string tolower $name]
    # toggles the double quotes in names
    set primo  [expr [string first "\"" $name]+1]
    set ultimo [expr [string last "\"" $name]-1]
    # "nome" is "name" without the initial and final double quotes
    set nome [string range $name $primo $ultimo]
    if {$nome==$specname} {
     set namef 1
    }
   }
  }
  # if the word is "end"
  foreach dum [split $row " "] {
   if {[string tolower $dum]=="end"} {
    set found 0
    set namef 0
   }
  }
 }
}

close $fp

