#!/bin/sh

#
# outer_loop                                           ts A01228
# 
# This script demonstrates how to perform the single steps of a
# scdbackup run within a controlling shell script. It is possible
# to perform arbitrary activities between those steps.
#

 
# plan backup and create backup script  
scdbackup -prepare_only "$@"
echo ; echo ; echo

# obtain number of volumes
count=""
count=$(scdbackup -last_volume_count)
if test "$count" = "" ; then
  count=999999999
fi

# loop to create all volumes
i=1
while test $i -le $count ; do

# pre-volume action 
  echo "trying volume $i of $count"
  echo ; echo ; echo

# create single volume
  scdbackup -resume -$i

# post-volume action
  echo ; echo ; echo
  exit_code="$?"
  if test "$exit_code" = 0 ; then
    echo "seems to be OK"
  elif test "$exit_code" = 11 ; then 
    echo "The volume number $i was too large, backup is done"
    count=$(echo "$i-1" | bc)
  else
    echo "failed with exit code $exit_code"
    exit $exit_code
  fi

  i=$(echo "$i+1" | bc)
done

echo "all $count volumes done"