493 lines
13 KiB
Bash
493 lines
13 KiB
Bash
#!/bin/bash
|
|
|
|
NAME="kfg"
|
|
VERSION="0.2"
|
|
|
|
HOME_PATH="$HOME"
|
|
TMP_PATH="$HOME_PATH/tmp/kamu_install"
|
|
CONFIG_PATH="$HOME_PATH/.config"
|
|
BINARY_PATH="/usr/bin"
|
|
RC_FILE=".zshrc"
|
|
|
|
ASK="\e[38;5;9m"
|
|
INFO="\e[38;5;39m"
|
|
ADD="\e[32m\e[1m"
|
|
ERROR="\033[0;31m"
|
|
ENDCOLOR="\e[0m"
|
|
|
|
declare -A TOOLS || typeset -A TOOLS
|
|
|
|
TOOLS[all]="Install everything"
|
|
TOOLS[zsh]="Powerful Shell"
|
|
TOOLS[tmux]="Terminal Multiplexer - https://github.com/tmux/tmux"
|
|
TOOLS[unp]="Perl script to make file archive extraction easier"
|
|
TOOLS[bat]="cat replacement with syntax highlighting and git integration - https://github.com/sharkdp/bat"
|
|
TOOLS[lsd]="ls replacement with colors, icons, tree-view and more - https://github.com/lsd-rs/lsd"
|
|
TOOLS[omp]="Customisable shell prompt renderer - https://github.com/jandedobbeleer/oh-my-pos"
|
|
|
|
function show_help_name {
|
|
echo "$NAME v$VERSION"
|
|
}
|
|
|
|
function show_help_cmd {
|
|
echo -e "cmd := {
|
|
${INFO}update${ENDCOLOR} \t\t Update $NAME,
|
|
${INFO}install${ENDCOLOR}\t\t Install configs :),
|
|
${INFO}version${ENDCOLOR}\t\t Print the version number
|
|
}\n"
|
|
}
|
|
|
|
function show_help_opt {
|
|
echo -en "opt := {
|
|
${INFO}-h${ENDCOLOR} | ${INFO}--help${ENDCOLOR} \t\t Show this help,
|
|
${INFO}-y${ENDCOLOR} | ${INFO}--yes${ENDCOLOR} \t\t Skip confirmation questions,
|
|
${INFO}-a${ENDCOLOR} | ${INFO}--arch STRING${ENDCOLOR} \t Set the architecture string to be used when downloading files from repositories. Most common are "amd64", "arm64", "armhf", etc,
|
|
${INFO}-d${ENDCOLOR} | ${INFO}--debug${ENDCOLOR}\t\t Print debug output,
|
|
}\n"
|
|
}
|
|
|
|
function show_help {
|
|
show_help_name
|
|
echo -e "Usage: $NAME <cmd> [ opt ]\n"
|
|
show_help_cmd
|
|
show_help_opt
|
|
exit 2
|
|
}
|
|
|
|
function version {
|
|
echo "v$VERSION"
|
|
}
|
|
|
|
function log {
|
|
color $1 "[$2]"
|
|
echo "$3"
|
|
}
|
|
|
|
function log_add {
|
|
log $ADD "+" "$1"
|
|
}
|
|
|
|
function log_info {
|
|
log $INFO "*" "$1"
|
|
}
|
|
|
|
function log_error {
|
|
log $ERROR "!" "$1"
|
|
}
|
|
|
|
function log_debug {
|
|
if [ -z "$P_Debug" ]; then return 0; fi
|
|
log $INFO "&" "$1"
|
|
}
|
|
|
|
function ask {
|
|
color $ASK "[?]"
|
|
read -d '' -p "$1 [Y/n] " -N 1 -r
|
|
}
|
|
|
|
function color {
|
|
echo -en "$1$2$ENDCOLOR "
|
|
}
|
|
|
|
function user_confirm {
|
|
if [ ! -z "$P_Yes" ]; then return 0; fi
|
|
ask "$1"
|
|
while [ true ]; do
|
|
if [[ ! $REPLY =~ ^[$'\n']$ ]]; then echo -e -n "\n"; fi
|
|
case $REPLY in
|
|
[Yy$'\n']* ) return 0;;
|
|
[Nn]* ) return 1;;
|
|
* ) ask "Please enter";;
|
|
esac
|
|
done
|
|
}
|
|
|
|
function user_confirm_overwrite {
|
|
user_confirm "$1 already exists. Do you want to overwrite?"
|
|
return $?
|
|
}
|
|
|
|
function download_config {
|
|
if [ -z "$P_CONFIG" ]; then return 0; fi
|
|
mkdir -p "$CONFIG_PATH/$1/"
|
|
|
|
contains_path="${2%*/*}"
|
|
if [ -z $contains_path ]; then
|
|
mkdir -p "$CONFIG_PATH/$1/$contains_path/"
|
|
fi
|
|
|
|
FILE="$CONFIG_PATH/$1/$2"
|
|
if [ ! -e "$FILE" ] || user_confirm_overwrite "$FILE"; then
|
|
wget -O "$FILE" "$3" > /dev/null 2>&1
|
|
log_add "Added $FILE"
|
|
fi
|
|
}
|
|
|
|
function install_rc {
|
|
if [ -z "$P_RC" ]; then return 0; fi
|
|
# Make sure the directory exists
|
|
mkdir -p "$CONFIG_PATH/$1/"
|
|
|
|
# This is the config file
|
|
FILE="$CONFIG_PATH/$1/.$1.rc"
|
|
|
|
# Does the file already exist? or does the user want to overwrite it?
|
|
if [ ! -e "$FILE" ] || user_confirm_overwrite "$FILE"; then
|
|
# Download the file and save it
|
|
wget -O "$FILE" "$2" > /dev/null 2>&1
|
|
log_add "Added $FILE"
|
|
fi
|
|
|
|
# Add the file to the rc file as source $file
|
|
SOURCE_CMD=$( echo "source $FILE" )
|
|
RC="$HOME_PATH/$RC_FILE"
|
|
if ! cat "$RC" | grep "$SOURCE_CMD" > /dev/null; then
|
|
echo "$SOURCE_CMD" >> "$RC"
|
|
log_add "Added \"$SOURCE_CMD\" to $RC"
|
|
else
|
|
log_info "\"$SOURCE_CMD\" already in $RC"
|
|
fi
|
|
|
|
# Add the file to the rc file as source $file in root
|
|
RC_ROOT="/root/$RC_FILE"
|
|
if ! sudo cat "$RC_ROOT" | grep "$SOURCE_CMD" > /dev/null; then
|
|
echo "$SOURCE_CMD" | sudo tee -a "$RC_ROOT"
|
|
log_add "Added \"$SOURCE_CMD\" to $RC_ROOT"
|
|
else
|
|
log_info "\"$SOURCE_CMD\" already in $RC_ROOT"
|
|
fi
|
|
}
|
|
|
|
function init_install_rc {
|
|
if [ -z "$P_RC" ]; then return 0; fi
|
|
# Add the file to the rc file as source $file
|
|
SOURCE_CMD="$1"
|
|
RC="$HOME_PATH/$RC_FILE"
|
|
if ! cat "$RC" | grep "$SOURCE_CMD" > /dev/null; then
|
|
echo "$SOURCE_CMD" >> "$RC"
|
|
log_add "Added \"$SOURCE_CMD\" to $RC"
|
|
else
|
|
log_info "\"$SOURCE_CMD\" already in $RC"
|
|
fi
|
|
|
|
# Add the file to the rc file as source $file in root
|
|
RC_ROOT="/root/$RC_FILE"
|
|
if ! sudo cat "$RC_ROOT" | grep "$SOURCE_CMD" > /dev/null; then
|
|
echo "$SOURCE_CMD" | sudo tee -a "$RC_ROOT"
|
|
log_add "Added \"$SOURCE_CMD\" to $RC_ROOT"
|
|
else
|
|
log_info "\"$SOURCE_CMD\" already in $RC_ROOT"
|
|
fi
|
|
}
|
|
|
|
function install_deb_check {
|
|
CHECK_CURRENT=$(dpkg -s "$1" | grep -E "Status|Version")
|
|
INSTALLED_CURRENT=$(echo $CHECK_CURRENT | grep -oE "Status: .*" | cut -d ' ' -f 2)
|
|
VERSION_CURRENT=$(echo $CHECK_CURRENT | grep -oE "Version: .*" | cut -d ' ' -f 2)
|
|
CHECK_NEW=$(sudo dpkg -I $TMP_PATH/$1.deb | grep -E "Status|Version")
|
|
INSTALLED_NEW=$(echo $CHECK_NEW | grep -oE "Status: .*" | cut -d ' ' -f 2)
|
|
VERSION_NEW=$(echo $CHECK_NEW | grep -oE "Version: .*" | cut -d ' ' -f 2)
|
|
|
|
if [ ! -z "$CHECK_CURRENT" ]; then
|
|
if [[ "$VERSION_CURRENT" != "$VERSION_NEW" ]]; then
|
|
log_info "$1 ($VERSION_CURRENT) is installed."
|
|
else
|
|
log_info "$1 ($VERSION_CURRENT) is already installed. Skipping ..."
|
|
return 1
|
|
fi
|
|
if ! user_confirm "Do you want to install $1 ($VERSION_NEW)?"; then
|
|
return 1
|
|
fi
|
|
fi
|
|
return 0
|
|
}
|
|
|
|
function install_deb {
|
|
if [ -z "$P_DEB" ]; then return 0; fi
|
|
log_info "Downloading $2 to $TMP_PATH ..."
|
|
wget -O $TMP_PATH/$1.deb $2 > /dev/null 2>&1
|
|
|
|
if ! install_deb_check $1; then
|
|
return 1
|
|
fi
|
|
|
|
INSTALL=$(sudo dpkg -i $TMP_PATH/$1.deb)
|
|
UNPACK=$(echo -n "$INSTALL" | grep -oE "Unpacking .*")
|
|
PROG=$(echo -n $UNPACK | cut -d '(' -f 1 | cut -d ' ' -f 2)
|
|
CURR_VERSION=$(echo -n $UNPACK | cut -d '(' -f 2 | cut -d ')' -f 1)
|
|
PREV_VERSION=$(echo -n $UNPACK | cut -d '(' -f 3 | cut -d ')' -f 1)
|
|
color $ADD "[+]"
|
|
echo -n "Installed $PROG ($CURR_VERSION)"
|
|
if [ ! -z ${PREV_VERSION+x} ]; then
|
|
echo " over ($PREV_VERSION)"
|
|
else
|
|
echo ""
|
|
fi
|
|
#rm $TMP_PATH/$1.deb
|
|
return 0
|
|
}
|
|
|
|
function init {
|
|
if [ ! -z "$P_BINARY" ] || [ ! -z "$P_DEB" ]; then
|
|
log_info "Found Architecture: $ARCH"
|
|
if ! user_confirm "Is '$ARCH' the correct architecture to download programs?"; then
|
|
log_info "Exiting ..."
|
|
exit 0
|
|
fi
|
|
fi
|
|
if [ ! -z "$P_APT" ]; then
|
|
log_info "Running apt update ..."
|
|
sudo apt update > /dev/null 2>&1
|
|
fi
|
|
mkdir -p $TMP_PATH/
|
|
mkdir -p $CONFIG_PATH/
|
|
}
|
|
|
|
function cleanup {
|
|
rm -R $TMP_PATH
|
|
}
|
|
|
|
function apt_install {
|
|
if [ -z "$P_APT" ]; then return 0; fi
|
|
RESULT=$(sudo apt install -y $1 2>&1 )
|
|
if echo "$1 is already the newest version" | grep "$RESULT" > /dev/null; then
|
|
log_info "$1 is already installed"
|
|
else
|
|
log_add "Installed $1"
|
|
fi
|
|
}
|
|
|
|
function github_latest {
|
|
VERSION=$(curl -I "https://github.com/$1/releases/latest/" 2> /dev/null | grep -iE "^Location:" | tr -s ' ' | cut -d '/' -f 8 2> /dev/null)
|
|
echo -n ${VERSION//[$'\t\r\n']}
|
|
}
|
|
|
|
function install_deb_github {
|
|
if [ -z "$P_DEB" ]; then return 0; fi
|
|
local PATTERN=${3/ARCH/$ARCH};
|
|
log_debug "Loading most recent version for $1"
|
|
local VERSION=$(github_latest $2 2> /dev/null )
|
|
VERSION=${VERSION/v/}
|
|
log_debug "Found version $VERSION"
|
|
local FILE=${PATTERN/VERSION/$VERSION}
|
|
local LINK="https://github.com/$2/releases/latest/download/$FILE"
|
|
log_debug "Download link: $LINK"
|
|
install_deb "$1" ${LINK//[$'\t\r\n']}
|
|
}
|
|
|
|
function git_clone {
|
|
CLONE=$(git clone "$1" "$2" 2>&1)
|
|
if [[ $CLONE =~ "already exists" ]]; then
|
|
log_debug "$1 already exists. Running 'git pull'."
|
|
PULL=$(git -C "$2" pull)
|
|
log_info "$PULL"
|
|
fi
|
|
}
|
|
|
|
function install_binary {
|
|
if [ -z "$P_BINARY" ]; then return 0; fi
|
|
log_info "Downloading $2 to $BINARY_PATH ..."
|
|
sudo wget -O $BINARY_PATH/$1 $2 > /dev/null 2>&1
|
|
sudo chmod +x $BINARY_PATH/$1
|
|
}
|
|
|
|
function install_binary_github {
|
|
if [ -z "$P_BINARY" ]; then return 0; fi
|
|
local PATTERN=${3/ARCH/$ARCH};
|
|
log_debug "Loading most recent version for $1"
|
|
VERSION=$(github_latest $2 2> /dev/null)
|
|
VERSION=${VERSION/v/}
|
|
log_debug "Found version $VERSION"
|
|
local FILE=${PATTERN/VERSION/$VERSION}
|
|
local LINK="https://github.com/$2/releases/latest/download/$FILE"
|
|
log_debug "Download link: $LINK"
|
|
install_binary "$1" ${LINK//[$'\t\r\n']}
|
|
}
|
|
|
|
function set_shell {
|
|
if [ -z "$P_ZSH" ]; then return 0; fi
|
|
CURR_U=$(grep "^$USER" /etc/passwd | cut -d ':' -f 7 | cut -d '/' -f 4)
|
|
CURR_R=$(grep "^root" /etc/passwd | cut -d ':' -f 7 | cut -d '/' -f 4)
|
|
if [[ $1 != $CURR_U ]]; then
|
|
log_add "Setting default shell to $1"
|
|
sudo -u $USER chsh -s $(which $1)
|
|
sudo chsh -s $(which $1)
|
|
else
|
|
log_info "Shell already set to $1"
|
|
fi
|
|
}
|
|
|
|
# --------------------------------------------------------------------------------------------------
|
|
|
|
function update {
|
|
cd "$(dirname "$0")"
|
|
wget -O kfg https://git.cbeck.tech/kamu/dotfiles/raw/branch/main/kfg
|
|
exit 0
|
|
}
|
|
|
|
function show_help_install {
|
|
show_help_name
|
|
echo -e "Usage: $NAME install <program> [ opt ]\n"
|
|
echo -e "program := {"
|
|
for key in ${!TOOLS[@]}; do
|
|
echo -e " ${INFO}${key}${ENDCOLOR}\t\t\t ${TOOLS[$key]}"
|
|
done
|
|
echo -e "}\n"
|
|
show_help_opt
|
|
exit 2
|
|
}
|
|
|
|
function p_all {
|
|
p_zsh
|
|
p_tmux
|
|
p_unp
|
|
p_bat
|
|
p_lsd
|
|
p_omp
|
|
}
|
|
|
|
function p_zsh {
|
|
# -----------------
|
|
# zsh
|
|
# -----------------
|
|
apt_install zsh
|
|
set_shell zsh
|
|
# --- TODO --- : Keyboard navigation, Suggestions, ...
|
|
|
|
#wget -O .zshrc https://git.grml.org/f/grml-etc-core/etc/zsh/zshrc
|
|
|
|
#git clone https://github.com/zsh-users/zsh-autosuggestions "$HOME/.config/zsh/zsh-autosuggestions"
|
|
# source ~/.config/zsh/zsh-autosuggestions/zsh-autosuggestions.zsh
|
|
}
|
|
|
|
function p_tmux {
|
|
# -----------------
|
|
# tmux
|
|
# -----------------
|
|
apt_install tmux
|
|
download_config tmux tmux.conf https://git.cbeck.tech/kamu/dotfiles/raw/branch/main/.tmux.conf
|
|
download_config tmux tmux.theme https://git.cbeck.tech/kamu/dotfiles/raw/branch/main/.tmux.theme
|
|
download_config tmux myip.sh https://git.cbeck.tech/kamu/dotfiles/raw/branch/main/myip.sh
|
|
sudo chmod +x ${CONFIG_PATH}/tmux/myip.sh
|
|
install_rc tmux https://git.cbeck.tech/kamu/dotfiles/raw/branch/main/.tmux.rc
|
|
git_clone "https://github.com/tmux-plugins/tpm" "${CONFIG_PATH}/tmux/plugins/tpm"
|
|
}
|
|
|
|
function p_unp {
|
|
# -----------------
|
|
# unp
|
|
# -----------------
|
|
apt_install unp
|
|
# --- TODO --- : rc file
|
|
}
|
|
|
|
function p_bat {
|
|
# -----------------
|
|
# bat
|
|
# -----------------
|
|
install_deb_github bat "sharkdp/bat" "bat_VERSION_ARCH.deb"
|
|
install_rc bat https://git.cbeck.tech/kamu/dotfiles/raw/branch/main/.bat.rc
|
|
}
|
|
|
|
function p_lsd {
|
|
# -----------------
|
|
# Install lsd https://github.com/Peltoche/lsd
|
|
# -----------------
|
|
install_deb_github "lsd" "lsd-rs/lsd" "lsd_VERSION_ARCH.deb"
|
|
install_rc lsd https://git.cbeck.tech/kamu/dotfiles/raw/branch/main/.lsd.rc
|
|
download_config lsd "config.yaml" "https://git.cbeck.tech/kamu/dotfiles/raw/branch/main/.lsd.conf"
|
|
download_config lsd "themes/kamu.yaml" "https://git.cbeck.tech/kamu/dotfiles/raw/branch/main/.lsd.theme"
|
|
}
|
|
|
|
function p_omp {
|
|
# -----------------
|
|
# Install OhMyPosh
|
|
# -----------------
|
|
download_config "omp" "omp.json" "https://git.cbeck.tech/kamu/dotfiles/raw/branch/main/.omp.json"
|
|
install_rc "omp" "https://git.cbeck.tech/kamu/dotfiles/raw/branch/main/.omp.rc"
|
|
install_binary_github "oh-my-posh" "JanDeDobbeleer/oh-my-posh" "posh-linux-ARCH"
|
|
}
|
|
|
|
function install {
|
|
if [ -z $1 ]; then
|
|
show_help_install
|
|
fi
|
|
|
|
init
|
|
|
|
P_APT=1; P_DEB=1; P_CONFIG=1; P_RC=1; P_BINARY=1; P_ZSH=1
|
|
|
|
# -----------------
|
|
# General Config
|
|
# -----------------
|
|
init_install_rc "export KHOME=$HOME_PATH"
|
|
init_install_rc "export XDG_CONFIG_HOME=$CONFIG_PATH"
|
|
install_rc "shell" "https://git.cbeck.tech/kamu/dotfiles/raw/branch/main/.general.rc"
|
|
|
|
while :
|
|
do
|
|
case "$1" in
|
|
help) show_help_install; exit 0 ;;
|
|
"") break ;;
|
|
all) p_all; shift ;;
|
|
zsh) p_zsh; shift ;;
|
|
bat) p_bat; shift ;;
|
|
tmux) p_tmux; shift ;;
|
|
unp) p_unp; shift ;;
|
|
lsd) p_lsd; shift ;;
|
|
omp) p_omp; shift ;;
|
|
# If invalid options were passed, then getopt should have reported an error,
|
|
# which we checked as VALID_ARGUMENTS when getopt was called...
|
|
*) log_error "Unrecognised program: $1"; shift ;;
|
|
esac
|
|
done
|
|
|
|
cleanup
|
|
|
|
exit 0
|
|
}
|
|
|
|
if [ -z $1 ]; then
|
|
show_help
|
|
fi
|
|
|
|
PARSED_ARGUMENTS=$(getopt -a -n install.sh -o hvdya: --long yes,help,debug,arch,verbose: -- "$@")
|
|
VALID_ARGUMENTS=$?
|
|
if [ "$VALID_ARGUMENTS" != "0" ]; then
|
|
show_help
|
|
fi
|
|
|
|
ARCH=$(/usr/bin/dpkg --print-architecture)
|
|
|
|
#echo "PARSED_ARGUMENTS is $PARSED_ARGUMENTS"
|
|
eval set -- "$PARSED_ARGUMENTS"
|
|
while :
|
|
do
|
|
case "$1" in
|
|
-h | --help) show_help ;;
|
|
-v | --verbose) P_Verbose=1 ; shift ;;
|
|
-d | --debug) P_Debug=1 ; shift ;;
|
|
-y | --yes) P_Yes=1 ; shift ;;
|
|
-a | --arch) ARCH="$2" ; shift 2 ;;
|
|
# -- means the end of the arguments; drop this, and break out of the while loop
|
|
--) shift; break ;;
|
|
# If invalid options were passed, then getopt should have reported an error,
|
|
# which we checked as VALID_ARGUMENTS when getopt was called...
|
|
*) log_error "Unexpected option: $1"
|
|
show_help ;;
|
|
esac
|
|
done
|
|
|
|
#echo "Parameters remaining are: $@"
|
|
case "$1" in
|
|
update) update ;;
|
|
version) version ;;
|
|
install) shift; install $@;;
|
|
# -- means the end of the arguments; drop this, and break out of the while loop
|
|
--) shift; break ;;
|
|
# If invalid options were passed, then getopt should have reported an error,
|
|
# which we checked as VALID_ARGUMENTS when getopt was called...
|
|
*) log_error "Unexpected command: $1"
|
|
show_help ;;
|
|
esac
|