# /packages/intranet-core/www/admin/menus/index.tcl
#
# Copyright (C) 2004 Project/Open
# The code is based on ArsDigita ACS 3.4
#
# 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 2 of the License, 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.

ad_page_contract {
    Show the permissions for all menus in the system

    @author frank.bergmann@project-open.com
} {
    { return_url "/intranet/admin/backup/index" }

    item_remove:optional
    filename:multiple,optional
}

# ------------------------------------------------------
# Defaults & Security
# ------------------------------------------------------

set user_id [ad_maybe_redirect_for_registration]
set user_is_admin_p [im_is_user_site_wide_or_intranet_admin $user_id]

if {!$user_is_admin_p} {
    ad_return_complaint 1 "You have insufficient privileges to use this page"
    return
}

set page_title "Backup & Restore"
set context_bar [im_context_bar $page_title]
set context ""
set find_cmd [parameter::get -package_id [im_package_core_id] -parameter "FindCmd" -default "/bin/find"]

set menu_url "/intranet/admin/menus/new"
set toggle_url "/intranet/admin/toggle"
set group_url "/admin/groups/one"

set bgcolor(0) " class=rowodd"
set bgcolor(1) " class=roweven"

# ------------------------------------------------------
# Get the list of backup sets for restore
# ------------------------------------------------------

# Get the list of all backup sets under backup_path
set backup_path [im_backup_path]

multirow create backup_files filename extension date size

foreach file [glob -nocomplain -type f -directory $backup_path "pg_dump.*.{sql,pgdmp}"]  {
    set trim [string range $file [string length $backup_path] end]

    if {[regexp {(\d\d\d\d)(\d\d)(\d\d)\.(\d\d)(\d\d)\d\d\.([a-z]+)$} $trim match file_year file_month file_day file_hour file_second file_extension]} {

	multirow append backup_files \
	    $file \
	    $file_extension \
	    "$file_day.$file_month.$file_year $file_hour:$file_second" \
	    [file size $file] 
    }
}


template::list::create \
    -name backup_files \
    -key filename \
    -elements {
	filename {
	    label "file name"
	}
	extension {
	    label "type"
	}
	date {
	    label "date"
	}
	size {
	    label "size"
	    html { align right }
	}
	remove {
	    display_template {<a href="restore-pgdmp?filename=@backup_files.filename@&return_url=$return_url">restore</a>}
	}
    } \
    -bulk_actions {
	"Delete" "delete-pgdump" "Remove checked backups"
    } \
    -bulk_action_method post \
    -bulk_action_export_vars { return_url } \
    -actions [list "New backup" [export_vars -base pg_dump] "create new postgres dump"] \






