#!/bin/bash
# By MB
# run this script to deploy essential functions to the root user
CPATH=$(dirname $0)
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

pause() {
	if $DEBUG; then
		read -n1 -rsp $'Press any key to continue or Ctrl+C to exit...\n'
	fi
}

warning() {
	printf "%-68s[$YELLOW%10s$NC]\n" "$@" "SKIPPED"
	return 0
}

report() {
	if [ $? -eq 0 ]; then
		printf "%-68s[$GREEN%10s$NC]\n" "$@" "OK"
		return 0
	else
		printf "%-68s[$RED%10s$NC]\n" "$@" "FAILED"
		return 1
	fi
}

cat > $HOME/.imgw_alias<<'EOF'
# User specific aliases and functions
alias ll='ls -l'
alias la='ls -A'
alias l='ls -CF'
alias ...="cd ../.."    #go to grandparent dir
alias -- -="cd -"       #go to previous dir
alias l.='ls -d .*'     #list hidden files

alias o='$EDITOR '

# Git shortcuts
alias gits='git status'
alias gitd='git diff --'

# ssh
alias ssh-pwd='ssh -o PreferredAuthentications=password'
EOF
report "[SHELL] .imgw_alias"

cat > $HOME/.imgw_bashrc <<'EOF'
# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

# User specific environment
export HISTCONTROL=ignoreboth:erasedups
export HISTFILESIZE=10000
export HISTSIZE=1000
# time format (Date + time)
export HISTTIMEFORMAT='%F %T '
# combine History of all instances
PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
export PATH="$HOME/.local/bin:$PATH"
export EDITOR=vim
# Uncomment the following line if you don't like systemctl's auto-paging feature:
export SYSTEMD_PAGER=cat

# same version as userservices
ssh-agent-reconnect() {
    for agent in /tmp/ssh-*/agent.* /run/user/$UID/keyring/ssh $HOME/.ssh/ssh-agent-socket; do
        export SSH_AUTH_SOCK=$agent
        if ssh-add -l &>/dev/null; then
            echo "[s] Found working SSH Agent:"
            ssh-add -l
            return
        fi
    done
    echo "[s] No ssh-agent found."
    echo "[s] start new agent: eval \$(ssh-agent -s)"
}

# transfer function
transfersh() {
    if [ $# -eq 0 ]; then
        echo "No arguments specified.\nUsage:\n transfer <file|directory>\n ... | transfer <file_name>" >&2
        return 1
    fi
    if tty -s; then
        file="$1"
        file_name=$(basename "$file")
        if [ ! -e "$file" ]; then
            echo "$file: No such file or directory" >&2
            return 1
        fi
        if [ -d "$file" ]; then
            file_name="$file_name.zip"
            (cd "$file" && zip -r -q - .) | curl -H "Max-Downloads: 1" --progress-bar --upload-file "-" "https://transfersh.wolke.img.univie.ac.at/$file_name" | tee /dev/null
        else 
            cat "$file" | curl -H "Max-Downloads: 1" --progress-bar --upload-file "-" "https://transfersh.wolke.img.univie.ac.at/$file_name" | tee /dev/null
            echo
        fi
    else
        file_name=$1
        curl -H "Max-Downloads: 1" --progress-bar --upload-file "-" "https://transfersh.wolke.img.univie.ac.at/$file_name" | tee /dev/null
        echo
    fi
}

transfersh4ever() {
    if [ $# -eq 0 ]; then
        echo "No arguments specified.\nUsage:\n transfer <file|directory>\n ... | transfer <file_name>" >&2
        return 1
    fi
    if tty -s; then
        file="$1"
        file_name=$(basename "$file")
        if [ ! -e "$file" ]; then
            echo "$file: No such file or directory" >&2
            return 1
        fi
        if [ -d "$file" ]; then
            file_name="$file_name.zip"
            (cd "$file" && zip -r -q - .) | curl --progress-bar --upload-file "-" "https://transfersh.wolke.img.univie.ac.at/$file_name" | tee /dev/null
        else 
            cat "$file" | curl --progress-bar --upload-file "-" "https://transfersh.wolke.img.univie.ac.at/$file_name" | tee /dev/null
            echo
        fi
    else
        file_name=$1
        curl --progress-bar --upload-file "-" "https://transfersh.wolke.img.univie.ac.at/$file_name" | tee /dev/null
        echo
    fi
}

# root needs to be asked before removal
if [ $EUID -eq 0 ]; then
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
fi

EOF
report "[SHELL] .imgw_bashrc"


cat > $HOME/.tmux.conf <<EOF
# newer version of tmux
set -g mouse on
set -g history-limit 30000
# older version of tmux
#setw -g mode-mouse on
#set -g mouse-select-window on
#set -g mouse-select-pane on
#set -g mouse-resize-pane on
#set -g mouse-utf on
EOF
report "[SHELL] .tmux.conf"

#
# Append to bashrc
#
cat >> $HOME/.bashrc<<EOF
if [ -f \$HOME/.imgw_alias ]; then
    . \$HOME/.imgw_alias
fi
if [ -f \$HOME/.imgw_bashrc ]; then
    . \$HOME/.imgw_bashrc
fi
EOF
report "[SHELL] .bashrc"

#
# input rc                                                                                                                                                  
#
cat > $HOME/.inputrc<<EOF
# Make history searchable
"\e[B": history-search-forward
"\e[A": history-search-backward
"\e[1~": beginning-of-line
"\e[4~": end-of-line
"\e[5~": history-search-backward
"\e[6~": history-search-forward
"\e[3~": delete-char
"\e[2~": quoted-insert
"\e[5C": forward-word
"\e[5D": backward-word
"\e\e[C": forward-word
"\e\e[D": backward-word
set completion-ignore-case On
set show-all-if-ambiguous on
set completion-query-items 350
set page-completions off
set match-hidden-files off

# Include system wide settings which are ignored
# by default if one has their own .inputrc
\$include /etc/inputrc
EOF
report "[SHELL] .inputrc"

# Git credential helper
echo "Maybe run: git config --global credential.helper store"