package main

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"time"
)

// getRepoRoot returns the full path to the root
// of the closest git dir
func getRepoRoot() string {
	var rootPath string

	path, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
	if err != nil {
		rootPath = "~"
	} else {
		rootPath = strings.TrimSpace(string(path))
	}
	return rootPath
}

func resolveEmoji(hostname string) string {
	emoji := "💀"
	if hostname == "zora" {
		emoji = "🍣"
	} else if hostname == "nostromo" {
		emoji = "🛸"
	}
	return emoji
}

func main() {
	cwd, _ := os.Getwd()
	host, _ := os.Hostname()
	home := os.Getenv("HOME")
	now := time.Now().Format("15:04:05")
	emoji := resolveEmoji(host)
	fmt.Printf("%s %s ", now, emoji)

	gitRoot := getRepoRoot()

	var prefix string
	if gitRoot == home {
		prefix = "~"
	} else {
		prefix = filepath.Base(gitRoot)
	}
	suffix := cwd[len(gitRoot):]
	fmt.Printf("%s", prefix)

	var parts []string
	parts = strings.Split(suffix, "/")
	for i, part := range parts {
		if i == len(parts)-1 {
			fmt.Printf("%s", part)
		} else {
			if len(part) != 0 {
				fmt.Printf("%c/", part[0])
			} else {
				fmt.Printf("/")
			}
		}
	}
}
