#!/bin/sh

. scripts/functions

$bperl \
    -e "extract_resource('$conf/bacula-dir.conf', 'Client', '$HOST-fd')" \
    > $tmp/1

$bperl \
    -e "extract_resource('$conf/bacula-dir.conf', 'Storage', 'File')" \
    > $tmp/2

sed "s/$HOST-fd/test1-fd/" $tmp/1 >> $conf/bacula-dir.conf
sed "s/$HOST-fd/test2-fd/" $tmp/1 >> $conf/bacula-dir.conf
sed "s/$HOST-fd/test-rst-fd/" $tmp/1 >> $conf/bacula-dir.conf
sed "s/$HOST-fd/test-bkp-fd/" $tmp/1 >> $conf/bacula-dir.conf


sed "s/$HOST-fd/T1:C1/" $tmp/1 > $tmp/tenant.conf
sed "s/$HOST-fd/T1:C2/" $tmp/1 >> $tmp/tenant.conf
sed "s/Name = File/Name = T1:S1/" $tmp/2 >> $tmp/tenant.conf

cat <<EOF >> $tmp/tenant.conf
Tenant {
 Name = T1
 MaximumVolumeBytes = 2000
 WarningVolumeBytes = 1000
}
Console {
  Name = T1
  Password = T1
  TenantACL = T1
  CatalogACL = *all*
  DirectoryACL = *all*
  UserIdACL = *all*
  WhereACL = *all*
  CommandACL = *all*
}
FileSet {
 Name = T1:F1
 Include {
   File = $cwd/build/po
 }
}
FileSet {
 Name = T1:F2
 Include {
   File = $cwd/build/po
 }
}
Pool {
  Name = T1:P1
  Pool Type = Backup
  Recycle = yes                       # Bacula can automatically recycle Volumes
  AutoPrune = yes                     # Prune expired volumes
  Volume Retention = 365d             # one year
}
JobDefs {
  Name = "T1:JD"
  Type = Backup
  Level = Incremental
  Messages = Standard
  Priority = 10
  Maximum Concurrent Jobs = 16
}
Job {
  Name = T1:J1
  JobDefs = T1:JD
  Client = T1:C1
  Pool = T1:P1
  FileSet = T1:F1
  Storage = T1:S1
}
EOF

cat <<EOF >> $conf/bacula-dir.conf
FileSet {
  Name = empty
}
Pool {
  Name = Test
  Pool Type = Backup
  Recycle = yes                       # Bacula can automatically recycle Volumes
  AutoPrune = yes                     # Prune expired volumes
  Volume Retention = 365d             # one year
}
Console {
  Name = restricted
  Password = xxx
  JobAcl    = *all*
  ClientAcl = test1-fd, test2-fd
  RestoreClientAcl = test-rst-fd
  BackupClientAcl = test-bkp-fd  
  PoolAcl   = *all*
  CatalogAcl  = *all*
  FileSetAcl  = *all*
  CommandAcl  = *all*
  StorageAcl  = *all*
  WhereAcl = $tmp/test-bkp-fd-restore, $tmp/test1-fd-restore, $tmp/test-rst-fd-restore-1, $tmp/test-rst-fd-restore-2
  DirectoryAcl = *all*
  UserIdAcl = *all*
}
Console {
  Name = restricted-bvfs1
  Password = xxx
  JobAcl    = *all*
  ClientAcl = test1-fd, test2-fd
  RestoreClientAcl = test-rst-fd
  PoolAcl   = *all*
  CatalogAcl  = *all*
  FileSetAcl  = *all*
  CommandAcl  = *all*
  StorageAcl  = *all*
  WhereAcl = $tmp/test-bkp-fd-restore, $tmp/test1-fd-restore, $tmp/test-rst-fd-restore-1, $tmp/test-rst-fd-restore-2
  DirectoryAcl = *all*
  UserIdAcl = *all*
}
Console {
  Name = restricted-bvfs2
  Password = xxx
  JobAcl    = *all*
  RestoreClientAcl = test-rst-fd
  PoolAcl   = *all*
  CatalogAcl  = *all*
  FileSetAcl  = *all*
  CommandAcl  = *all*
  StorageAcl  = *all*
  WhereAcl = $tmp/test-bkp-fd-restore, $tmp/test1-fd-restore, $tmp/test-rst-fd-restore-1, $tmp/test-rst-fd-restore-2
  DirectoryAcl = *all*
  UserIdAcl = *all*
}
Console {
  Name = restricted-job
  Password = xxx
  JobAcl    = $JobName
  ClientAcl = *all*
  PoolAcl   = *all*
  CatalogAcl  = *all*
  FileSetAcl  = *all*
  CommandAcl  = *all*
  StorageAcl  = *all*
  WhereAcl = *all*
  DirectoryAcl = *all*
  UserIdAcl = *all*
}
Console {
  Name = restricted-client
  Password = xxx
  JobAcl    = *all*
  ClientAcl = $HOST-fd
  PoolAcl   = *all*
  CatalogAcl  = *all*
  FileSetAcl  = *all*
  CommandAcl  = *all*
  StorageAcl  = *all*
  WhereAcl = *all*
  DirectoryAcl = *all*
  UserIdAcl = *all*
}
Console {
  Name = restricted-clients
  Password = xxx
  JobAcl    = *all*
  ClientAcl = $HOST-fd, test1-fd
  PoolAcl   = *all*
  CatalogAcl  = *all*
  FileSetAcl  = *all*
  CommandAcl  = *all*
  StorageAcl  = *all*
  WhereAcl = *all*
  DirectoryAcl = *all*
  UserIdAcl = *all*
}
Console {
  Name = restricted-pool
  Password = xxx
  JobAcl    = *all*
  ClientAcl = *all*
  PoolAcl   = Test
  CatalogAcl  = *all*
  FileSetAcl  = *all*
  CommandAcl  = *all*
  StorageAcl  = *all*
  WhereAcl = *all*
  DirectoryAcl = *all*
  UserIdAcl = *all*
}
Console {
  Name = restricted-fileset
  Password = xxx
  JobAcl    = *all*
  ClientAcl = *all*
  PoolAcl   = *all*
  CatalogAcl  = *all*
  FileSetAcl  = "Full Set"
  CommandAcl  = *all*
  StorageAcl  = *all*
  WhereAcl = *all*
  DirectoryAcl = *all*
  UserIdAcl = *all*
}
Console {
  Name = restricted-job-client
  Password = xxx
  JobAcl    = backup
  ClientAcl = test1-fd
  PoolAcl   = *all*
  CatalogAcl  = *all*
  FileSetAcl  = *all*
  CommandAcl  = *all*
  StorageAcl  = *all*
  WhereAcl = *all*
  DirectoryAcl = *all*
  UserIdAcl = *all*
}
EOF

if test "$FORCE_TENANT" = "1"; then
    cat $tmp/tenant.conf >> $conf/bacula-dir.conf
    sed "s/T1/T2/g" $tmp/tenant.conf >> $conf/bacula-dir.conf
fi

cat <<EOF >> $tmp/bconsole.conf2
Console {
  Name = restricted
  Password = xxx
}
EOF

sed 's/Password.*/Password = xxx/' $conf/bconsole.conf >> $tmp/bconsole.conf2

for i in job pool client clients fileset bvfs1 bvfs2 job-client
do
    sed "s/restricted/restricted-$i/" $tmp/bconsole.conf2 > $tmp/bconsole.conf.$i
done

sed 's/Password.*/Password = T1/' $tmp/bconsole.conf2 | sed 's/restricted/T1/' >> $tmp/bconsole.conf.T1

sed 's/Password.*/Password = T2/' $tmp/bconsole.conf2 | sed 's/restricted/T2/' >> $tmp/bconsole.conf.T2

