#!/bin/sh

if ! command -v git >/dev/null 2>&1; then
    echo "❌ Git is not installed or not in your PATH."
    echo "➡️  Please install Git before running this script: https://git-scm.com/downloads"
    exit 1
fi

if [ -n "$HOME" ]; then
    HOME_DIR="$HOME"
elif [ -n "$USERPROFILE" ]; then
    HOME_DIR="$USERPROFILE"
else
    echo "❌ Unable to detect home directory."
    exit 1
fi

GIT_CONFIG_DIR="$HOME_DIR/.config/git"
GIT_CONFIG_FILE="$GIT_CONFIG_DIR/config"
CREDENTIALS_FILE="$GIT_CONFIG_DIR/credentials"
LEGACY_GITCONFIG="$HOME_DIR/.gitconfig"
LEGACY_CREDENTIALS="$HOME_DIR/.git-credentials"

mkdir -p "$GIT_CONFIG_DIR"

if [ -f "$LEGACY_GITCONFIG" ] && [ ! -f "$GIT_CONFIG_FILE" ]; then
    echo "📦 Moving existing .gitconfig to $GIT_CONFIG_FILE"
    mv "$LEGACY_GITCONFIG" "$GIT_CONFIG_FILE"
elif [ -f "$LEGACY_GITCONFIG" ] && [ -f "$GIT_CONFIG_FILE" ]; then
    echo "⚠️ Both .gitconfig and .config/git/config exist. Leaving both untouched."
fi

if [ -f "$LEGACY_CREDENTIALS" ] && [ ! -f "$CREDENTIALS_FILE" ]; then
    echo "📦 Moving existing .git-credentials to $CREDENTIALS_FILE"
    mv "$LEGACY_CREDENTIALS" "$CREDENTIALS_FILE"
elif [ -f "$LEGACY_CREDENTIALS" ] && [ -f "$CREDENTIALS_FILE" ]; then
    echo "⚠️ Both .git-credentials and .config/git/credentials exist. Leaving both untouched."
fi

if [ ! -f "$GIT_CONFIG_FILE" ]; then
    echo "📝 Creating empty Git config at $GIT_CONFIG_FILE"
    touch "$GIT_CONFIG_FILE"
fi

if [ ! -f "$CREDENTIALS_FILE" ]; then
    echo "🔐 Creating empty credentials file at $CREDENTIALS_FILE"
    touch "$CREDENTIALS_FILE"
fi

printf "Enter your Git user.name: "
read -r GIT_USER
printf "Enter your Git user.email: "
read -r GIT_EMAIL

if [ -n "$GIT_USER" ]; then
    git config --global user.name "$GIT_USER"
else
    echo "⚠️ user.name not set (empty input)"
fi

if [ -n "$GIT_EMAIL" ]; then
    git config --global user.email "$GIT_EMAIL"
else
    echo "⚠️ user.email not set (empty input)"
fi

git config --global credential.helper store
git config --global push.autoSetupRemote true
git config --global pull.rebase true
git config --global rebase.autoStash true
git config --global status.branch true
git config --global alias.st "status -s"
git config --global alias.lol "log --all --graph --decorate --oneline"
git config --global alias.l "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
git config --global alias.stats "shortlog -s -n --no-merges"
git config --global column.ui auto
git config --global branch.sort -committerdate
git config --global tag.sort version:refname
git config --global init.defaultBranch main
git config --global diff.algorithm histogram
git config --global help.autocorrect prompt
git config --global core.whitespace trailing-space
git config --global core.compression 9
git config --global maintenance.auto true
git config --global maintenance.strategy incremental

if command -v diff-so-fancy >/dev/null 2>&1; then
    git config --global core.pager "diff-so-fancy | less --tabs=4 -RF"
    git config --global interactive.diffFilter "diff-so-fancy --patch"
fi

# Done
echo "✅ Git has been configured successfully."
echo "📄 Git config is located at: $GIT_CONFIG_FILE"
echo "🔐 Credentials stored at: $CREDENTIALS_FILE"
