#
# Copyright (c) 2011-2013, 2018, 2026 LAAS/CNRS
# All rights reserved.
#
# Permission to use, copy, modify, and distribute this software for any purpose
# with or without   fee is hereby granted, provided   that the above  copyright
# notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS  SOFTWARE INCLUDING ALL  IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR  BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR  ANY DAMAGES WHATSOEVER RESULTING  FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION,   ARISING OUT OF OR IN    CONNECTION WITH THE USE   OR
# PERFORMANCE OF THIS SOFTWARE.
#
#                                            Anthony Mallet on Sun Jul 24 2011
#

# --- main -----------------------------------------------------------------
#
proc main { } {
  global argc argv

  if { $argc < 1} { usage stderr 2 }

  switch -exact [lindex $argv 0] {
    log2db {
      # [-path category] <bulk-log-dir> <sqlite3-db>
      set opts [lrange $argv 1 end]
      incr argc -1
      set path {}
      switch -- [lindex $opts 0] {
        {-path} {
          set path [lindex $opts 1]
          set opts [lrange $opts 2 end]
          incr argc -2
        }
      }
      if {$argc != 2} { usage stderr 2 }
      log2db [lindex $opts 0] [lindex $opts 1] $path
    }

    db2html {
      # [-path category] <sqlite3-db> <dest-dir>
      set opts [lrange $argv 1 end]
      incr argc -1
      set path {robotpkg}
      switch -- [lindex $opts 0] {
        {-path} {
          set path robotpkg/[lindex $opts 1]
          set opts [lrange $opts 2 end]
          incr argc -2
        }
      }
      if {$argc != 2} { usage stderr 2 }

      db2html [lindex $opts 0] $path [lindex $opts 1]
    }

    db2mail {
      # [-path category] <sqlite3-db> <tag> <email> <sendmail>
      set opts [lrange $argv 1 end]
      incr argc -1
      set path {robotpkg}
      switch -- [lindex $opts 0] {
        {-path} {
          set path robotpkg/[lindex $opts 1]
          set opts [lrange $opts 2 end]
          incr argc -2
        }
      }
      if {$argc != 4} { usage stderr 2 }
      db2mail $path {*}$opts
    }

    rm {
      if {$argc != 3} { usage stderr 2 }
      rmpkg [lindex $argv 1] [lindex $argv 2]
    }

    rmarch - runarch {
      if {$argc != 3} { usage stderr 2 }
      [lindex $argv 0] [lindex $argv 1] [lindex $argv 2]
    }

    default { usage stderr 2 }
  }

  return
}


# --- usage ----------------------------------------------------------------
#
proc usage { channel code } {
  global argv

  set p [file tail [info script]]
  puts $channel "Usage:	$p <command> \[<args>\]"
  puts $channel ""
  puts $channel "Commands:"
  puts $channel "	log2db <bulk-pkg-log-dir> <sqlite3-db>"
  puts $channel "	db2html <sqlite3-db> <destination>"
  puts $channel "	db2mail <sqlite3-db> <arch> <email> <sendmail>"
  puts $channel "	rm <sqlite3-db> <pkg>"
  puts $channel "	rmarch <sqlite3-db> <arch>"

  exit $code
}


# --- errx -----------------------------------------------------------------
#
proc errx { code msg } {
  global argv

  set p [file tail [info script]]
  puts stderr "$p: $msg"
  exit $code
}

proc warnx { msg } {
  global argv

  set p [file tail [info script]]
  puts stderr "$p: $msg"
  return
}

proc infox { msg } {
  puts $msg
  return
}


# --- opendb ---------------------------------------------------------------
#
proc opendb { db } {
  package require sqlite3
  sqlite3 db $db

  # the databases are not critical, and may be located on NFS
  db eval { PRAGMA journal_mode=off }

  db eval {
    create table if not exists
    pkg(
        id integer primary key,
        tag text,
        pkgname text,
        unique (tag, pkgname) on conflict ignore)
  }
  db eval {
    create table if not exists
    meta(
         id int,
         path text,
         pkgbase text,
         na int,
         date_start int,
         date_stop int,
         warning int,
         broken int,
         fixed int,
         sysdep int,
         unique (id) on conflict replace)
  }
  db eval {
    create table if not exists
    breaks(
             id integer,
             breaks integer,
             unique (id, breaks) on conflict ignore)
  }
  db eval {
    create table if not exists
    category(
             id int,
             category text,
             unique (id, category) on conflict ignore)
  }
  db eval {
    create table if not exists
    log(
        id int,
        file text,
        data text,
        unique (id, file) on conflict replace)
  }
  db eval {
    create table if not exists
    session(
        tag text,
        log text,
        date int,
        updates int,
        running int,
        unique (tag) on conflict replace)
  }

  return db
}


# --- closedb --------------------------------------------------------------
#
proc closedb { db analyze } {
  if {$analyze} {
    $db eval { analyze }
    $db eval { vacuum }
  }
  $db close

  return
}
package provide rbulk-report 20260610
