#!/bin/sh -efu

. bacula-sh-functions

# definitions (fileset)
bacula_local_fileset_file="$bacula_sysconf_dir/fileset/fullset.conf"
bacula_local_fileset_name="Full Set"
bacula_local_filelist_dir="$bacula_filelist_dir/localhost"
bacula_local_filelist_include_file="$bacula_local_filelist_dir/include"
bacula_local_filelist_excludeuser_file="$bacula_local_filelist_dir/exclude-user"
bacula_local_filelist_excludestorage_file="$bacula_local_filelist_dir/exclude-storage"

# definitions (time)
bacula_local_schedule_file="$bacula_sysconf_dir/schedule/weeklycycle.conf"
bacula_local_schedule_name="WeeklyCycle"

# definitions (client)
bacula_local_client_name="fd"

# definitions (backup)
bacula_local_backup_name='BackupFullSet'
bacula_local_volume_name="b"
bacula_local_pool_file="$bacula_pool_dir/default.conf"
bacula_local_pool_name="Default"

### local client specific functions (time)

# setup local schedule
# usage: bacula_local_set_time time
bacula_local_set_time()
{
    local schedtime="$1";shift
    bacula_schedule_set_time \
	"$bacula_local_schedule_file" "$bacula_local_schedule_name" "$schedtime"
    bacula_catalog_set_time "$schedtime"
}

bacula_local_get_time()
{
    bacula_schedule_get_time "$bacula_local_schedule_file"
}

### local client specific functions (fileset)

__local_exclude_fs()
{
    local fs="$1";shift
    local exclude_file="$1";shift

    [ -s "$exclude_file" ] || return 1

    while read f; do
	[ -n "${fs%$f*}" ] || return 0
    done<"$exclude_file"
    return 1
}

__local_include()
{
    local excludeuser_file="$1";shift
    local excludestorage_file="$1";shift

    local fs_spec fs_file fs_vfstype fs_rest

    while read fs_spec fs_file fs_vfstype fs_rest;do
	    case "$fs_vfstype" in
		ext2|ext3|ext4|xfs)
		    __local_exclude_fs "$fs_file" "$excludeuser_file" ||
			__local_exclude_fs "$fs_file" "$excludestorage_file" ||
			echo "$fs_file"
		    ;;
		*) continue ;;
	    esac
    done</etc/fstab >"$bacula_local_filelist_include_file"
}

# write initial local fileset data
# specific: two exclude lists: first to exclude storage data, second - user defined exclude
# specific: include list and exclude-storage lists are autogenerated
__local_fileset()
{
    grep -wqs "$bacula_magic_string" "$bacula_local_fileset_file" &&
	return 0

    mkdir -p -- "$bacula_local_filelist_dir"

    cat>"$bacula_local_filelist_excludeuser_file"<<EOF
/mnt
/media
/opt
/srv
EOF

    >"$bacula_local_filelist_excludestorage_file"

    __director_include_add "$bacula_local_fileset_file"<<EOF
$bacula_magic_string
FileSet {
  Name = "$bacula_local_fileset_name"
  Include {
    Options {
      signature = MD5
    }
    File="<$bacula_local_filelist_include_file"
  }

  Exclude {
    File="<$bacula_local_filelist_excludeuser_file"
    File="<$bacula_local_filelist_excludestorage_file"
  }
}
EOF
}

bacula_local_storage_set_path()
{
    local backup_path="$1";shift

    bacula_storage_set_path "$backup_path"

    # recreate local fileset
    __local_fileset
    cat>"$bacula_local_filelist_excludestorage_file"<<EOF
$backup_path/$bacula_backup_subdir
$bacula_localstate_dir
$bacula_mount_dir
/proc
/sys
/dev
/tmp
/.journal
/.fsck
EOF
    __local_include \
	"$bacula_local_filelist_excludeuser_file" \
	"$bacula_local_filelist_excludestorage_file"
}

bacula_local_storage_get_path()
{
    bacula_storage_get_path
}

bacula_local_exclude_add()
{
    bacula_filelist_add "$bacula_local_filelist_excludeuser_file" "$1"

    # update local include list
    __local_include \
	"$bacula_local_filelist_excludeuser_file" \
	"$bacula_local_filelist_excludestorage_file"
}

bacula_local_exclude_del()
{
    bacula_filelist_del "$bacula_local_filelist_excludeuser_file" "$1"

    # update local include list
    __local_include \
	"$bacula_local_filelist_excludeuser_file" \
	"$bacula_local_filelist_excludestorage_file"
}

bacula_local_exclude_list()
{
    cat "$bacula_local_filelist_excludeuser_file"
}

### local client specific functions (status)

# read current status
bacula_local_client_status()
{
    bacula_file_get_progress "$bacula_local_client_name"
}

### local client specific functions (backup)

bacula_local_backup_run()
{
    bacula_job_run "$bacula_local_backup_name" >&2
    bacula_job_run "$bacula_catalog_job_name" >&2
}

bacula_local_backup_cancel()
{
    bacula_job_cancel "$bacula_local_backup_name"
    bacula_job_cancel "$bacula_catalog_job_name"
}

bacula_local_backup_list()
{
    bacula_console_query_sql "select startTime,jobStatus from Job where Name='$bacula_local_backup_name' and jobStatus!='C' order by StartTime desc;"
}

# start time is always 0 here
bacula_local_waiting_list()
{
    bacula_console_query_sql "select SchedTime from Job where Name='$bacula_local_backup_name' and jobStatus='C' order by StartTime desc;"
}

bacula_local_backup_clear()
{
    local volume_dir="$1";shift

    bacula_pool_clear "$bacula_local_pool_name"
    bacula_job_del_by_name "$bacula_local_backup_name"
}

bacula_local_backup_date_list()
{
    bacula_console_query_sql "select startTime from Job where Name='$bacula_local_backup_name' and jobStatus='T' order by StartTime desc;"
}


### local client specific functions (restore)

bacula_local_restore_file()
{
    local date="$1";shift
    local file="$1";shift

    bacula_restore_file "$bacula_local_client_name" "$bacula_local_fileset_name" "$date" "$file"
}

#### local client specific functions (pool)

# usage: bacula_local_pool_set_retention <value>
bacula_local_pool_set_retention()
{
    local cycle="$1";shift

    __director_include_add "$bacula_local_pool_file"<<EOF
$bacula_magic_string
Pool {
  Name = Default
  Pool Type = Backup
  Recycle = yes
  AutoPrune = yes
  Volume Use Duration = 23 hours
  Volume Retention = $cycle days
  LabelFormat = "${bacula_local_volume_name}-"
}
EOF
}

# usage: bacula_local_pool_get_retention <value>
bacula_local_pool_get_retention()
{
    bacula_pool_get_retention "$bacula_local_pool_file"
}
