|
- #!/bin/sh
-
- #To add more caches, add a newline to this string and follow the existing name:path format
- cache_info="home:$HOME"
- HOME=/home/immanuel
- XDG_CACHE_HOME="$HOME/.cache"
-
- init() {
- mkdir -p $XDG_CACHE_HOME/mru
- file=$XDG_CACHE_HOME/mru/"$1"
- if [ -z "$1" ]; then file="$file""home"; fi
- path=$(printf $cache_info | grep "^$1" - | cut -d: -f2)
-
- if [ -z "$path" -o ! -e "$path" ]; then
- printf "path:$path from cache:$file does not exist\n" 1>&2; exit;
- fi
- find $path -type f -not \( -path '*/.*/*' -o -path '*node_modules/*' -o \
- -path '*Backups/my-plugins*' -o -path '*.sw[po]' \) -printf \
- '%TY-%Tm-%Td\t%TT\t%p\n' | sort -r > $file'.new'
- mv "$file.new" "$file"
- }
-
- get_cache_path() {
- if [ -z "$1" ]; then printf "no arg to get_cache_path\n" 1>&2; exit; fi
- path="$(printf $cache_info | grep "^$1" - | cut -d: -f2)"
- if [ -z "$path" ]; then printf "invalid cache name: $1\n" 1>&2; exit; fi
- printf "$path"
- }
-
- update() {
- clean
- file=$XDG_CACHE_HOME/mru/"$1"
- if [ -z "$1" ]; then file="$file""home"; fi
- path=$(printf $cache_info | grep "^$1" - | cut -d: -f2)
-
- if [ -z "$path" -o ! -e "$path" ]; then
- printf "path:$path from cache:$file does not exist\n"; exit;
- fi
- #init a temporary file. The traverse the original, if a file exists in a
- # but not in b, delete it from a, if it exists in a and b delete it from b.
- # append the rest of b to a.
- # This should check if each file found is already there. If not, add it to it's position based on modified time
- # find $path -type f -not \( -path '*/.*/*' -o -path '*node_modules/*' -o \
- # -path '*Backups/my-plugins*' -o -path '*.sw[po]' \) -printf \
- # '%TY-%Tm-%Td\t%TT\t%p\n' | sort -r > $file
- }
-
- output() {
- if [ -z "$1" ]; then
- cat $XDG_CACHE_HOME/mru/home; else cat "$XDG_CACHE_HOME/mru/$1";
- fi
- }
-
- list() {
- output "$1" | cut -f3
- }
-
- insert() {
- if [ -z "$1" ]; then printf "No path given\n" 1>&2; exit; fi
- cache_name=${1+'home'}
- cache_path=$(get_cache_path "$cache_name")
-
- file_path=$(realpath "$1")
- if [ -e "$file_path" ]; then
- # sed "0,\|.*\t.*\t$file_path|s|||" $XDG_CACHE_HOME/mru/$cache_name
- sed -e "0,\|.*\t.*\t$file_path|s|||" -e "1s;^;$(date '+%Y-%m-%d%t%T')\t$file_path\n;" <$XDG_CACHE_HOME/mru/$cache_name \
- >$XDG_CACHE_HOME/mru/$cache_name.insert$$
- mv $XDG_CACHE_HOME/mru/$cache_name.insert$$ $XDG_CACHE_HOME/mru/$cache_name
- else
- sed "0,\|.*\t.*\t$file_path|s|||" <$XDG_CACHE_HOME/mru/$cache_name \
- >$XDG_CACHE_HOME/mru/$cache_name.insert$$
- mv $XDG_CACHE_HOME/mru/$cache_name.insert$$ $XDG_CACHE_HOME/mru/$cache_name
-
- fi
- }
-
- clean() {
- rm $XDG_CACHE_HOME/mru/*.{insert,new}*
- }
-
- case "$1" in
- init) init "$2";;
- update) update $2;;
- output) output "$2";;
- list) list "$2";;
- insert) insert "$2" "$3";;
- clean) clean;;
- esac
|