#!/bin/sh 
#
# Copyright 2006-2016 Hunan Kylin, Inc.  All Rights Reserved.
# 
# NOTICE: ALL INFORMATION CONTAINED HEREIN IS, AND REMAINS THE PROPERTY OF
# HUNAN KYLIN, INC. AND ITS SUPPLIERS, IF ANY. THE INTELLECTUAL AND
# TECHNICAL CONCEPTS CONTAINED HEREIN ARE PROPRIETARY TO HUNAN KYLIN, INC.
# AND ITS SUPPLIERS AND MAY BE COVERED BY U.S. AND FOREIGN PATENTS, PATENTS IN
# PROCESS, AND ARE PROTECTED BY TRADE SECRET OR COPYRIGHT LAW. DISSEMINATION
# OF THIS INFORMATION OR REPRODUCTION OF THIS MATERIAL IS STRICTLY FORBIDDEN
# UNLESS PRIOR WRITTEN PERMISSION IS OBTAINED FROM HUNAN KYLIN, INC.

# file-by-file recursive renaming of a directory
# needed because on CIFS shares mounted from Linux the built-in mv command
# fails if any of the files in the renamed directory is open at the time
# the command is issued.
#
usage()
{
	msg="$1"

	if [ -n "$msg" ] ; then
		echo
		error $prog ": " $msg
	fi
	cat <<EOT

Usage: ksvd-mv-dir [options] src dst

Options:
  -v       verbose mode
  -x       debugging mode
  -n       dry run

EOT
	[ -n "$msg" ] && exit 1
}

warn()
{
	echo "${ansi_black_on_yellow}$@" "[m"
}

error()
{
	echo "${ansi_black_on_yellow}$@" "[m"
}

fatal()
{
	error "$@"
	undo
	exit 1
}

init_colors()
{
	ansi_black_on_yellow="[30;43m"
	ansi_purple="[01;35m"

	ansi_on="$ansi_purple"
	ansi_off="[m"
}

undo()
{
	[ -z "$tmpfile" -o ! -e "$tmpfile" ] && return
	warn "undoing file moves"
	/bin/sh "$tmpfile"
	rm "$tmpfile"
}

# --------------------- main starts here ------------------------------
	[ -n "$UNIQB_VERBOSE_SCRIPTS" ] && set -x
	init_colors

	prog=$0
	pid=$$

	while [ $# -gt 0 ]; do
		case "$1" in 
		-x) set -x;;
		-v) verbose=1;;
		-n) dry_run=1;;
		*)  break;;
		esac
		shift
	done
	src="$1"
	dst="$2"

	[ -n "$src" ] || usage "missing source argument"
	[ -n "$dst" ] || usage "missing destination argument"

	[ -d "$src" ] || fatal "$src is not a directory"
	[ ! -e "$dst" ] || fatal "$dst already exists"

	# dst must be absolute because we chdir into src below
	case "$dst" in
	/*) ;;
	*) fatal "destination path must be absolute";;
	esac

	cd "$src" || fatal "cannot chdir to $src"

	dirs=`find * -type d`
	[ "$?" = 0 ] || fatal "failed to find directories to move"
	files=`find * -type f`
	[ "$?" = 0 ] || fatal "failed to find files to move"

	# Step 1: create the distination directories
	for dir in $dirs ; do
		dst_dir="$dst/$dir"
		if [ -z "$dry_run" ] ; then
			mkdir -p "$dst_dir" || fatal "failed to mkdir $dst_dir"
			[ -n "$verbose" ] && echo "created $dst_dir"
		else
			echo "not creating $dst_dir"
		fi
	done

	tmpfile=/tmp/ksvd.$$

	# Step 2: move the files one by one
	for file in $files ; do
		dst_file="$dst/$file"
		if [ -z "$dry_run" ] ; then
			mv "$file" "$dst_file" || fatal "failed to move $src/$file to $dst_file"
			[ -n "$verbose" ] && echo "moved $src/$file to $dst_file"
			echo "mv $dst/$file $src/$file" >> $tmpfile
		else
			echo "not moving $src/$file to $dst_file"
		fi
	done

	# Step 3: delete the now-empty source directories
	if [ -z "$dry_run" ] ; then
		rm -rf "$src" || fatal "failed to delete empty directories in $src"
	fi

	exit 0
