dotfiles

<-- duh.
git clone https://hhvn.uk/dotfiles
git clone git://hhvn.uk/dotfiles
Log | Files | Refs | Submodules | LICENSE

install.sh (1163B)


      1 #!/bin/sh
      2 # I think a script is more appropriate than a makefile, here
      3 
      4 [ "$USER" = "root" ] && {
      5 	printf 'run this from your user account\n'
      6 	exit 1
      7 }
      8 
      9 # substitute $INSTALL_ENV_FOO for $FOO
     10 _envsubst(){
     11 	str=$(cat | tr '\n' '\f')
     12 
     13 	env |
     14 	while read -r line
     15 	do
     16 		str=$(printf '%s\n' "$str" | awk -v "var=${line%%=*}" -v "val=${line#*=}" '
     17 			{
     18 				regex = sprintf("\\$INSTALL_ENV_%s", var)
     19 				gsub(regex, val)
     20 				print
     21 			}'
     22 		)
     23 		printf "\n%s" "$str"
     24 	done | tail -n 1 | tr '\f' '\n'
     25 	# combine echo/tail here, due to weirdness with while loops
     26 	# appears to be like a subshell, but also not:
     27 	#  - variable persists between each section of a loop
     28 	#  - variable doesn't persist after leaving the loop
     29 }
     30 
     31 dirs=$(find . -mindepth 1 -type d)
     32 files=$(find . -mindepth 2 -type f)
     33 
     34 export IFS=$(printf '\n\t')
     35 for dir in $dirs
     36 do
     37 	printf 'creating %s... ' "$dest"
     38 	dest=$(printf '%s\n' "$dir" | sed 's~^\.~~')
     39 	mkdir -p $dest
     40 	printf 'done\n'
     41 done
     42 
     43 for file in $files
     44 do
     45 	printf 'creating %s... ' "$dest"
     46 	dest=$(printf '%s\n' "$file" | sed 's~^\.~~')
     47 	content=$(_envsubst < "$file")
     48 	printf '%s\n' "$content" | doas tee "$dest" >/dev/null
     49 	printf 'done\n'
     50 done