module stratego/xtc/Repository
signature
  sorts XtcKey
  constructors
    Tool       : String -> XtcKey
    Repository : XtcKey 
    Import     : XtcKey
    Imported   : String -> XtcKey

    XTC	       : Table

/**
 * Loading the repository
 */
strategies

  /**
   * Returns the location of the XTC repository.
   *
   * @type _ -> String
   */
  xtc-location =
    <get-config> "-r"
    <+ <getenv> "XTC_REPOSITORY"
    <+ GetInternalDefaultXtcRepository

  external GetInternalDefaultXtcRepository(|)

  xtc-read = 
      ?rep
    ; not(<table-get>(XTC, Imported(rep)))
    ; where(<table-put>(XTC, Repository, [rep]))
    ; log(|Vomit(),<conc-strings>("Reading repository ",rep))
    ; read-repository-file
    ; log(|Vomit(),<conc-strings>("Read repository ",rep))
    ; <table-putlist(table-append)>(XTC, <id>)
    ; log(|Vomit(),"Repository stored in table")
    ; <table-put>(XTC, Imported(rep), [])
    ; log(|Vomit(),"Registered repository as imported")

  // read-repository-file =

  xtc-import = 
    where(
      repeat(<table-get>(XTC, Import)
             ; filter(xtc-read)
             ; not([]))
    )

  xtc-load =
    log(|Vomit(),"Loading repository") 
  ; where(
      <table-get>(XTC, Repository)
      <+ xtc-location
         ; xtc-read
	 ; xtc-import
    )
  ; log(|Vomit(),"Loaded repository ")