#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys

def update_sql_dump(input_file, output_file, old_username, new_username):
    try:
        # Open input and output files
        with open(input_file, 'r') as f_in, open(output_file, 'w') as f_out:
            # Iterate through each line in the input file
            for line in f_in:
                # Replace old username with new username in each line
                new_line = line.replace(old_username, new_username)
                # Write the modified line to the output file
                f_out.write(new_line)
                # Print the changed line
                if line != new_line:
                    print("Changed Line:")
                    print("Old: ", line.strip())
                    print("New: ", new_line.strip())

        print("SQL dump file updated successfully.")

    except FileNotFoundError:
        print("Input file not found. Please make sure the input file path is correct.")

    except Exception as e:
        print("An error occurred:", str(e))

if __name__ == "__main__":
    # Check if correct number of arguments provided
    if len(sys.argv) != 5:
        print("Usage: python sql_dump_change_role.py <input_file> <output_file> <old_username> <new_username>")
        sys.exit(1)

    # Extract arguments
    input_file = sys.argv[1]
    output_file = sys.argv[2]
    old_username = sys.argv[3]
    new_username = sys.argv[4]

    # Call function to update SQL dump
    update_sql_dump(input_file, output_file, old_username, new_username)