#!/bin/bash
#
#  vchanger-genudevrules ( vchanger v.1.0.3 ) 2020-05-06
#
#  Search all autochanger configuration files for magazines being defined
#  by filesystem UUID and print to stdout the udev rules for launching
#  mount and umount scripts for those filesystems whenever the corresponding
#  disk drives are attached or detached from the system.
#
LEDIR=/usr/libexec/vchanger
echo "# This file contains udev rules to launch mount and umount scripts"
echo "# for magazines defined by filesystem UUID in the autochanger configuration"
echo "# files under /etc/vchanger."
echo "#"
echo "# This file was generated by the vchanger-genudevrules script."
echo "#"
for cf in `ls -1 /etc/vchanger/*.conf` ; do
  for tmp in `cat ${cf} | grep -v "^#" | tr -d " \t" | grep -i "^magazine="` ; do
    tmp=`echo $tmp | grep -i "^magazine=uuid:"`
    if [ -n "$tmp" ]; then
      tmp=`echo $tmp | cut -d ':' -f 2 | tr -d ' \t\r\n'`
      uuid=${tmp,,}
      echo "ACTION==\"add\",SUBSYSTEM==\"block\", ENV{ID_FS_UUID}==\"$uuid\", RUN+=\"$LEDIR/vchanger-launch-mount.sh $uuid\""
      echo "ACTION==\"remove\",SUBSYSTEM==\"block\", ENV{ID_FS_UUID}==\"$uuid\", RUN+=\"$LEDIR/vchanger-launch-umount.sh $uuid\""
    fi
  done
done
exit 0
