install: add support for machine specific dotfiles

master
Taeyeon Mori 5 years ago
parent 127fcd6f3a
commit c1e8536ebd
  1. 4
      .gitignore
  2. 112
      install

4
.gitignore vendored

@ -1,5 +1,7 @@
/zsh/.* /zsh/.*
user-info /etc/user-info
/git/user-info
/etc/hostname
*.local *.local
__pycache__ __pycache__
*.pyc *.pyc

@ -59,7 +59,7 @@ function relink() {
[[ ! -e "$1" ]] && err "No such file or directory: '$1'" && return 1 [[ ! -e "$1" ]] && err "No such file or directory: '$1'" && return 1
[[ -L "$2" ]] && rm -f "$2" [[ -L "$2" ]] && rm -f "$2"
[[ -e "$2" ]] && ! $OVERWRITE && err "File already exists and isn't a symbolic link: '$2'" && return 1 [[ -e "$2" ]] && ! $OVERWRITE && err "File already exists and isn't a symbolic link: '$2'" && return 1
color 31 ln -s "$(relpath "$1" "$(dirname "$2")")" "$2" color 31 ln -sr "$1" "$2"
} }
@ -72,16 +72,22 @@ git submodule update --init --recursive
# Ask questions about user # Ask questions about user
$ASK && msg "Asking questions..." || err "Not asking any questions:" $ASK && msg "Asking questions..." || err "Not asking any questions:"
# Try to load previous configuration
[[ -e "$DOT/etc/user-info" ]] && source "$DOT/etc/user-info" [[ -e "$DOT/etc/user-info" ]] && source "$DOT/etc/user-info"
[[ -e "$DOT/etc/hostname" ]] && HOSTNAME=`cat "$DOT/etc/hostname"`
# Retrieve realname from /etc/passwd GECOS
[[ -z "$REALNAME" ]] && REALNAME=`grep "^$USER:[^:]*:$UID:" /etc/passwd | cut -d: -f5 | cut -d, -f1` [[ -z "$REALNAME" ]] && REALNAME=`grep "^$USER:[^:]*:$UID:" /etc/passwd | cut -d: -f5 | cut -d, -f1`
[[ -z "$HOSTNAME" ]] && HOSTNAME=`hostname`
if $ASK; then if $ASK; then
ask " Real name" REALNAME ask " Real name" REALNAME
ask " E-Mail address" EMAIL ask " E-Mail address" EMAIL
ask " Computer name" HOSTNAME
else else
color 36 echo " default REALNAME=\"$REALNAME\"" color 36 echo " default REALNAME=\"$REALNAME\""
color 36 echo " default EMAIL=\"$EMAIL\"" color 36 echo " default EMAIL=\"$EMAIL\""
color 36 echo " default HOSTNAME=\"$HOSTNAME\""
fi fi
# Write down answers # Write down answers
@ -91,39 +97,101 @@ REALNAME="$REALNAME"
EMAIL="$EMAIL" EMAIL="$EMAIL"
EOF EOF
color 32 echo " Writing etc/hostname"
echo $HOSTNAME >"$DOT/etc/hostname"
# Handle local-only alternatives
HOST_LC=${(L)HOSTNAME}
function refset() {
eval "$1=\$2"
}
function inspect_name() {
# gimme namerefs...
#local -n name=$1 prio=$2
#local filename=$3
# Check local-only files
refset $1 ${3%.local}
if [[ ${(P)1} != $3 ]]; then
refset $2 3
return 0
fi
# Check hostname-specific file
refset $1 ${3%@*}
if [[ ${(P)1} != $3 ]]; then
refset $2 2
[[ ${3##*@} == $HOST_LC ]]
return $?
fi
# Standard file
refset $2 1
return 0
}
function add_names() {
local an_name an_prio
#local -n names=$1 prios=$2
for an_file in ${(@)argv[3,$#]}; do
if inspect_name an_name an_prio $an_file && [[ $an_prio -gt ${${(P)2}[$an_name]} ]]; then
refset $1\[\$an_name\] $an_file
refset $2\[\$an_name\] $an_prio
fi
done
}
# Write files to $HOME # Write files to $HOME
# ============================= # =============================
# Setup dotfiles # Setup dotfiles
msg "Linking dotfiles..." msg "Linking dotfiles..."
for file in "$DOT/dotfiles"/*; do (){
NAM="${file:t}" local -A names prios
color 32 echo " Adding .$NAM" pushd $DOT/dotfiles
relink "$file" "$HOME/.$NAM" add_names names prios *
done for name src in ${(@kv)names}; do
color 32 echo " Adding .$name"
relink $src $HOME/.$name
done
popd
}
# Setup .config # Setup .config
: ${XDG_CONFIG_HOME:=~/.config} : ${XDG_CONFIG_HOME:=~/.config}
msg "Linking .config files... ($XDG_CONFIG_HOME)" msg "Linking .config files... ($XDG_CONFIG_HOME)"
( (){
cd "$DOT/config" function relink_r() {
for dir in $(find . -type d); do local target=$1 dest=$2 name=${3:-$2} from
if [ ! -d "$XDG_CONFIG_HOME/$dir" ]; then [[ ${target:t} != ${dest:t} ]] && from=" (${target:t})"
color 32 echo " Creating ${dir:2}" if [[ ! -d $target ]]; then
mkdir "$XDG_CONFIG_HOME/$dir" color 32 echo " Adding $name$from"
fi if [[ -L $target ]]; then
done cp -P $target $dest
for file in $(find . -type f -o -type l); do else
file="${file:2}" relink $target $dest
color 32 echo " Adding $file" fi
if [[ -L "$file" ]]; then
cp -P "$file" "$XDG_CONFIG_HOME/$file"
else else
relink "$DOT/config/$file" "$XDG_CONFIG_HOME/$file" if [[ ! -d $dest ]]; then
color 32 echo " Creating directory $name/$from"
mkdir $dest
elif [[ -n $from ]]; then
color 32 echo " Adding directory $name/$from"
fi
# This is still slightly ugly
local -A names prios
add_names names prios $target/*
for dst src in ${(@kv)names}; do
relink_r $src $dest/${dst:t} $name/${dst:t}
done
fi fi
}
pushd $DOT/config
local -A names prios
add_names names prios *
for dst src in ${(@kv)names}; do
relink_r $src $XDG_CONFIG_HOME/$dst $dst
done done
) popd
}
# Setup git # Setup git
# ============================= # =============================

Loading…
Cancel
Save