###############################################################################
#
# common.eagle --
#
# Written by Joe Mistachkin.
# Released to the public domain, use at your own risk!
#
###############################################################################

if {[isEagle]} then {
  proc getAssemblyFileName { fileName } {
    return [file join [file dirname $::path] bin \
        [expr {[haveConstraint imageRuntime40] ? "2010" : "2008"}] \
        $::test_configuration bin $fileName]
  }

  proc copyAssembly { fileName } {
    #
    # NOTE: If we cannot copy the file then it is probably already loaded,
    #       ignore the error.
    #
    catch {
      file copy -force [getAssemblyFileName $fileName] \
          [file join [info binary] $fileName]
    }
  }

  proc loadAssembly { fileName } {
    set fileName [getAssemblyFileName $fileName]

    if {[catch {set assembly \
        [object load -loadtype File $fileName]}] == 0} then {
      #
      # NOTE: Now, add the necessary test constraint.
      #
      addConstraint [file rootname [file tail $fileName]]

      #
      # NOTE: Return the full path of the loaded file.
      #
      return $fileName
    }

    return ""
  }

  proc setupDb {fileName {mode ""} {delete true} {varName db}} {
    set fileName [file join [getTemporaryPath] $fileName]

    if {$delete} then {
      catch {file delete $fileName}
    }

    upvar 1 $varName db

    set connection {Data Source=${fileName}}

    if {[string length $mode] > 0} then {
      append connection {;Journal Mode=${mode}}
    }

    set db [sql open -type SQLite [subst $connection]]
  }

  proc cleanupDb {fileName {varName db}} {
    upvar 1 $varName db
    catch {sql close $db}
    catch {file delete [file join [getTemporaryPath] $fileName]}
  }
}
