#!/usr/libexec/platform-python

# rdautocheck.py
#
# Read metadata for Rivendell backup sets.
#
#   (C) Copyright 2024 Fred Gleason <fredg@paravelsystems.com>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License version 2 as
#   published by the Free Software Foundation.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public
#   License along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import configparser
import datetime
import MySQLdb
import os
from pathlib import Path
import sys
import syslog

def PrintMetadata(mntpt):
    #
    # Mount backup device
    #
    Path(mntpt).mkdir(parents=True,exist_ok=True)
    result=os.WEXITSTATUS(os.system(command='mount '+mntpt+" 2> /dev/null"))
    if((result!=0)and(result!=64)):
        print(mntpt+': [not found]')
        print()
        return
    os.system(command='sleep 5')

    #
    # Read Info File
    #
    try:
        with open(mntpt+'/INFO.txt','r') as f:
            print(mntpt+':')
            print(f.read())
            f.close()
    except FileNotFoundError:
        print(mntpt+':')
        print('[no metadata found]')
        print()

    #
    # Unmount backup device
    #
    os.system(command='umount '+mntpt)
    os.rmdir(mntpt)

USAGE='rdautocheck.py <backup-mountpoint1> [<backup-mountpoint2>] [...]'

if(len(sys.argv)<2):
    print(USAGE)
    exit(1)
for arg in range(1,len(sys.argv)):
    PrintMetadata(sys.argv[arg])
