Skip to content

Instantly share code, notes, and snippets.

@zw963
Created April 2, 2022 12:35
Show Gist options
  • Save zw963/703da93a41e8868d3ee7f989782770b5 to your computer and use it in GitHub Desktop.
Save zw963/703da93a41e8868d3ee7f989782770b5 to your computer and use it in GitHub Desktop.
#! /bin/bash
USE_COPY=
function readlink1() {
(cd "$1" && pwd -P)
}
function backup () {
mv $* $*_bak-$(date '+%Y-%m-%d_%H:%M')
}
function safe_ln () {
local dir=$(dirname $2)
local current_path=$(readlink1 .)
if [ $EUID != 0 -a ! -d $dir ]; then
mkdir -p $dir
fi
if [ -d "$dir" ]; then
ln -sfv $current_path/$1 $2
# if [ $EUID == 0 ]; then
# # cp -lRv $1 $2
# cp -Rv $1 $2
# else
# ln -sfv $1 $2
# fi
fi
}
function run_install () {
local config=$1
local prefix=${2%/}
local current_path=$(readlink1 .)
IFS=$'\n'
for i in $config; do
# 确保当前目录存在这个目录名, 可以确保过滤掉所有 # 开头的目录.
if [ -e $current_path/"$i" ]; then
# 如果是一个 broken 的符号链接, 删除它
[ -L $prefix/"$i" -a ! -e $prefix/"$i" ] && rm $prefix/"$i"
# 如果不是一个符号链接(例如: 普通文件或不存在)
if [ ! -L $prefix/"$i" ]; then
# 如果是普通文件(通常是原有的文件), 改名备份之.
[ -e $prefix/"$i" ] && backup $prefix/"$i"
# 无论是否存在, 都应该创建一个符号链接.
safe_ln "$i" $prefix/"$i"
fi
fi
done
}
if [ $EUID == 0 ]; then
run_install "$root_config" '/root/'
run_install "$etc_config" '/'
else
run_install "$current_config" "$HOME/"
run_install "$tmp_config" '/tmp/.tmp-'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment