#!/usr/bin/env python

# SPDX-License-Identifier: BSD-3-Clause
# SPDX-FileCopyrightText: Czech Technical University in Prague

"""Fix message definitions in a bag file according to local definitions."""

from __future__ import print_function

import os
import sys

from cras_bag_tools.fix_msg_defs import fix_msg_defs
from cras_bag_tools.tqdm_bag import TqdmBag


if __name__ == '__main__':

    if len(sys.argv) <= 1:
        print("Fix message definitions in a bag file according to local definitions", file=sys.stderr)
        print("Usage: fix_msg_defs bag [topic [topic ...]]", file=sys.stderr)
        sys.exit(1)

    bag_file = sys.argv[1]
    topics = sys.argv[2:]

    if not os.path.exists(bag_file):
        print("Bag file %s does not exist." % bag_file, file=sys.stderr)
        sys.exit(2)

    with TqdmBag(bag_file, mode='a', skip_index=True) as bag:
        fix_msg_defs(bag, topics)

        print("Saving changes to %s." % (bag_file,))
        # the bag is autosaved when closed
