#!/usr/bin/env bash # Copyright 2016 Luke Shumaker # This work is free. You can redistribute it and/or modify it under the # terms of the Do What The Fuck You Want To Public License, Version 2, # as published by Sam Hocevar. See the COPYING file for more details. # The user should not call this script directly. declare -r workdir=/var/lib/pristine-etc pacman-etc-name-ver() { LC_ALL=C pacman -Qo /etc | sed 's|^/etc/ is owned by ||' } pacman-all-name-arch() { LC_ALL=C pacman -Qni | tr $'\n' $'\r' | sed 's/\r\r/\n/g' | sed -r 's|(.*\r)?Name\s*:\s*(\S+)(\r.*)?\rArchitecture\s*:\s*(\S+)\r.*|\2 \4|' } commit() ( local msg="$1" cd "$workdir" if ! [[ -d etc.git ]]; then mkdir -p etc (cd etc && etckeeper init -d "$PWD") mv etc/.git etc.git ln -sr etc.git etc/.git fi rm -rf etc/ local err=false local files=() while IFS=' ' read -r pkgname pkgver arch; do local file=("/var/cache/pacman/pkg/$pkgname-$pkgver-$arch".pkg.tar.*) if ! test -f "$file"; then printf "ERROR: no cached package for %s %s %s\n" "$pkgname" "$pkgver" "$arch" err=true fi files+=("$file") done < <(join <(pacman-etc-name-ver|sort) <(pacman-all-name-arch|sort)) if $err; then return 1 fi local file for file in "${files[@]}"; do printf " -> %s\n" "$file" bsdtar xpvf "$file" etc done ln -sr etc.git etc/.git cd etc/ etckeeper update-ignore -d "$PWD" if etckeeper unclean -d "$PWD"; then etckeeper commit -d "$PWD" "$msg" fi ) pull() ( cd /etc git remote add pristine "${workdir}/etc" &>/dev/null || true git fetch pristine ) lock() { local fd=$1 local file=$2 eval "exec $fd>"'"$file"' flock "${@:3}" "$fd" } unlock() { local fd=$1 exec {fd}>&- } main() { set -e -o pipefail umask 0022 if ! lock 7 "${workdir}/etc.lock" -n; then return 0 fi while true; do lock 8 "${workdir}/spool.lock" if ! [[ -f "${workdir}/spool" ]]; then return 0 fi msg="$(cat "${workdir}/spool")" rm -f "${workdir}/spool" unlock 8 commit "$msg" pull done } main "$@"