import os
import subprocess
import sys

def main():
    # Determine the absolute path of this script's directory
    script_dir = os.path.dirname(os.path.abspath(__file__))
    
    # Compute the path to the FoodsharingSiegen.Server project directory
    # This assumes the following structure:
    # files/
    #   Docker/               <-- this script resides here
    #   FoodsharingSiegen.Server/
    project_dir = os.path.normpath(os.path.join(script_dir, '..', 'FoodsharingSiegen.Server'))
    
    # Check if the project directory exists
    if not os.path.isdir(project_dir):
        print(f"Error: Project directory not found: {project_dir}")
        sys.exit(1)
        
    # Prepare the dotnet publish command
    command = ["dotnet", "publish", project_dir]
    print("Running command:", " ".join(command))
    
    # Execute the command and capture the output
    try:
        result = subprocess.run(command, capture_output=True, text=True, check=True)
        print(result.stdout)
        if result.stderr:
            print("Warnings/Errors:", result.stderr, file=sys.stderr)
    except subprocess.CalledProcessError as e:
        print("An error occurred while publishing the dotnet project.")
        print(e.stdout)
        print(e.stderr, file=sys.stderr)
        sys.exit(e.returncode)

if __name__ == '__main__':
    main()
