#!/bin/bash

unset REP_DST_PATH
unset REP_SRC_PATH
unset REP_SRC_ID

make_replace_list()
{
	local d=""

	find "${REP_SRC_PATH}" ! -type d | grep -v "/\.svn/" \
		| sort -u > "${REP_DST_PATH}/../replace.log"
	d=$(echo "${REP_SRC_PATH}" | sed -r 's#\+#\\+#g')
	sed -i -r "s#${d}/##" "${REP_DST_PATH}/../replace.log"
	if [ -e "${REP_FILTER}" ] && [ -s "${REP_FILTER}" ]; then
		awk "/^TARGET_REMOVE:BEGIN/,/^TARGET_REMOVE:END/" \
			"${REP_FILTER}" | sed '1d;$d' \
			>> "${REP_DST_PATH}/../replace.log"
		cat "${REP_DST_PATH}/../replace.log" | sort -u \
			> "${REP_DST_PATH}/../replace.log.temp"
		mv "${REP_DST_PATH}/../replace.log.temp" \
			"${REP_DST_PATH}/../replace.log"
	fi
}

make_replace_list_ex()
{
	local d=""
	local i=0
	local src_path=""
	local src_id=""

	if [ ! -e "${REP_FILTER}" ] || [ ! -s "${REP_FILTER}" ]; then
		return 0
	fi

	for ((i=0; i < ${#REP_SRC_PATH[@]}; i++)); do
		src_path=${REP_SRC_PATH[i]}
		src_id=${REP_SRC_ID[i]}
		find "$src_path" ! -type d | grep -v "/\.svn/" \
			| sort -u > \
			"${REP_DST_PATH}/../replace_${src_id}.log"
		d=$(echo "$src_path" | sed -r 's#\+#\\+#g')
		sed -i -r "s#${d}/##" \
			"${REP_DST_PATH}/../replace_${src_id}.log"
	done

	for ((i=0; i < ${#REP_SRC_ID[@]}; i++)); do
		src_id=${REP_SRC_ID[i]}
		awk "/^REP_SRC_FILTER:${src_id}:BEGIN/, \
			/^REP_SRC_FILTER:${src_id}:END/" \
			"${REP_FILTER}" | sed '1d;$d' \
			> "${REP_DST_PATH}/del_${src_id}.log"
		if [ -s "${REP_DST_PATH}/del_${src_id}.log" ]; then
			cat "${REP_DST_PATH}/../replace_${src_id}.log" \
				| grep -v -f \
				"${REP_DST_PATH}/del_${src_id}.log" \
				> "${REP_DST_PATH}/../replace_temp"
			mv "${REP_DST_PATH}/../replace_temp" \
				"${REP_DST_PATH}/../replace_${src_id}.log"
		fi
	done

	for ((i=0; i < ${#REP_SRC_ID[@]}-1; i++)); do
		src_id=${REP_SRC_ID[i]}
		comm -23 "${REP_DST_PATH}/../replace_${src_id}.log" \
			"${REP_DST_PATH}/../replace_${REP_SRC_ID[i+1]}.log" \
			> "${REP_DST_PATH}/../replace_temp"
		mv "${REP_DST_PATH}/../replace_temp" \
			"${REP_DST_PATH}/../replace_${src_id}.log"
	done

	:>"${REP_DST_PATH}/../replace.log"
	for ((i=0; i < ${#REP_SRC_ID[@]}; i++)); do
		src_id=${REP_SRC_ID[i]}
		cat "${REP_DST_PATH}/../replace_${src_id}.log" \
			>> "${REP_DST_PATH}/../replace.log"
		rm -f "${REP_DST_PATH}/del_${src_id}.log"
	done

	awk "/^TARGET_REMOVE:BEGIN/,/^TARGET_REMOVE:END/" \
		"${REP_FILTER}" | sed '1d;$d' \
		>> "${REP_DST_PATH}/../replace.log"
	cat "${REP_DST_PATH}/../replace.log" | sort -u \
		> "${REP_DST_PATH}/../replace.log.temp"
	mv "${REP_DST_PATH}/../replace.log.temp" \
		"${REP_DST_PATH}/../replace.log"
}

backup_replace_files()
{
	local f=""
	local pdir=""

	cat "${REP_DST_PATH}/../replace.log" | while read f; do
		if [ -f "${REP_DST_PATH}/${f}" ] \
			|| [ -L "${REP_DST_PATH}/${f}" ]; then
			pdir=$(dirname "${f}")
			mkdir -p "${REP_DST_PATH}/../replace_bak/${pdir}"
			mv "${REP_DST_PATH}/${f}" \
				"${REP_DST_PATH}/../replace_bak/${pdir}"
		fi
	done
}

restore_replace_files()
{
	local f=""
	local pdir=""

	if [ ! -e "${REP_DST_PATH}/../replace.log" ] \
		|| [ ! -d "${REP_DST_PATH}/../replace_bak" ]; then
		return 0
	fi

	cat "${REP_DST_PATH}/../replace.log" | while read f; do
		rm -rf "${REP_DST_PATH}/${f}"
		if [ -f "${REP_DST_PATH}/../replace_bak/${f}" ] \
			|| [ -L "${REP_DST_PATH}/../replace_bak/${f}" ]; then
			pdir=$(dirname "${f}")
			mkdir -p "${REP_DST_PATH}/${pdir}"
			mv "${REP_DST_PATH}/../replace_bak/${f}" \
				"${REP_DST_PATH}/${pdir}"
		fi
	done

	rm -f "${REP_DST_PATH}/../replace.log"
	rm -rf "${REP_DST_PATH}/../replace_bak"
}

get_replace_dst_path()
{
	unset REP_DST_PATH
	REP_DST_PATH=$( \
		echo -n ${REP_DST_BASE_PATH}/; \
		echo -n $(awk "/^REP_DST:BEGIN/,/^REP_DST:END/" \
			"${REP_FILTER}" | sed '1d;$d'); \
	)
}

get_replace_src_path()
{
	local i=0

	unset REP_SRC_PATH
	REP_SRC_PATH=$( \
		echo $(awk "/^REP_SRC_LIST:BEGIN/,/^REP_SRC_LIST:END/" \
			"${REP_FILTER}" | sed '1d;$d'); \
	)
	REP_SRC_PATH=($REP_SRC_PATH)
	unset REP_SRC_ID
	for ((i=0; i < ${#REP_SRC_PATH[@]}; i++)); do
		REP_SRC_ID[i]=`echo ${REP_SRC_PATH[i]} \
			| awk -F ':' '{print $1}'`
		REP_SRC_PATH[i]=`echo ${REP_SRC_PATH[i]} \
			| awk -F ':' '{print $2}'`
	done
}

replace_files()
{
	local f=""
	local pdir=""

	if [ -e "${REP_DST_PATH}/../replace.log" ] \
		&& [ -d "${REP_DST_PATH}/../replace_bak" ]; then
		restore_replace_files
	fi

	make_replace_list

	backup_replace_files

	mkdir -p "${REP_DST_PATH}/../replace_bak"
	cat "${REP_DST_PATH}/../replace.log" | while read f; do
		if [ -f "${REP_SRC_PATH}/${f}" ] \
			|| [ -L "${REP_SRC_PATH}/${f}" ]; then
			pdir=$(dirname "${f}")
			mkdir -p "${REP_DST_PATH}/${pdir}"
			cp -f "${REP_SRC_PATH}/${f}" \
				"${REP_DST_PATH}/${f}"
		fi
	done
}

replace_files_ex()
{
	local f=""
	local pdir=""
	local i=0
	local src_path=""
	local src_id=""
	local prev_dir=""

	prev_dir=`pwd`
	cd ${REP_SRC_BASE_PATH}
	if [ -e "${REP_DST_PATH}/../replace.log" ] \
		&& [ -d "${REP_DST_PATH}/../replace_bak" ]; then
		restore_replace_files
	fi

	make_replace_list_ex

	backup_replace_files

	mkdir -p "${REP_DST_PATH}/../replace_bak"
	for ((i=0; i < ${#REP_SRC_ID[@]}; i++)); do
		src_path=${REP_SRC_PATH[i]}
		src_id=${REP_SRC_ID[i]}
		cat "${REP_DST_PATH}/../replace_${src_id}.log" \
			| while read f; do
			if [ -f "${REP_SRC_BASE_PATH}/${src_path}/${f}" ] \
			  || [ -L "${REP_SRC_BASE_PATH}/${src_path}/${f}" ]
			then
				pdir=$(dirname "${f}")
				mkdir -p "${REP_DST_PATH}/${pdir}"
				cp -f \
				  "${REP_SRC_BASE_PATH}/${src_path}/${f}" \
					"${REP_DST_PATH}/${f}"
			fi
		done
	done
	cd $prev_dir
}

get_dir_abs_path()
{
	local cur_path=`pwd`
	
	if [ -f "${REQ_PATH}" ]; then
		cur_path=`pwd`
		cd $(dirname "${REQ_PATH}")
		echo $(pwd)"/${REQ_PATH##*/}"
		cd ${cur_path}
	else
		echo $(cd "${REQ_PATH}"; pwd)
		cd "${cur_path}"
	fi
}

progress()
{
	while true; do
		sleep 0.1
		echo -en "\e[34;49m\e[1m#\e[0m"
	done
}

# This is simple replace
#REQ_PATH="./MF970"
#REP_SRC_PATH=`get_dir_abs_path`
#REQ_PATH="./temp/MF970+"
#REP_DST_PATH=`get_dir_abs_path`
#REQ_PATH="./filter"
#REP_FILTER=`get_dir_abs_path`

#if [ "$#" = "0" ]; then
#	replace_files
#elif [ "$1" = "restore" ]; then
#	restore_replace_files
#else
#	echo -en "Usage:\n\t./replace.sh\n\t./replace.sh restore\n"
#fi

# This is extended replace
#REQ_PATH="./"
#REP_DST_BASE_PATH=`get_dir_abs_path`
#REQ_PATH="./"
#REP_SRC_BASE_PATH=`get_dir_abs_path`
#REQ_PATH="./filter_ex"
#REP_FILTER=`get_dir_abs_path`

#get_replace_dst_path
#get_replace_src_path

#progress 1&
#progress_pid=$!
#trap "kill $progress_pid;echo;exit" 1 2 3 15

#if [ "$#" = "0" ]; then
#	replace_files_ex
#elif [ "$1" = "restore" ]; then
#	restore_replace_files
#else
#	echo -en "Usage:\n\t./replace_ex.sh\n\t./replace_ex.sh restore\n"
#fi
#sleep 5

#kill $progress_pid
#echo



