#!/bin/sh

#To add more caches, add a newline to this string and follow the existing name:path format 
#Edit create_new to change find behaviour
cache_info="home:$HOME"

init() {
	mkdir -p $XDG_CACHE_HOME/mru
	file=XDG_CACHE_HOME/mru/$1
	create_new "$1"
	mv "$file.new" "$file"
	touch $XDG_CACHE_HOME/mru/$file.recent
}

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"
}

create_new() {
	file=$XDG_CACHE_HOME/mru/"$1"
	path=$(get_cache_path "$1")

	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'
}

update() {
	file=$XDG_CACHE_HOME/mru/$1
	name="$1"
	if [ -z "$1" ]; then name='home'; fi

	create_new "$name"
	mv "$file.new" "$file"
	rm_recent "$1"
}

updatedirs() {
	cache_name="$1"
	if [ -z "$1" ]; then cache_name=home; fi
	path="$(printf $cache_info | grep "^$cache_name" - | cut -d: -f2)"
	if [ -z "$path" ]; then printf "invalid cache name: $1\n" 1>&2; exit; fi

	list "$1" | sed -n 's:\(.*\)/.*$:\1:p' | sort -u >$XDG_CACHE_HOME/mru/"$cache_name.dirs"
}

# This remove duplicates by deleting files in recent from the main file
rm_recent() {
	file=$XDG_CACHE_HOME/mru/$1
	if [ -z "$1" ]; then file=$XDG_CACHE_HOME/mru/home; fi
	
	while read line
	do
		l=$(printf "$line" | cut -f3)
		if [ ! -z $l ]; then sed -i "\|.*\t.*\t$l|d" $file; fi
	done <$file.recent
}

output() {
	if [ -z "$1" ]; then
		file1="$XDG_CACHE_HOME/mru/home.recent"; file2="$XDG_CACHE_HOME/mru/home" 
	else
		file1="$XDG_CACHE_HOME/mru/$1.recent"; file2="$XDG_CACHE_HOME/mru/$1" 
	fi

	if [ -s $file1 ]; then cat $file1; printf "\n"; fi
	cat $file2
}

list() {
	output "$1" | cut -f3
}

listdirs() {
	cache_name="$1"
	if [ -z "$1" ]; then cache_name=home; fi
	path="$(printf $cache_info | grep "^$cache_name" - | cut -d: -f2)"
	if [ -z "$path" ]; then printf "invalid cache name: $1\n" 1>&2; exit; fi

	cat $XDG_CACHE_HOME/mru/"$cache_name.dirs"
}

insert() {
	if [ -z "$1" ]; then printf "No path given\n" 1>&2; exit; fi
	cache_name=$2
	if [ -z "$2" ]; then cache_name=home; fi
	cache_path=$(get_cache_path "$cache_name")
	file_path=$(realpath "$1")	

	if [ -e "$file_path" ]; then
		sed -i -e "0,\|.*\t.*\t$file_path|d" $XDG_CACHE_HOME/mru/$cache_name.recent
		if [ -s $XDG_CACHE_HOME/mru/$cache_name.recent ]; then
			echo "sed starts$(date '+%Y-%m-%d%t%T')\t$file_path\n"
			sed -i "1i$(date '+%Y-%m-%d%t%T')\t$file_path" $XDG_CACHE_HOME/mru/$cache_name.recent
			echo 'sed ends'
		else 
			printf "$(date '+%Y-%m-%d%t%T')\t$file_path" >$XDG_CACHE_HOME/mru/$cache_name.recent
		fi
	fi

	if [ ! -z "$file_path" ]; then
		echo "second sed starts"
		sed -i "0,\|.*\t.*\t$file_path|d" $XDG_CACHE_HOME/mru/$cache_name
	fi

}

case "$1" in 
	init) init "$2";;
	update) update $2;;
	updatedirs) updatedirs "$2";;
	output) output "$2";;
	list) list "$2";;
	listdirs) listdirs "$2";;
	insert) insert "$2" "$3";;
esac