#!/bin/bash
# doc/f4 - file management helper

set -e

source "/home/zwelch/src/mcf/mcsh-release/install/share/mcsh/mcsh.sh"

lib_load "mcui"


######
# File hashing

f4_hash() {
	file_hash "$@"
	file_hash_print
}


######
# File duplicates

f4_dup() { cmd_dispatch "$@"; }

f4_dup_usage() {
	cat <<USAGE
<cmd> ...
File Duplicate Commands:
	scan <path>+			Finds duplicate files
	link <path>+			Replaces duplicates with hard links
	symlink <path>+			Replaces duplicates with symlinks
	delete <path>+			Removes duplicate files
USAGE
}

f4_dup_scan() { file_duplicate_scan "$@"; }

f4_dup_symlink() { file_duplicate_symlink f4_dup_link_confirm "$@"; }

f4_dup_link() { file_duplicate_link f4_dup_link_confirm "$@"; }

_f4_dup_list_print() {
	app_echo "Duplicate files: "
	(IFS=$'\n' && echo "$*" | grep '^+')
}

f4_dup_link_confirm() {
	_f4_dup_list_print "$@"
	local mcui_title="Replace Duplicates?"
	local link_label="with $file_duplicate_link_type links"
	local msg="Replace all $# duplicates $link_label?"

	local result
	mcui_yesno_warn result "$msg"

	[ "$result" = yes ]
}

f4_dup_delete() { file_duplicate_delete f4_dup_delete_confirm "$@"; }

f4_dup_delete_confirm() {
	_f4_dup_list_print "$@"

	local result
	mcui_yesno_warn result "Delete all $# duplicate files?"

	[ "$result" = yes ]
}


######
# Directory flattening

f4_flatten() {
	local dir=$1
	shift

	[ ! -d "$dir" ] || error "$dir: already exists"
	run_mkdir "$dir"

	local tmp
	tmp=$(cmd_tempfile)
	for path in "$@"; do
		find "$path" -type f >"$tmp"
		local -a files
		readarray -t files <"$tmp"
		for file in "${files[@]}"; do
			local flat
			flat=${file//\//-}
			run ln "$file" "$dir/$flat"
		done
	done
}


######
# Main

f4_desc() { echo "File management helper"; }

f4_usage() {
	cat <<USAGE
<cmd> ...
File Management Commands:
	hash <file>+			Runs $file_hashbin on files
	flatten <dir> <path>+		Creates a flat directory from trees
	dup ...				Manages duplicate files
USAGE
}

app_run "$@"

View the Developer Guide Index

View the Reference Manual Index


Generated on Fri Jul 28 14:34:28 PDT 2017 by mcsh d14 v0.23.0.