#!/bin/bash
# By MB
# Install and setup bash customizations


inputrcs(){
    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
}

bashrcs(){
    cat > $HOME/.imgw_bashrc <<'EOF'
# 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

# 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
# shell options
shopt -s direxpand
EOF
}

aliass(){
    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 '

# systemctl
alias ssc="systemctl"
alias sscs="systemctl status"

# firewall
alias fwc="firewall-cmd"
alias fwcl="firewall-cmd --list-all"
alias fwclz="firewall-cmd --list-all-zones"

# podman-compose
alias podc="podman-compose"
podcr(){
    podman-compose -f "$1" down && podman-compose -f "$1" up -d
}


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

# ssh
alias ssh-pwd='ssh -o PreferredAuthentications=password'

# GPFS
EOF
}

if [ ! -f $HOME/.inputrc ]; then
    inputrcs
fi


if [ ! -f $HOME/.imgw_bashrc ]; then
    bashrcs
fi

cat $HOME/.bashrc | grep .imgw_bashrc > /dev/null
if [ $? -ne 0 ]; then
    cat >>$HOME/.bashrc <<EOF
if [ -f \$HOME/.imgw_bashrc ]; then
    . \$HOME/.imgw_bashrc
fi
EOF
fi

if [ ! -f $HOME/.imgw_alias ]; then
    aliass
fi

cat $HOME/.bashrc | grep .imgw_alias > /dev/null
if [ $? -ne 0 ]; then
    cat >>$HOME/.bashrc <<EOF
if [ -f \$HOME/.imgw_alias ]; then
    . \$HOME/.imgw_alias
fi
EOF
fi
# Git credential helper
echo "Maybe run: git config --global credential.helper store"
