add the functions needed for some keybindings
[zsh.git] / .zsh / functions / dist-config
1 # -*- sh -*-
2
3 autoload colors ; colors
4
5 plog() {
6 if [ $1 = "OK" ]; then
7 print "$fg[cyan]OK.${reset_color}"
8 else
9 print -n "$fg[green]$@...${reset_color} "
10 fi
11 }
12
13 # We can be executed to install ourself to the final destination
14 if [[ $1 == "MAGIC" ]]; then
15 local anon=${2:-"true"}
16 plog "Installing basic dotfile support"
17
18 mkdir -p ${HOME}/bin
19 mkdir -p ${HOME}/.certs
20 for file in mr vcsh; do
21 cp bin/${file} ${HOME}/bin/${file}
22 done
23 cp .certs/ganneff.pem ${HOME}/.certs/ganneff.pem
24 plog "OK"
25 PATH=$HOME/bin:$PATH
26 answer="y"
27 if [[ -d "${HOME}/.config/vcsh/" ]]; then
28 answer="N"
29 echo "$fg[blue]Looks like there is a vcsh controlled config already."
30 echo "I can get rid of it and all its files before fetching the new one.${reset_color}"
31 echo "$fg[red]This will move ALL files from ANY vcsh controlled repository into '~/.oldhome'!${reset_color}"
32 vared -p "Want to do this it? [N/y] " answer
33 case ${answer} in
34 y|Y)
35 # Replace, get rid of old
36 plog "Moving files from vcsh"
37 mkdir ${HOME}/.oldhome
38 for delfile in $(vcsh list-tracked); do
39 dirpart=${delfile:h}
40 mkdir -p "${HOME}/.oldhome/${dirpart}"
41 mv --target-directory="${HOME}/.oldhome/${dirpart}" ${HOME}/${delfile}
42 done
43 plog "OK"
44 mkdir -p "${HOME}/.oldhome/.config/"
45 mv --target-directory="${HOME}/.oldhome" "${HOME}/.config/vcsh/"
46 mv --target-directory="${HOME}/.oldhome" "${HOME}/.config/mr/"
47 mv --target-directory="${HOME}/.oldhome" "${HOME}/.mrconfig"
48 plog "OK"
49 answer="y"
50 read
51 ;;
52 n|N)
53 plog "Not deleting old vcsh directories, trying to continue with existing stuff\n"
54 ;;
55 *)
56 echo "Silly?"
57 return
58 esac
59 fi
60 if [[ $answer = "y" ]]; then
61 plog "Fetching mr config "
62 if [[ $anon = "true" ]]; then
63 plog "using the anonymous version\n"
64 GIT_SSL_CAINFO=${HOME}/.certs/ganneff.pem vcsh clone https://kosh.ganneff.de/git/mr.git
65 cd ${HOME}/.config/mr/config.d
66 for file in *.vcsh; do
67 rm ${file}
68 ln -s ../available.d/${file}-anon
69 done
70 plog "OK"
71 else
72 plog "using the ssh version\n"
73 vcsh clone git@git.ganneff.de:mr.git
74 plog "OK"
75 fi
76 fi
77 plog "Pulling all dotfile repositories\n"
78 cd ${HOME}
79 # The GIT_SSL_CAINFO variable isn't used when using the ssh version.
80 # But it also doesn't hurt, so leave it in, less code than an if clause
81 GIT_SSL_CAINFO=${HOME}/.certs/ganneff.pem mr update
82 plog "OK"
83 if [[ $anon = "true" ]]; then
84 plog "Setup ssl certs for all dotfile repositories\n"
85 mr run git config --local --add http.sslCAInfo ${HOME}/.certs/ganneff.pem
86 plog "OK"
87 fi
88 plog "Hard resetting dotfiles to ensure all files are there and the VCS versions\n"
89 mr run git reset --hard
90 plog "OK"
91
92 plog "Now sourcing parts of the new zsh config"
93 # Shut up "helper" function
94 function debug() {}
95 source ${HOME}/.zsh/zshenv.home
96 source ${HOME}/.zsh/00_Basics.zsh
97 source ${HOME}/.zsh/30_Options.zsh
98 autoload -U udh
99 plog "OK"
100 mkdir ${HOME}/tmp
101 plog "Running udh to setup the rest\n"
102 udh
103
104 unfunction plog
105 return
106 fi
107
108 {
109 # install my dotfiles in a remote host.
110 # needs makeself locally and git remotely.
111 local remote
112 local work
113 local start
114 remote=$1
115 anon=${2:-"true"}
116 work=$(mktemp -d)
117 start=$(pwd)
118 {
119 plog "Building archive..."
120 mkdir -p ${work}/bin
121 cp $(which mr) ${work}/bin
122 cp $(which vcsh) ${work}/bin
123 mkdir -p ${work}/.certs
124 cp ~/.TinyCA/Ganneff_CA/cacert.pem ${work}/.certs/ganneff.pem
125 cp ~/.zsh/functions/dist-config ${work}/dist-config
126 plog "OK"
127 plog "Building installer..."
128 makeself --gzip $work ${TMPDIR}/dotfiles-install.sh \
129 "$USER dotfiles" zsh ./dist-config MAGIC ${anon}
130 plog "OK"
131 [[ -z $1 ]] || {
132 plog "Remote install..."
133 scp $TMPDIR/dotfiles-install.sh ${remote}:
134 ssh -t $remote sh ./dotfiles-install.sh
135 plog "OK"
136 }
137 } always {
138 # Always clean up behind us
139 rm -rf $work
140 cd $start
141 unfunction plog
142 }
143 }