diff options
1 files changed, 115 insertions, 1 deletions
| -rwxr-xr-x | make | 116 |
@@ -19,10 +19,13 @@ then exit 1 fi +no_overwrite= # __merge_link <.FILES_DIR> <HOME_DIR> __merge_link() { while read -r f do + [ -z "$f" ] && continue + # Output file doesn't exist -> Create link # This is also true on broken symlinks if [ ! -e "$2/$f" ] @@ -48,6 +51,12 @@ __merge_link() { # replace output file with (link to) current if ! cmp -s "$1/$f" "$2/$f" then + if [ -n "$no_overwrite" ] + then + echo "Warn: '$2/$f' is different from '$1/$f', skipping (./make safe_link is being executed)!" >/dev/stderr + continue + fi + if [ -h "$2/$f" ] then real="$(readlink "$2/$f")" @@ -85,6 +94,8 @@ __merge_link() { mkdir "$2/$f" while read -r ff do + [ -z "$ff" ] && continue + ln -s "$real/$ff" "$2/$f/$ff" done <<FILES $(ls -A1 "$real") @@ -122,6 +133,8 @@ link() { __remove_link() { while read -r f do + [ -z "$f" ] && continue + if [ -h "$2/$f" ] then real="$(realpath "$2/$f")" @@ -150,6 +163,8 @@ FILES # cannot be merged with the loop above (remove dirs on backtracking) while read -r f do + [ -z "$f" ] && continue + # Output file exists and is an empty directory -> Remove it # We're assuming this directory was created by the script and/or the user # will not care for empty dirs @@ -177,7 +192,106 @@ unlink() { done <"./SYSTEM/$HOSTNAME" } +import() { + dir="${2%%*/}" + shift; shift + mkdir -p "./$dir" + + for arg in "$@" + do + [ -z "$arg" ] && continue + + file="$(realpath "$arg")" + if [ "${file#$HOME}" = "$file" ] + then + echo "'$file' is not under '$HOME'! Can only import from home directory!" >/dev/stderr + continue + fi + + if [ ! -f "$file" -a ! -d "$file" ] + then + echo "'$file' must be either a normal file or directory!" >/dev/stderr + continue + fi + + file="${file#$HOME}" + file="${file#/}" + file_dir="${file%/*}" + [ "$file_dir" = "$file" ] && file_dir= + + mkdir -p "./$dir/$file_dir" + mv "$HOME/$file" "./$dir/$file_dir/" + ln -s "$PWD/$dir/$file" "$HOME/$file" + + echo "Imported '$HOME/$file' under '$dir'" + done +} + +export_() { + shift + + for arg in "$@" + do + [ -z "$arg" ] && continue + + file="$(realpath "$arg")" + if [ "${file#$PWD}" = "$file" ] + then + echo "'$file' is not under '$PWD'! Can only export from .files directory!" >/dev/stderr + continue + fi + + if [ -f "$file" ] + then + home_file="${file#$PWD}" # Remove .files path + home_file="${home_file#/}" # Remove leading /, if it exists + home_file="${home_file#*/}" # Remove directory under .files + + home_dir="$(realpath "$HOME/${home_file%/*}")" + + if [ "${home_dir#$PWD}" != "$home_dir" ] + then + echo "'$home_dir is under '$PWD'! Refusing to export dir to .dirs!" >/dev/stderr + continue + fi + mkdir -p "$home_dir" + + home_file="$HOME/$home_file" + if [ -h "$home_file" ] + then + rm "$home_file" + fi + + # Output file exists and has different content -> swap the two files + # With git it's easy to find the differences and accept/discard them + # This makes things more annoying since to accept changes (after export), + # you'll need to export again (swap back), import and then export. + # Declining changes requires a git discard, which is more ok. + if [ -f "$home_file" ] && ! cmp -s "$file" "$home_file" + then + echo "Warn: '$home_file' is different from '$file'! Swapping the contents of the files!" >/dev/stderr + mv "$home_file" "__.files_tmp.$$" + mv "$file" "$home_file" + mv "__.files_tmp.$$" "$file" + else + cp "$file" "$home_file" + fi + + echo "Exported '$file' to '$home_file'" + elif [ -d "$file" ] + then + # TODO: Recursively traverse output directories and copy when you can + echo "Directories not currently supported!" >/dev/stderr + else + echo "'$file' must be either a normal file or directory!" >/dev/stderr + fi + done +} + case "$1" in + (import) import "$@" ;; + (export) export_ "$@" ;; (unlink) unlink ;; - (link|*) link ;; + (link) link ;; + (safe_link|sink|*) no_overwrite=yes; link ;; esac |
