dotfiles/sync.sh

133 lines
2.9 KiB
Bash
Raw Normal View History

2023-10-14 22:38:03 +02:00
#!/bin/sh
2023-10-14 22:38:11 +02:00
osreleaseprop() {
cat /etc/os-release | grep -e "^$1=" | sed 's/ID=//'
}
elevated() {
s=${SUDO:-sudo}
if [ ! "$s" = "none" ]; then
2023-10-14 22:38:11 +02:00
if ! command -v $s &> /dev/null; then
2023-10-14 22:38:11 +02:00
echo "command \"$s\" not found"
exit 1
fi
else
s=""
fi
$s $@
}
2023-10-14 22:38:06 +02:00
2023-10-14 22:38:11 +02:00
DISTRO=$(osreleaseprop ID)
if [ ! -d "distros/$DISTRO" ]; then
echo "$DISTRO is not supported."
exit 1
fi
2023-10-14 22:38:03 +02:00
2023-10-14 22:38:11 +02:00
pm() {
elevated "sh distros/$DISTRO/pm.sh $@"
}
2023-10-14 22:38:07 +02:00
2023-10-14 22:38:11 +02:00
install_package_list() {
pkgs=""
[ -f "distros/${1:-$DISTRO}/package.list" ] || return 1
for pkg in $(cat distros/${1:-$DISTRO}/package.list | sed 's/#.*$//'); do
pkgs="$pkgs $pkg"
done
pm -i $pkgs
}
run_scripts() {
cmd=$1
shift
case $cmd in
sys)
dir="distros/${1:-$DISTRO}/sys-scripts"
[ -d "$dir" ] || return
for s in $(ls $dir); do
echo "# running $dir/$s..."
elevated sh $dir/$s || exit 1
done
;;
user)
dir="distros/${1:-$DISTRO}/user-scripts"
[ -d "$dir" ] || return
for s in $(ls $dir); do
echo "# running $dir/$s..."
sh $dir/$s || exit 1
done
;;
esac
}
overlay() {
[ -d "distros/${1:-$DISTRO}/root" ] || return 0
elevated cp -r distros/${1:-$DISTRO}/root /
}
__step_update() {
echo "# updating system..."
pm -u
}
__step_install_package_list() {
echo "# installing package lists..."
install_package_list any
install_package_list
}
__step_run_system_scripts() {
echo "# running system scripts..."
run_scripts sys any
run_scripts sys
}
__step_run_user_scripts() {
echo "# running user scripts..."
run_scripts user any
run_scripts user
}
__step_overlay() {
echo "# overlaying root directories..."
overlay any
overlay
}
__step_link_home() {
echo "# linking all home files..."
trackingfile=".dotfiles/$(git rev-parse HEAD)-links"
for item in $(find ./home -type f); do
path=$(echo $item | sed "s/\.\/home//")
[ -z "$path" ] && continue
2023-10-14 22:38:11 +02:00
grep -q "$path" $trackingfile &> /dev/null && continue
2023-10-14 22:38:11 +02:00
path="$HOME$path"
mkdir -p $(dirname $path) &> /dev/null
ln $item $path && echo "$path" >> $trackingfile
2023-10-14 22:38:07 +02:00
done
2023-10-14 22:38:03 +02:00
}
2023-10-14 22:38:11 +02:00
step() {
if ! grep -q "executed_step:$1" .dotfiles/pending_sync &> /dev/null; then
$(printf "__step_%s" "$1")
echo "executed_step:$1" >> .dotfiles/pending_sync
fi
}
2023-10-14 22:38:11 +02:00
mkdir -p .dotfiles &> /dev/null
2023-10-14 22:38:11 +02:00
touch .dotfiles/pending_sync
2023-10-14 22:38:07 +02:00
2023-10-14 22:38:11 +02:00
if [ -n "$1" ]; then
2023-10-14 22:38:11 +02:00
step $1
2023-10-14 22:38:11 +02:00
else
2023-10-14 22:38:11 +02:00
echo "started_sync:$(date)" >> .dotfiles/log
step update
step install_package_list
step run_system_scripts
step run_user_scripts
step link_home
step overlay
echo "finished_sync:$(date)" >> .dotfiles/log
2023-10-14 22:38:11 +02:00
fi
2023-10-14 22:38:11 +02:00
unlink .dotfiles/pending_sync