import Foundation

@main
struct ForgejeSeed {
    static func main() async throws {
        // Line-buffer stdout so seed progress streams live instead of being held in a
        // block buffer (and dumped all at once) when stdout is a pipe rather than a TTY.
        setvbuf(stdout, nil, _IOLBF, 0)

        let args = CommandLine.arguments
        guard args.count >= 3 else {
            throw SeedError.missingArguments
        }

        let baseURL = args[1]
        let serviceName = args[2]

        // Compose file path: passed as 3rd arg, or derived from cwd
        let composeFile: String = if args.count >= 4 {
            args[3]
        } else {
            FileManager.default.currentDirectoryPath
                + "/integration/docker-compose.yml"
        }

        let seeder = Seeder(
            baseURL: baseURL,
            serviceName: serviceName,
            composeFile: composeFile,
        )

        try await seeder.seed()
    }
}
