/**
# Copyright (c) Advanced Micro Devices, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the \"License\");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an \"AS IS\" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
**/

package main

import (
	"log/slog"
	"os"

	gpuTracker "github.com/ROCm/container-toolkit/internal/gpu-tracker"
	"github.com/ROCm/container-toolkit/internal/logger"
	"github.com/ROCm/container-toolkit/internal/runtime"
)

func main() {
	logger.Init(false)

	if os.Getuid() != 0 {
		slog.Error("Rootless mode is not supported")
		os.Exit(1)
	}

	slog.Info("Creating ROCm container runtime", "args", os.Args)

	rt, err := runtime.New(os.Args)
	if err != nil {
		slog.Error("Failed to create container runtime", "error", err)
		gpuTracker, err := gpuTracker.New()
		if err != nil {
			slog.Error("Failed to create GPU tracker", "error", err)
			os.Exit(1)
		}
		gpuTracker.ReleaseGPUs(os.Args[len(os.Args)-1])
		os.Exit(1)
	}

	slog.Info("Running ROCm container runtime")
	err = rt.Run()
	if err != nil {
		slog.Error("Failed to run container runtime", "error", err)
		gpuTracker, err := gpuTracker.New()
		if err != nil {
			slog.Error("Failed to create GPU tracker", "error", err)
			os.Exit(1)
		}
		gpuTracker.ReleaseGPUs(os.Args[len(os.Args)-1])
		os.Exit(1)
	}
}
