#!/bin/zsh
## Gets $HOME usable in a hurry.
main() {
    set -e
    welcome
    demand_git # note: One of ingy's goals is to undep from git. (TODO)
    choose_remote_github_type
    cd
    get_base_dotdotdot_system
    configure_dotdotdot_conf_file
    .../... super_update_install </dev/null
}

configure_dotdotdot_conf_file() {
    conf=~/.../conf
    cyan_line Configuring the $conf file
    echo
    echo install_method: symlink > $conf
    echo dots: >> $conf
    no_biggie='Skipping this repo should cause no problems.'
    sharpsaw loop \
'This one seems odd, but it is an important piece for plural *-dots to work' \
'Without it, only the first .zshrc/.bashrc/.vimrc/etc will work. "y" is smart.'
    sharpsaw sane \
'These are basic settings that are kind of annoying when they are missing' \
'Without these, you will not have the "ll" alias, etc. No biggie, though.'
    sharpsaw 1337 \
'Semi-aggressive configs for the adventurous.' \
'Skip this if you want, but it can be more fun with it in place.'
    sharpsaw tmux \
'The ,t command (session create-or-attach), plus a tweaked ~/.tmux.conf' \
"Skip if you do not use tmux (but who doesn't use tmux?)"
    sharpsaw vim \
'A wad of scripts and plugins for vim' "$no_biggie"
    sharpsaw git \
'A pile of trs gt cmnds, plus a few config file tweaks.' \
'Decline if you do not want to enjoy a bunch of shortcuts.'
    cyan_line OS-dots
    sharpsaw posix 'Common *nix tools: ,i = install; ,s = search; etc' \
        "$no_biggie"
    sharpsaw x 'X11 config (good for OS X, too)' "$no_biggie"
    if [ -d /Library ]; then
        sharpsaw mac \
'A few spiffups for OS X. Needs more stuff, but should be OK' "$no_biggie"
    elif [ Linux = "`uname`" ]; then
        sharpsaw linux 'Linux-specific stuff' "$no_biggie"
    fi

    cyan_line Language-specific dots
    sharpsaw ruby '~/.pryrc, terse aliases, etc.' "$no_biggie"
    sharpsaw perl 'pmf/mi/mt/etc.' "$no_biggie"
    sharpsaw acmeism 'uniscript .vim, more to come' "$no_biggie"

    sharpsaw local 'Compile/install to $HOME, if without root' "$no_biggie"
    sharpsaw unicode 'Input/search for Unicode chars' "$no_biggie"
    ask_to_add_github_repo exad/zu 'Unarchive anything(?)' "$no_biggie"

    cyan_line OK. Created the following in $conf
    cat $conf
}

welcome() {
    cat <<EOT
`cyan Welcome to the world of Sharpsaw.org!`
  A few quick prompts and you are on your way.

EOT
}

demand_git() {
    if ! type git 2>&1 >/dev/null; then
        cat <<EOT
`red git not found`. This really won't work without it.

Try: apt-get install git
 or: emerge git
 or: Install XCode + Homebrew then brew
...etc.
EOT
      exit 1
    fi
}

choose_remote_github_type() {
    binary_prompt \
        h "Using https means you don't have to set up SSH keys." \
        s "Using ssh means you can 'git push' without password prompts." \
        " Github remotes are: `green h`ttps or `yellow s`sh (default):"
    if [ 's' = "$got" ]; then
        eval `ssh-agent`
        ssh-add
        gh_base='git@github.com:'
    elif [ 'h' = "$got" ]; then
        gh_base='https://github.com/'
    else
        red_line "Uh-oh. Unrecoginized '$got'. Please email oops@sharpsaw.org"
    fi
    echo $gh_base
}

get_base_dotdotdot_system() {
    [ -e .../... ] && return
    cyan_line Getting the ... base system
    git clone ${gh_base}ingydotnet/... || exit 2
}

ask_to_add_github_repo() {
    cyan_line "https://github.com/$1"
    binary_prompt y "$2" n "$3" \
        "Choose `green y` to add this repo (default), or `yellow n` to skip:"
    if [ 'y' = "$got" ]; then
        echo "- repo: $gh_base$1" >> $conf
    fi
    echo
}
sharpsaw() {
    ask_to_add_github_repo sharpsaw/$1-dots "$2" "$3"
}

red() { printf "\033[31;1m%s\033[0m" "$*"; }
red_line() { echo `red "$@"`; }
yellow() { printf "\033[33m%s\033[0m" "$*"; }
yellow_line() { echo `yellow "$@"`; }
green() { printf "\033[32m%s\033[0m" "$*"; }
green_line() { echo `green "$@"`; }
cyan() { printf "\033[36;1m%s\033[0m" "$*"; }
cyan_line() { echo `cyan "$@"`; }
binary_prompt() {
    a=$1
    green_line $2
    b=$3
    yellow_line $4
    prompt=$5
    printf "$prompt "
    unset got
    until [ -n "$got" ]; do
        read got
        [ '' = "$got" ] && got=$a
        case $got in
        $a*) got=$a;;
        $b*) got=$b;;
        *)
            red_line "'$got' was neither $a, nor $b, nor blank (implying $a)."
            printf 'Please try again: '
            unset got
        esac
    done
}

main