package main

import "os"

// interactiveConsole reports whether a person is sitting in front of a console
// waiting for this program, as opposed to a script or a pipe running it.
//
// This is the condition that matters when somebody double-clicks the program
// in Windows Explorer. Explorer opens a fresh console, runs the program in it
// with no arguments, and destroys the window the instant the process exits —
// so printing usage and quitting looks exactly like a crash. When both ends
// are a real console we stay on screen and ask what to do instead.
//
// Deliberately checked with a character-device test rather than the Windows
// GetConsoleProcessList call: this one is true on every platform, behaves the
// same for a pipe, a file and a redirect, and can be tested for real.
//
// Anything scripted fails the test and takes exactly the path it always did:
//   - drivepulse scan D:\Photos        has arguments, never reaches here
//   - drivepulse < /dev/null           stdin is not a character device
//   - drivepulse | more                stdout is a pipe
func interactiveConsole() bool {
	return isCharDevice(os.Stdin) && isCharDevice(os.Stdout)
}

func isCharDevice(f *os.File) bool {
	info, err := f.Stat()
	if err != nil {
		return false
	}
	return info.Mode()&os.ModeCharDevice != 0
}
