#!/bin/bash
#
# For more information type checkVolLabel --help

declare -r cmdbasename=`basename $0`

showhelp(){
cat << EOF

Usage: $cmdbasename [OPTIONS]
Check volumes labels.

OPTIONS:

    --autochanger=0|1               Required. "1", for physical autochangers, "0" otherwise.
    --changer-device=CHANGERDEV     Required for physical autochangers. If the storage is an
                                    autochanger, this is the SCSI control channel (/dev/sg0, for example).
    --device=DEVICE                 Required. This is the name of a physical drive as it is in
                                    bacula-sd.conf in the case of a physical autochanger or the path for
                                    the drive (/mnt/disk/volumes, for example) for disk volumes (including
                                    virtual autochangers) to be used by Bls utility.
    --bls=PATHTOBLS                 Optional. The script will try to use the path returned by the
                                    command type -p bls.
    --mtx-changer=PATHTOMTXCHANGER  Optional. The script will try to use the path returned by the
                                    command type -p mtx-changer
    --help                          Display this help and exit.

    Examples:
        ./checkVolLabel.sh --autochanger=1 --changer-device=/dev/sg0 --device=Drive-1 --bls=/etc/bacula/bin/bls --mtx-changer=/etc/bacula/bin/mtx-changer
        ./checkVolLabel.sh --autochanger=0 --device=/mnt/disk --bls=/etc/bacula/bin/bls

    Note:
        This script will use always /dev/nst0 for the volumes load/unload by mtx-changer script in the
        case of a physical autochanger.

EOF
}

# variables
: ${BLS:=$(type -p bls)}
: ${MTXCHANGER:=$(type -p mtx-changer)}

arguments=("$@")
for arg; do
  case "$arg" in
     --help)   showhelp;
               exit 0;;
     --autochanger=*) AUTOCHANGER=${arg#*=};;
     --changer-device=*) CHANGERDEV=${arg#*=};;
     --device=*) DEVICE=${arg#*=};;
     --bls=*) BLS=${arg#*=};;
     --mtx-changer=*) MTXCHANGER=${arg#*=};;
     *) printf "%s: unrecognized option \'%s\'" $cmdbasename ${arg%%=*};
        showhelp;
        exit 1;;
  esac
done

if [ -z "$AUTOCHANGER" ]; then
  printf "%s: missing --autochanger=0|1|2 option\n" $cmdbasename
  showhelp
  exit 1
fi

if [ -z "$DEVICE" ]; then
  printf "%s: option --device required.\n" $cmdbasename
  showhelp
  exit 1
fi

if [ -z "$BLS" ]; then
  printf "%s: invalid path to bls utility\n" $cmdbasename
  showhelp
  exit 1
fi

if [ "$AUTOCHANGER" -eq "1" ]; then
  if [ -z "$CHANGERDEV" ]; then
    printf "%s: option --changerdevice required for physical autochnagers.\n" $cmdbasename
    showhelp
    exit 1
  fi
  if [ -z "$MTXCHANGER" ]; then
    printf "%s: invalid path to mtx-changer script\n" $cmdbasename
    showhelp
    exit 1
  fi
  SLOT=($($MTXCHANGER $CHANGERDEV loaded 1 $DEVICE 0))
  $MTXCHANGER $CHANGERDEV unload $SLOT $DEVICE 0
  VOLUMES=($($MTXCHANGER $CHANGERDEV list | awk -F ':' '{printf "%s %s\n", $2, $1}'))
  for ((i=0; i<${#VOLUMES[@]}/2; i++)) do
    if [[ "${VOLUMES[i*2]}" != "CLN"* ]]; then
      $MTXCHANGER $CHANGERDEV load ${VOLUMES[i*2+1]} /dev/nst0 0
      if [ $? -eq 0 ]; then
        ${BLS} -L -V ${VOLUMES[i*2]} $DEVICE > /dev/null
        if [ $? -eq 0 ]; then
          printf "Volume label for %s OK\n" ${VOLUMES[i*2]}
        else
          printf "Volume label for %s ERROR\n" ${VOLUMES[i*2]}
        fi
        $MTXCHANGER $CHANGERDEV unload ${VOLUMES[i*2+1]} /dev/nst0 0
      else
        printf "It was not possible to load media %s from slot %s OK\n" ${VOLUMES[i*2]} ${VOLUMES[i*2+1]}
      fi
    fi
  done
else
  VOLUMES=($(ls $DEVICE | awk -F' ' '{print $1}'))
  for VOL in ${VOLUMES[@]}; do
    ${BLS} -L -V $VOL $DEVICE> /dev/null
    if [ $? -eq 0 ]; then
      printf "Volume label for %s OK\n" $VOL
    else
      printf "Volume label for %s ERROR\n" $VOL
    fi
  done
fi