import os
import shutil

# Define source and destination paths
script_dir = os.path.dirname(os.path.abspath(__file__))
source_file = os.path.join(script_dir, 'version.txt')
destination_dir = r'C:\\JenkinsHomeDir\\workspace\\Production-RCCP_NetCore\\RetailCamControlPanel_NetCore\\Content'
destination_file = os.path.join(destination_dir, 'version.txt')

# Ensure destination directory exists
if not os.path.exists(destination_dir):
    os.makedirs(destination_dir)

# Copy version.txt
shutil.copy2(source_file, destination_file)
print('Copied version.txt to {}'.format(destination_file))

# Read, increment, and update version.txt in the script directory
with open(source_file, 'r+') as f:
    version_str = f.read().strip()
    if version_str.isdigit():
        version = int(version_str) + 1
        f.seek(0)
        f.write(str(version))
        f.truncate()
        print('Version incremented to {}'.format(version))
    else:
        print('version.txt does not contain a valid integer.')
