package main

import (
	"bufio"
	"fmt"
	"os"
	"path/filepath"
	"strings"
)

// runGuided is what happens when somebody double-clicks the program instead of
// typing its name at a prompt.
//
// Without this, Explorer opens a console, main() finds no arguments, prints
// the usage text to stderr and exits — and Windows destroys the window in the
// same instant. From the other side of the screen that is indistinguishable
// from a crash. So when we know we were double-clicked, we ask the one
// question the program actually needs and stay on screen until the reader is
// done.
//
// This path is entered ONLY when there are no arguments and both ends of the
// program are a real console. Any scripted or piped use takes exactly the same
// code path it always did.
//
// PrivacySweep destroys files beyond recovery, and there is no undo and no
// quarantine. So guided mode runs the preview and ONLY the preview: it lists
// and fingerprints what a real erase would destroy and stops there. Erasing
// for real stays where it belongs — a deliberate command typed at a prompt.
func runGuided() {
	in := bufio.NewScanner(os.Stdin)

	fmt.Println()
	fmt.Println("  PrivacySweep")
	fmt.Println("  Erase files so thoroughly that recovery software cannot bring them back.")
	fmt.Println()
	fmt.Println("  Ordinary deleting only forgets where a file was; the contents stay on")
	fmt.Println("  the disk until something else happens to land on top. PrivacySweep")
	fmt.Println("  writes random data over the file itself before removing it.")
	fmt.Println()
	fmt.Println("  Right now I will only SHOW you what a real erase would destroy, with")
	fmt.Println("  the size and fingerprint of each file. Nothing at all is erased,")
	fmt.Println("  changed or removed by this preview.")
	fmt.Println()

	suggested := suggestedFolder()
	for {
		fmt.Println("  Which file or folder shall I preview?")
		if suggested != "" {
			fmt.Printf("  (press Enter for %s)\n", suggested)
		}
		fmt.Print("  > ")

		if !in.Scan() {
			// stdin closed on us; there is nothing sensible left to ask.
			return
		}
		answer := strings.TrimSpace(in.Text())
		answer = strings.Trim(answer, `"`)
		if answer == "" {
			answer = suggested
		}
		if answer == "" {
			fmt.Println()
			fmt.Println("  I need a file or folder to look at. Try again, or close this window.")
			fmt.Println()
			continue
		}

		if _, err := os.Stat(answer); err != nil {
			fmt.Println()
			fmt.Printf("  I cannot find %q.\n", answer)
			fmt.Println("  Tip: you can drag a file or folder from Explorer onto this window")
			fmt.Println("  to paste its location, then press Enter.")
			fmt.Println()
			continue
		}

		fmt.Println()
		fmt.Println("  Reading. Every file has to be fingerprinted, so this can take a")
		fmt.Println("  minute on a large folder.")
		fmt.Println()
		cmdShred([]string{answer})
		break
	}

	fmt.Println()
	fmt.Println("  That was a preview. Everything listed above is still exactly where")
	fmt.Println("  it was, untouched.")
	fmt.Println()
	fmt.Println("  Erasing for real is only ever done from a command prompt, on purpose,")
	fmt.Println("  and it cannot be undone: privacysweep help")
	pause(in)
}

// suggestedFolder offers somewhere plausible that is certain to exist, so the
// reader can see a real preview by pressing one key.
func suggestedFolder() string {
	home, err := os.UserHomeDir()
	if err != nil {
		return ""
	}
	// The download folder is where files people later want gone tend to
	// accumulate, and it is the least alarming place to demonstrate on.
	for _, name := range []string{"Downloads", "Documents"} {
		candidate := filepath.Join(home, name)
		if info, err := os.Stat(candidate); err == nil && info.IsDir() {
			return candidate
		}
	}
	return home
}

// pause keeps the console window open. Explorer closes it the moment the
// process exits, so without this the reader never sees the output.
func pause(in *bufio.Scanner) {
	fmt.Println()
	fmt.Print("  Press Enter to close this window. ")
	in.Scan()
}
