My build of nnn with minor changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

76 lines
1.5 KiB

  1. #!/usr/bin/env sh
  2. # Description: Backup nnn configuration
  3. # - config dir content
  4. # - environment config
  5. # - shell functions and aliases
  6. #
  7. # Shell: POSIX compliant
  8. # Author: Léo Villeveygoux
  9. nnn_aliases="n nnn"
  10. outdir="nnn-$(whoami)@$(hostname)"
  11. outfile="${outdir}.tar.bz2"
  12. shellname="$(basename "$SHELL")"
  13. conffile="config.txt"
  14. configdir="${XDG_CONFIG_HOME:-$HOME/.config}/nnn"
  15. workdir="$PWD"
  16. tempdir="$(mktemp -d)"
  17. mkdir "$tempdir/$outdir"
  18. if [ ! -d "$tempdir" ]; then
  19. echo "Can't create work directory." >&2
  20. exit 1
  21. fi
  22. cd "$tempdir/$outdir" || exit 1
  23. # Backing up config dir content
  24. cp -r "$configdir" . || exit 1
  25. # Environment config
  26. env | sed "s/'/'\\\\''/" |\
  27. awk '/^NNN_/{print "export '\''"$0"'\''"}' > "$conffile"
  28. # Shell functions/aliases
  29. case "$shellname" in
  30. bash)
  31. for name in $nnn_aliases ; do
  32. if [ "$(bash -ic "type -t $name")" = "function" ] ; then
  33. bash -ic "type $name" | tail -n+2 >> "$conffile"
  34. elif bash -ic "alias $name" >/dev/null 2>&1 ; then
  35. bash -ic "alias $name" >> "$conffile"
  36. fi
  37. done
  38. ;;
  39. zsh)
  40. for name in $nnn_aliases ; do
  41. if zsh -ic "functions $name" ; then
  42. zsh -ic "functions $name" >> "$conffile"
  43. elif zsh -ic "alias $name" ; then
  44. echo alias "$(zsh -ic "alias $name")" >> "$conffile"
  45. fi
  46. done
  47. ;;
  48. *)
  49. echo "Unknown shell, skipping alias/function checking." >&2
  50. ;;
  51. esac
  52. cd .. || exit 1
  53. printf "Saving as '%s' ... " "$workdir/$outfile"
  54. tar caf "$workdir/$outfile" "$outdir" && echo "Done" || echo "Failed"
  55. cd "$workdir" && rm -rf "$tempdir"