summaryrefslogtreecommitdiff
path: root/bin/insinuate-dotfiles
blob: a061af511206b3b29676a016a6b66df3e0206596 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/sh

# insinuate-dotfiles -- try to get my dotfiles onto a host, securely

# Copyright (C) 2017-2019, 2023 Sean Whitton
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.



# Replacement for COM.SILENTFLAME.CONSFIG::DOTFILES-INSINUATED for when I
# can't access my consfig locally.  For tightly space-constrained hosts, we
# might want a feature to just copy over .bashrc, .inputrc, .profile, .shinit,
# .emacs.d and maybe more, overwriting what's already there.
#
# A very slow alternative to --update, for hosts without Git, is to mount with
# sshfs and then can use the local installation of Git to update.

set -e

getopt=$(getopt -s bash -o "" -n insinuate-dotfiles -l update -- "$@")
eval set -- "$getopt"
update=false
while true; do
    case "$1" in
	--update) update=true; shift; continue ;;
	--) shift; break ;;
    esac
done
[ $# -eq 1 ] || { echo >&2 "usage: insinuate-dotfiles [--update] [USER@]HOST"
		  exit 1; }
host=$1

# assume connection sharing is set up in ~/.ssh/config
ssh -fN "$host"

if ssh "$host" "test -d src/dotfiles"; then
    if $update \
	    || { mr_out="$(ssh -t "$host" \
	       	 	       src/dotfiles/bin/mr -m -d src/dotfiles status)"
		 mr_exit=$?
		 [ $mr_exit -eq 0 -a -z "$mr_out" ]; }; then
	ssh "$host" \
	    "rm -rf src/dotfiles/* src/dotfiles/.[!.]* src/dotfiles/..?*"
    else
	printf >&2 "%s\n\n" "$mr_out"
	if [ $mr_exit -eq 0 ]; then
	    echo >&2 "src/dotfiles exists;"\
		 "run again with --update to replace contents"
	else
	    echo >&2 "remote 'mr status' for src/dotfiles exited non-zero"
	fi
	exit 1
    fi
else
    ssh "$host" "mkdir -p src/dotfiles"
fi

temp="$(mktemp -d -p $HOME/tmp insinuate.XXX)"
git clone --no-hardlinks --depth 1 -o local -b master \
    "file://$HOME/src/dotfiles" "$temp"
( cd "$temp" && git remote rm local && rm -rf .git/refs/remotes/local )
remote_uid="$(ssh "$host" id -u)"
main_opts="$([ "$remote_uid" = 0 ] && echo ozxf || echo zxf)"
tar -C"$temp/.." -zcf - "$(basename $temp)" \
    | ssh "$host" 'tar --strip-components=1 -Csrc/dotfiles -'$main_opts' -'
rm -rf "$temp"

# stow dotfiles into $HOME
ssh "$host" "sh src/dotfiles/bin/bstraph"

# copy my gpg key over there so I can use `mr up` to update dotfiles
if ssh "$host" command -v gpg >/dev/null; then
    gpg --export-options export-minimal --export spwhitton@spwhitton.name \
        | ssh "$host" gpg --import
fi