#!/usr/bin/python3.11

# Copyright 2025 SUSE LLC
#
# This file is part of gceimgutils
#
# gceimgutils is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# gceimgutils 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 gceremoveblob. If not, see <http://www.gnu.org/licenses/>.

import argparse
import logging
import os
import sys

from gceimgutils.gceuploadblob import GCEUploadBlob
from gceimgutils.gceutils import get_version, get_logger

# Set up command line argument parsing
argparse = argparse.ArgumentParser(description='Upload image blob in GCE')
argparse.add_argument(
    '-c', '--credentials-file',
    dest='credentials_file_path',
    help='Path to the service account credentials file',
    metavar='CREDENTIALS_FILE'
)
argparse.add_argument(
    '-p', '--project',
    dest='project_name',
    help='Project to use for blob upload',
    metavar='PROJECT_NAME',
    required=True
)
argparse.add_argument(
    '--bucket-name',
    dest='bucket_name',
    help='The name of the Bucket where the blob will be uploaded',
    metavar='BUCKET_NAME',
    required=True
)
argparse.add_argument(
    '--blob-name',
    dest='blob_name',
    help=(
        'The name to use for the blob. '
        'If not provided the source filename is used'
    ),
    metavar='BLOB_NAME'
)
argparse.add_argument(
    '--source-filename',
    dest='source_filename',
    help='The path to the file to be uploaded',
    metavar='SOURCE_FILENAME',
    required=True
)
argparse.add_argument(
    '--checksum',
    dest='checksum',
    help=(
        'The type of checksum to compute to verify the integrity of the object'
    ),
    metavar='CHECKSUM',
    choices=['auto', 'md5', 'crc32c']
)
argparse.add_argument(
    '--verbose',
    action='store_true',
    default=False,
    dest='verbose',
    help='Enable verbose output'
)
argparse.add_argument(
    '--version',
    action='version',
    version=get_version(),
    help='Program version'
)

args = argparse.parse_args()

logger = get_logger(args.verbose)

if not args.credentials_file_path and not args.project_name:
    error_msg = (
        'gceuploadblob: error: one of the arguments '
        '--credentials-file or --project is required'
    )
    logger.error(error_msg)
    sys.exit(1)

credentials_file = None
if args.credentials_file_path:
    credentials_file = os.path.expanduser(args.credentials_file_path)

source_filename = None
if args.filename:
    source_filename = os.path.expanduser(args.filename)

log_level = logging.INFO
if args.verbose:
    logging.DEBUG

uploader = GCEUploadBlob(
    credentials_path=credentials_file,
    bucket_name=args.bucket_name,
    blob_name=args.blob_name,
    source_filename=source_filename,
    log_callback=logger,
    log_level=log_level,
    project=args.project_name,
    checksum=args.checksum
)

try:
    uploader.upload_blob()
except Exception as e:
    logger.exception(e)
    sys.exit(1)
