2023-07-26 21:18:24 +08:00
|
|
|
#!/usr/bin/env bash
|
2023-01-06 16:14:41 +08:00
|
|
|
set -e
|
2022-08-26 20:43:00 +08:00
|
|
|
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
|
|
|
|
source "$THIS_DIR/tools/common.sh"
|
2022-05-12 19:29:49 +08:00
|
|
|
|
2022-11-05 19:45:21 +08:00
|
|
|
|
|
|
|
if [[ ! "$DOTFILES" == "${HOME}"* ]]; then
|
2022-08-26 20:43:00 +08:00
|
|
|
fmt_fatal "\"$DOTFILES\" is not under \"$HOME\". aborting ..."
|
2022-05-12 19:29:49 +08:00
|
|
|
fi
|
2022-11-05 19:45:21 +08:00
|
|
|
DOTFILE_TILDE=${DOTFILES/"$HOME"/\~}
|
|
|
|
|
|
|
|
CRON_JOB="0 * * * * ${DOTFILES}/update.sh"
|
2023-01-06 16:14:41 +08:00
|
|
|
declare -a DFS_CONFIGS
|
2022-11-05 19:45:21 +08:00
|
|
|
declare -a HOME_FILES_PATH
|
|
|
|
declare -a HOME_FILES_CONTENT
|
|
|
|
HOME_FILES_PATH[0]=".zshrc"
|
|
|
|
HOME_FILES_CONTENT[0]="source ${DOTFILE_TILDE}/.zshrc2"
|
|
|
|
HOME_FILES_PATH[1]=".tmux.conf"
|
|
|
|
HOME_FILES_CONTENT[1]="source-file ${DOTFILE_TILDE}/.tmux.conf2"
|
|
|
|
HOME_FILES_PATH[2]=".vimrc"
|
|
|
|
HOME_FILES_CONTENT[2]="source ${DOTFILE_TILDE}/.vimrc2"
|
|
|
|
HOME_FILES_PATH[3]=".gitconfig"
|
|
|
|
HOME_FILES_CONTENT[3]="[include] path = \"${DOTFILE_TILDE}/.gitconfig2\""
|
|
|
|
|
|
|
|
declare -a HOME_SYMLINKS_SRC
|
|
|
|
declare -a HOME_SYMLINKS_DST
|
|
|
|
HOME_SYMLINKS_SRC[0]=".ssh/authorized_keys2"
|
|
|
|
HOME_SYMLINKS_DST[0]=".ssh/authorized_keys2"
|
2023-07-27 02:46:28 +08:00
|
|
|
HOME_SYMLINKS_SRC[1]=".eid/authorized_certificates"
|
|
|
|
HOME_SYMLINKS_DST[1]=".eid/authorized_certificates"
|
2022-11-05 19:45:21 +08:00
|
|
|
|
2022-11-08 22:58:17 +08:00
|
|
|
install_dependencies()
|
|
|
|
{
|
2023-01-06 16:14:41 +08:00
|
|
|
local ret=0
|
2022-11-08 22:58:17 +08:00
|
|
|
fmt_note "installing dependencies ..."
|
2023-01-06 16:14:41 +08:00
|
|
|
set +e
|
2022-12-03 20:37:20 +08:00
|
|
|
case $(get_os_name) in
|
|
|
|
"ubuntu"|"debian" )
|
2023-01-06 16:14:41 +08:00
|
|
|
$SUDOE "$DOTFILES/tools/ubuntu.sh" apt-install
|
|
|
|
ret=$?
|
2022-12-03 20:37:20 +08:00
|
|
|
;;
|
|
|
|
"alpine" )
|
2023-01-06 16:14:41 +08:00
|
|
|
$SUDOE "$DOTFILES/tools/alpine.sh" apk-add
|
|
|
|
ret=$?
|
2022-11-08 22:58:17 +08:00
|
|
|
;;
|
|
|
|
"macos" )
|
2022-12-03 21:27:22 +08:00
|
|
|
"$DOTFILES/tools/macos.sh" brew-install
|
2023-01-06 16:14:41 +08:00
|
|
|
ret=$?
|
2022-11-08 22:58:17 +08:00
|
|
|
;;
|
|
|
|
"msys" )
|
2022-12-03 21:27:22 +08:00
|
|
|
"$DOTFILES/tools/msys2.sh" pacman-S
|
2023-01-06 16:14:41 +08:00
|
|
|
ret=$?
|
2022-11-08 22:58:17 +08:00
|
|
|
;;
|
2023-01-06 16:14:41 +08:00
|
|
|
* ) fmt_error "dfs auto-install is not implemented on OS: $(get_os_name). skipping ..."
|
2022-11-08 22:58:17 +08:00
|
|
|
esac
|
2023-01-06 16:14:41 +08:00
|
|
|
set -e
|
|
|
|
if [[ "$ret" != "0" ]]; then
|
|
|
|
fmt_error "failed to install dependencies."
|
|
|
|
fi
|
2022-11-08 22:58:17 +08:00
|
|
|
}
|
2022-11-05 19:45:21 +08:00
|
|
|
|
|
|
|
preinstall_check()
|
|
|
|
{
|
2022-11-08 22:58:17 +08:00
|
|
|
fmt_note "checking requirements ..."
|
2022-12-03 20:37:20 +08:00
|
|
|
local mandatory_commands=( "git" "zsh" "curl" "grep" "cat" "cp" "bash" "mkdir" )
|
|
|
|
local optional_commands=( "vim" "tmux" "ping" )
|
2022-11-05 19:45:21 +08:00
|
|
|
for i in "${mandatory_commands[@]}"; do
|
2022-12-03 20:37:20 +08:00
|
|
|
if ! command -v $i 1>/dev/null; then
|
2022-11-05 19:45:21 +08:00
|
|
|
fmt_info "all this utils are required: ${mandatory_commands[@]}"
|
|
|
|
fmt_info "install them manually or check scripts in tools/"
|
|
|
|
fmt_fatal "\"$i\" not found. aborting ..."
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
for i in "${optional_commands[@]}"; do
|
2022-12-03 20:37:20 +08:00
|
|
|
if ! command -v $i 1>/dev/null; then
|
2022-11-05 19:45:21 +08:00
|
|
|
fmt_warning "\"$i\" not found"
|
2023-01-06 16:14:41 +08:00
|
|
|
yn=$(ask_for_Yn "continue anyway?")
|
|
|
|
if [[ "$yn" == "0" ]]; then
|
2022-11-05 19:45:21 +08:00
|
|
|
fmt_info "all this utils are suggested: ${optional_commands[@]}"
|
|
|
|
fmt_info "install them manually or check scripts in tools/"
|
|
|
|
fmt_fatal "aborting ..."
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
2022-05-12 19:29:49 +08:00
|
|
|
|
2023-01-06 16:14:41 +08:00
|
|
|
prepare_config()
|
|
|
|
{
|
|
|
|
local remote=$(cd "$DOTFILES" && git remote get-url origin)
|
|
|
|
if [[ -z "$DFS_NO_WALL" && $remote == *github* ]]; then
|
|
|
|
DFS_CONFIGS+=("DFS_NO_WALL=1")
|
|
|
|
fi
|
|
|
|
if [[ ${#DFS_CONFIGS[@]} == 0 ]]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
fmt_note "preparing dotfiles configurations ..."
|
|
|
|
local key value
|
|
|
|
for i in "${DFS_CONFIGS[@]}"; do
|
|
|
|
if [[ "$i" == *"="* ]]; then
|
|
|
|
key=${i%%=*}
|
|
|
|
value=${i#*=}
|
|
|
|
else
|
|
|
|
key=$i
|
|
|
|
value=$(eval echo \$$key)
|
|
|
|
fi
|
|
|
|
HOME_FILES_PATH+=(".config/dotfiles/env")
|
|
|
|
HOME_FILES_CONTENT+=("$key=$value")
|
|
|
|
echo -n "$key=$value "
|
|
|
|
export $key=$value
|
|
|
|
done
|
|
|
|
echo
|
|
|
|
}
|
|
|
|
|
2023-04-21 12:37:05 +08:00
|
|
|
append_hist()
|
|
|
|
{
|
|
|
|
fmt_note "appending zsh history ..."
|
|
|
|
"$DOTFILES/tools/append_zsh_hist.sh" "$@"
|
|
|
|
}
|
|
|
|
|
2022-11-05 19:45:21 +08:00
|
|
|
install_file_content()
|
2022-05-12 19:29:49 +08:00
|
|
|
{
|
2022-11-08 22:58:17 +08:00
|
|
|
fmt_note "installing file content ..."
|
2022-11-05 19:45:21 +08:00
|
|
|
for ((i=0; i<${#HOME_FILES_PATH[@]}; i++)); do
|
|
|
|
local filename="$HOME/${HOME_FILES_PATH[$i]}"
|
2023-01-06 16:14:41 +08:00
|
|
|
local content="${HOME_FILES_CONTENT[$i]}"
|
2022-11-08 22:58:17 +08:00
|
|
|
fmt_info "installing \"$content\" into \"$filename\" ..."
|
2022-11-05 19:45:21 +08:00
|
|
|
mkdir -p $(dirname "$filename")
|
|
|
|
if [ ! -f "$filename" ]; then
|
|
|
|
touch $filename
|
|
|
|
fi
|
|
|
|
grep -qxF -- "$content" "$filename" || echo "$content" >> "$filename"
|
|
|
|
done
|
2022-05-12 19:29:49 +08:00
|
|
|
}
|
|
|
|
|
2022-11-05 19:45:21 +08:00
|
|
|
uninstall_file_content()
|
2022-05-15 12:55:50 +08:00
|
|
|
{
|
2022-11-08 22:58:17 +08:00
|
|
|
fmt_note "uninstalling file content ..."
|
2022-11-05 19:45:21 +08:00
|
|
|
for ((i=0; i<${#HOME_FILES_PATH[@]}; i++)); do
|
|
|
|
local filename="$HOME/${HOME_FILES_PATH[$i]}"
|
|
|
|
local content=${HOME_FILES_CONTENT[$i]}
|
2022-11-08 22:58:17 +08:00
|
|
|
fmt_info "removing \"$content\" from \"$filename\" ..."
|
2022-11-05 19:45:21 +08:00
|
|
|
if [ -f "$filename" ]; then
|
|
|
|
grep -vxF -- "$content" "$filename" | tee "$filename"
|
|
|
|
fi
|
|
|
|
done
|
2022-05-15 12:55:50 +08:00
|
|
|
}
|
|
|
|
|
2022-11-05 19:45:21 +08:00
|
|
|
install_symlink()
|
2022-05-12 19:29:49 +08:00
|
|
|
{
|
2022-11-08 22:58:17 +08:00
|
|
|
fmt_note "installing symlinks ..."
|
2022-11-05 19:45:21 +08:00
|
|
|
for ((i=0; i<${#HOME_SYMLINKS_SRC[@]}; i++)); do
|
|
|
|
local src="$DOTFILES/${HOME_SYMLINKS_SRC[$i]}"
|
|
|
|
local dst="$HOME/${HOME_SYMLINKS_DST[$i]}"
|
2022-11-08 22:58:17 +08:00
|
|
|
fmt_info "creating symlink \"$dst\" --> \"$src\" ..."
|
2022-11-05 19:45:21 +08:00
|
|
|
if [ ! -f "$src" ]; then
|
|
|
|
fmt_error "\"$src\" does not exist! aborting this job ..."
|
|
|
|
continue
|
2022-05-17 18:22:43 +08:00
|
|
|
fi
|
2022-11-05 19:45:21 +08:00
|
|
|
mkdir -p $(dirname "$dst")
|
|
|
|
if [ -f "$dst" ]; then
|
|
|
|
if [ "$(readlink $dst)" -ef "$src" ]; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
fmt_warning "\"$dst\" already exists! stat output:"
|
|
|
|
echo ----------
|
|
|
|
stat $dst
|
|
|
|
echo ----------
|
2023-01-06 16:14:41 +08:00
|
|
|
yn=$(ask_for_yN "would you like to replace ${dst}?")
|
|
|
|
if [[ "$yn" == "1" ]]; then
|
2022-11-05 19:45:21 +08:00
|
|
|
rm $dst
|
|
|
|
else
|
|
|
|
fmt_error "aborting this job ..."
|
|
|
|
continue
|
|
|
|
fi
|
2022-05-12 19:29:49 +08:00
|
|
|
fi
|
2022-11-05 19:45:21 +08:00
|
|
|
ln -s $src $dst
|
|
|
|
done
|
2022-05-12 19:29:49 +08:00
|
|
|
}
|
|
|
|
|
2022-11-05 19:45:21 +08:00
|
|
|
uninstall_symlink()
|
2022-05-15 12:55:50 +08:00
|
|
|
{
|
2022-11-08 22:58:17 +08:00
|
|
|
fmt_note "uninstalling symlinks ..."
|
2022-11-05 19:45:21 +08:00
|
|
|
local src
|
|
|
|
for src in "${!HOME_SYMLINKS[@]}"; do
|
|
|
|
local dst=${HOME_SYMLINKS[$src]}
|
|
|
|
src="$DOTFILES/$src"
|
|
|
|
dst="$HOME/$dst"
|
|
|
|
if [ "$(readlink $dst)" -ef "$src" ]; then
|
2022-11-08 22:58:17 +08:00
|
|
|
fmt_info "removing symlink \"$dst\" ..."
|
2022-11-05 19:45:21 +08:00
|
|
|
echo ----------
|
|
|
|
stat $dst
|
|
|
|
echo ----------
|
|
|
|
rm $dst
|
|
|
|
fi
|
|
|
|
done
|
2022-05-15 12:55:50 +08:00
|
|
|
}
|
|
|
|
|
2022-12-03 20:37:20 +08:00
|
|
|
install_crontab()
|
|
|
|
{
|
2022-11-05 19:45:21 +08:00
|
|
|
if [[ -x $(command -v crontab) ]]; then
|
|
|
|
fmt_note "installing \"$CRON_JOB\" to crontab ..."
|
2023-06-20 15:10:59 +08:00
|
|
|
if ! crontab -l 1>/dev/null 2>&1; then
|
|
|
|
echo -n | crontab -
|
|
|
|
fi
|
2022-11-05 19:45:21 +08:00
|
|
|
( crontab -l | grep -vxF "${CRON_JOB}" | grep -v "no crontab for"; echo "$CRON_JOB" ) | crontab -
|
|
|
|
else
|
|
|
|
fmt_warning "crontab does not exist. skipping ..."
|
|
|
|
fi
|
2022-05-12 19:29:49 +08:00
|
|
|
}
|
|
|
|
|
2022-12-03 20:37:20 +08:00
|
|
|
uninstall_crontab()
|
|
|
|
{
|
2022-11-05 19:45:21 +08:00
|
|
|
if [[ -x $(command -v crontab) ]]; then
|
|
|
|
fmt_note "removing \"$CRON_JOB\" from crontab ..."
|
|
|
|
( crontab -l | grep -vxF "$CRON_JOB" ) | crontab -
|
|
|
|
else
|
|
|
|
fmt_note "crontab does not exist. skipping ..."
|
|
|
|
fi
|
2022-05-12 19:29:49 +08:00
|
|
|
}
|
|
|
|
|
2022-12-03 20:37:20 +08:00
|
|
|
install_tmux_tpm()
|
|
|
|
{
|
2022-05-21 01:59:56 +08:00
|
|
|
TMUX_TPM="$HOME/.tmux/plugins/tpm"
|
2022-05-21 02:57:20 +08:00
|
|
|
if [[ -x $(command -v tmux) && ! -d "$TMUX_TPM" ]]; then
|
2022-05-21 01:59:56 +08:00
|
|
|
fmt_note "installing tmux tpm ..."
|
2022-05-22 18:07:50 +08:00
|
|
|
git clone https://gitee.com/dictxiong/tpm "$TMUX_TPM"
|
2022-05-22 00:53:55 +08:00
|
|
|
if [[ -x $(command -v g++) && -x $(command -v cmake) && -x $(command -v make) ]]; then
|
2022-11-09 23:19:55 +08:00
|
|
|
if [[ -z "$DFS_LITE" || "$DFS_LITE" == "0" ]]; then
|
|
|
|
fmt_note "initializing tmux plugins ..."
|
2022-08-28 20:51:44 +08:00
|
|
|
~/.tmux/plugins/tpm/bin/install_plugins
|
2022-11-09 23:19:55 +08:00
|
|
|
else
|
|
|
|
fmt_warning "in lite mode, tmux plugins are downloaded but not complied"
|
|
|
|
fmt_info "try <prefix + I> or ~/.tmux/plugins/tpm/bin/install_plugins to complie manually"
|
2022-08-28 20:51:44 +08:00
|
|
|
fi
|
2022-05-21 01:59:56 +08:00
|
|
|
else
|
2022-05-22 00:53:55 +08:00
|
|
|
fmt_warning "pls install g++,cmake,make and then init tmux plugins by <prefix + I> or ~/.tmux/plugins/tpm/bin/install_plugins"
|
2022-05-21 01:59:56 +08:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-12-03 20:37:20 +08:00
|
|
|
install_vim_vundle()
|
|
|
|
{
|
2022-05-21 01:59:56 +08:00
|
|
|
VIM_VUNDLE="$HOME/.vim/bundle/Vundle.vim"
|
2022-05-21 02:57:20 +08:00
|
|
|
if [[ -x $(command -v vim) && ! -d "$VIM_VUNDLE" ]]; then
|
2022-05-21 01:59:56 +08:00
|
|
|
fmt_note "installing vim vundle ..."
|
2022-05-22 18:07:50 +08:00
|
|
|
git clone https://gitee.com/dictxiong/Vundle.vim "$VIM_VUNDLE"
|
2022-05-21 01:59:56 +08:00
|
|
|
fmt_note "initializing vim plugins ..."
|
2022-11-06 14:00:25 +08:00
|
|
|
echo | vim +PluginInstall +qall
|
2022-05-21 01:59:56 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-12-03 20:37:20 +08:00
|
|
|
install_update()
|
|
|
|
{
|
2022-08-14 20:34:18 +08:00
|
|
|
fmt_note "installing update.sh ..."
|
2022-08-26 20:43:00 +08:00
|
|
|
cp "${DOTFILES}/.update.sh" "${DOTFILES}/update.sh"
|
|
|
|
chmod +x "${DOTFILES}/update.sh"
|
2022-11-05 19:45:21 +08:00
|
|
|
fmt_note "running update.sh ..."
|
2022-12-10 19:11:05 +08:00
|
|
|
set +e
|
2022-12-03 20:37:20 +08:00
|
|
|
DFS_UPDATED_RET=85 ${DOTFILES}/update.sh
|
2022-12-10 19:11:05 +08:00
|
|
|
RET=$?
|
|
|
|
if [[ $RET == 85 ]]; then
|
2022-11-12 20:05:29 +08:00
|
|
|
fmt_note "dfs updated. re-running install.sh ..."
|
2023-04-21 12:37:05 +08:00
|
|
|
"${DOTFILES}/install.sh" "${ORIGIN_ARGS[@]}" && exit
|
2022-12-10 19:11:05 +08:00
|
|
|
elif [[ $RET != 0 ]]; then
|
|
|
|
fmt_fatal "update.sh failed with exit code $RET"
|
2022-11-12 20:05:29 +08:00
|
|
|
fi
|
2022-12-10 19:11:05 +08:00
|
|
|
set -e
|
2022-08-14 20:34:18 +08:00
|
|
|
}
|
|
|
|
|
2022-12-03 20:37:20 +08:00
|
|
|
uninstall_update()
|
|
|
|
{
|
2022-08-14 20:34:18 +08:00
|
|
|
fmt_note "removing update.sh ..."
|
2022-08-26 20:43:00 +08:00
|
|
|
rm "${DOTFILES}/update.sh"
|
2022-08-14 20:34:18 +08:00
|
|
|
}
|
|
|
|
|
2022-12-03 20:37:20 +08:00
|
|
|
install()
|
|
|
|
{
|
2022-11-24 20:51:35 +08:00
|
|
|
if [[ "$INSTALL_DEP" == "1" ]]; then install_dependencies; fi
|
2023-04-21 12:37:05 +08:00
|
|
|
prepare_config
|
2022-12-03 20:37:20 +08:00
|
|
|
install_update
|
2022-11-12 20:05:29 +08:00
|
|
|
preinstall_check
|
2022-05-12 19:29:49 +08:00
|
|
|
install_crontab
|
2022-11-05 19:45:21 +08:00
|
|
|
install_file_content
|
|
|
|
install_symlink
|
2022-12-03 20:37:20 +08:00
|
|
|
apost_beacon "dfs.installed"
|
2022-05-21 01:59:56 +08:00
|
|
|
# those that won't be uninstalled in the future
|
|
|
|
install_tmux_tpm
|
|
|
|
install_vim_vundle
|
2023-04-21 12:37:05 +08:00
|
|
|
if [[ -n "$DFS_HIST" ]]; then append_hist "$DFS_HIST"; fi
|
2022-05-15 12:55:50 +08:00
|
|
|
fmt_note "done installing!"
|
|
|
|
}
|
|
|
|
|
2022-12-03 20:37:20 +08:00
|
|
|
uninstall()
|
|
|
|
{
|
2023-01-06 16:14:41 +08:00
|
|
|
yn=$(ask_for_yN "do you really want to uninstall?")
|
|
|
|
if [[ "$yn" != "1" ]]; then
|
|
|
|
fmt_fatal "aborting this job ..."
|
2022-05-15 13:18:42 +08:00
|
|
|
fi
|
2022-11-05 19:45:21 +08:00
|
|
|
uninstall_update
|
|
|
|
uninstall_crontab
|
|
|
|
uninstall_file_content
|
|
|
|
uninstall_symlink
|
2022-12-03 20:37:20 +08:00
|
|
|
apost_beacon "dfs.uninstalled"
|
2022-11-05 19:45:21 +08:00
|
|
|
fmt_note "done uninstalling!"
|
2022-05-12 19:29:49 +08:00
|
|
|
}
|
|
|
|
|
2023-04-21 12:37:05 +08:00
|
|
|
echo "this is the dotfiles installer, version $(cd "$DOTFILES" && git describe --tags --always --dirty)"
|
|
|
|
echo "install options:" "${GOT_OPTS[@]}"
|
2022-11-17 20:02:35 +08:00
|
|
|
FUNC=install
|
2022-11-24 20:51:35 +08:00
|
|
|
INSTALL_DEP=0
|
2023-01-06 16:14:41 +08:00
|
|
|
store_config=0
|
2023-04-21 12:37:05 +08:00
|
|
|
store_hist=0
|
2023-01-06 16:14:41 +08:00
|
|
|
for i in ${GOT_OPTS[@]}; do
|
|
|
|
if [[ "$store_config" == "1" ]]; then
|
|
|
|
store_config=0
|
|
|
|
DFS_CONFIGS+=("$i")
|
|
|
|
continue
|
|
|
|
fi
|
2023-04-21 12:37:05 +08:00
|
|
|
if [[ "$store_hist" == "1" ]]; then
|
|
|
|
store_hist=0
|
|
|
|
DFS_HIST=$i
|
|
|
|
continue
|
|
|
|
fi
|
2022-11-17 20:02:35 +08:00
|
|
|
case $i in
|
|
|
|
-i ) FUNC=install ;;
|
|
|
|
-r ) FUNC=uninstall ;;
|
2022-11-24 20:51:35 +08:00
|
|
|
-a|--auto ) INSTALL_DEP=1 ;;
|
2023-04-21 12:37:05 +08:00
|
|
|
-H|--hist|--history ) store_hist=1 ;;
|
2023-01-06 16:14:41 +08:00
|
|
|
-x ) store_config=1 ;;
|
|
|
|
* ) fmt_fatal "unknown option \"$i\"" ;;
|
2022-11-05 19:45:21 +08:00
|
|
|
esac
|
|
|
|
done
|
2022-11-17 20:02:35 +08:00
|
|
|
$FUNC
|