#! /bin/sh

if [ $# -lt 1 ]
then
	echo "Usage: $0 [FHS Domain name] ([sp_svc_tran_id] [repair])"
	exit 1
fi
FHS_HOST=$1
REPAIR=$3

if [ "$2" = "repair" ]; then
	REPAIR=$2
else 
	SP_SVC_TRAN_ID=$2
fi

FHSNAME=$FHS_HOST.data
rm -f $FHSNAME  2>&1 > /dev/null

PSQL="/user/service/bin/psql"
RCDB=`grep "^RCDB_IP" /user/service/etc/rcts.conf | grep -v "\_SLAVE" | cut -d = -f 2`
PORT=6543
RCDBNAME="localdb"
RCDBUSER="syshost"

PSQLRUN="$PSQL -h $RCDB -p $PORT -U $RCDBUSER -A -t -c"

META="t_dav_resource"
GET_URICOMM=uri
## check RCDB Type
SQL="SELECT COUNT(tablename) FROM pg_tables WHERE schemaname = 'public' AND tablename = '$META'"
OLDRCDB=`$PSQLRUN "$SQL" $RCDBNAME`
echo "#### Meta Type : $OLDRCDB"

## Get Service
SQL="SELECT sp_svc_tran_id, uri_ignore_case, replication_count FROM t_sms_sp_svc_product"
if [ -n "$SP_SVC_TRAN_ID" ]; then
	SQL="SELECT sp_svc_tran_id, uri_ignore_case, replication_count FROM t_sms_sp_svc_product WHERE sp_svc_tran_id = $SP_SVC_TRAN_ID"
fi 
SVC_LIST=`$PSQLRUN "$SQL" $RCDBNAME`
for v in $SVC_LIST
do
	tran_id=`echo $v | cut -d '|' -f 1`
	uri_ignore_case=`echo $v | cut -d '|' -f 2`
	replication_count=`echo $v | cut -d '|' -f 3`
	echo  -n "      ##### $tran_id, $uri_ignore_case "
	if [ "$OLDRCDB" = "0" ]; then
		META="t_meta_$tran_id"
		GET_URICOMM="display_name"
	fi
	echo  -n "$META "
	## Get Data
	if [ "$REPAIR" = "repair" ]; then
		###    uri|filename_hash|resource_type|get_content_length|file_lastmodified|deleted_yn|is_cache|host_name|creation_date|get_content_type|src_uri|slave tran_id|uri_ignore_case|replication_count
		SQL="SELECT uri,filename_hash,resource_type,get_content_length,file_lastmodified,deleted_yn,is_cache,host_name,creation_date,get_content_type,'',sp_svc_tran_id, '$uri_ignore_case', '$replication_count' FROM $META"
	else 
		###    flag|uri|filename_hash|resource_type|get_content_length|file_lastmodified|deleted_yn|host_name|creation_date|get_content_type|src_uri|slave tran_id|uri_ignore_case
		SQL="SELECT 'MOVE',$GET_URICOMM,filename_hash,resource_type,get_content_length,file_lastmodified,deleted_yn,host_name,creation_date,get_content_type,'',sp_svc_tran_id, '$uri_ignore_case' FROM $META"
	fi

	SQLGET="$SQL WHERE uri like '/$tran_id/%' AND deleted_yn = 'N' AND host_name = '$FHS_HOST'"
	#echo "######### $SQLGET"
	$PSQLRUN "$SQLGET" $RCDBNAME >> $FHSNAME

	echo "...[OK]"
done

if [ "$REPAIR" = "repair" ]; then
	echo "... MODE : repair "
	while read line
	do
		same=();
		same_host=();
		same_uri_attr_cache=();
		same_uri_cache_n=();
		same_uri_cache_r=();
		same_attr_cache=();
		attr_cache_y=();
		other_uri_cache_n=();
		other_uri_cache_r=();
		check=();

		#echo $line
		uri=`echo $line | cut -d '|' -f 1`
		filename_hash=`echo $line | cut -d '|' -f 2`
		filename_hash_in=`echo $filename_hash | sed -e 's|/stg/node0/||g' | sed -e 's|/stg/node1/||g'  | sed -e 's|/stg/node2/||g' | sed -e 's|/user2/dav_storage/||g'` 
		filename_hashs="'/stg/node0/$filename_hash_in','/stg/node1/$filename_hash_in','/stg/node2/$filename_hash_in','/user2/dav_storage/$filename_hash_in','/stg/node0/cache/$filename_hash_in','/stg/node1/cache/$filename_hash_in','/stg/node2/cache/$filename_hash_in','/user2/dav_storage/cache/$filename_hash_in'"
		resource_type=`echo $line | cut -d '|' -f 3`
		get_content_length=`echo $line | cut -d '|' -f 4`
		is_cache=`echo $line | cut -d '|' -f 7`
		host_name=`echo $line | cut -d '|' -f 8`
		tran_id=`echo $line | cut -d '|' -f 12`
		replication_count=`echo $line | cut -d '|' -f 14`

		## dir skip
		if [ $resource_type -eq 1 ]; then
			continue
		fi

		if [ "$OLDRCDB" = "0" ]; then
			META="t_meta_$tran_id"
		fi

				
		#echo "....... $tran_id, $replication_count, $META, $host_name, $uri, $filename_hash($filename_hash_in), $resource_type, $is_cache"

		## cache file delete
		if [ "$is_cache" = "Y" ]; then
			SQLSET="UPDATE $META SET deleted_yn = 'Y' "
			SQL="SQLSET WHERE filename_hash = '$filename_hash' AND deleted_yn = 'N' AND resource_type = 0 AND is_cache = 'Y' AND host_name = '$host_name' ;"
			
			$PSQLRUN "$SQL" $RCDBNAME
			if [ $? -eq 0 ]; then
				echo ".......... $uri, $filename_hash [DEL]"
			else 
				echo ".......... $uri, $filename_hash [DEL] FAILED"
				break
			fi
			
			continue
		fi
		
		SQL="SELECT uri,filename_hash,resource_type,get_content_length,file_lastmodified,deleted_yn,is_cache,host_name FROM $META"
		
		SQLGET="$SQL WHERE filename_hash in ($filename_hashs) AND deleted_yn = 'N' AND resource_type = 0 AND get_content_length = $get_content_length ;"
		#echo "......... $SQLGET"
		
		data=`$PSQLRUN "$SQLGET" $RCDBNAME`
		for v in $data
		do
			#echo ".......... $v"
			if [ `echo $v | grep "$uri" | grep "$host_name" | grep -c "|N|$is_cache|" ` -gt 0 ] ; then
				#echo "............. $v [same]"
				same[${#same[@]}]=$v
			elif [ `echo $v | grep -c "$host_name" ` -gt 0 ] ; then
				#echo "............. $v [same_host]"
				same_host[${#same_host[@]}]=$v
			elif [ `echo $v | grep "$uri" | grep -c "|N|$is_cache|" ` -gt 0 ] ; then
				#echo "............. $v [same_uri_attr_cache]"
				same_uri_attr_cache[${#same_uri_attr_cache[@]}]=$v
			elif [ `echo $v | grep "$uri" | grep -c "|N|N|" ` -gt 0 ] ; then
				#echo "............. $v [same_uri_cache_n]"
				same_uri_cache_n[${#same_uri_cache_n[@]}]=$v
			elif [ `echo $v | grep "$uri" | grep -c "|N|R|" ` -gt 0 ] ; then
				#echo "............. $v [same_uri_cache_r]"
				same_uri_cache_r[${#same_uri_cache_r[@]}]=$v
			elif [ `echo $v | grep -c "|N|$is_cache|"  ` -gt 0 ] ; then
				#echo "............. $v [same_attr_cache]"
				same_attr_cache[${#same_attr_cache[@]}]=$v
			elif [ `echo $v | grep -c "|N|Y|"  ` -gt 0 ] ; then
				#echo "............. $v [same_attr_cache]"
				attr_cache_y[${#attr_cache_y[@]}]=$v
			elif [ `echo $v | grep -c "|N|N|" ` -gt 0 ] ; then
				#echo "............. $v [outher_uri_cache_n]"
				other_uri_cache_n[${#other_uri_cache_n[@]}]=$v
			elif [ `echo $v | grep -c "|N|R|" ` -gt 0 ] ; then
				#echo "............. $v [other_uri_cache_r]"
				other_uri_cache_r[${#other_uri_cache_r[@]}]=$v
			else
				echo "............. [$line] => [$v] [check]"
				check[${#check[@]}]=$v 
				exit 1 
			fi
		done
		
		#echo -n "............"
		##echo -n " $line"
		#echo -n " [same : ${#same[@]}, same_host :${#same_host[@]}"
		#echo -n ", same_uri_attr_cache: ${#same_uri_attr_cache[@]}"
		#echo -n ", same_uri_cache_n: ${#same_uri_cache_n[@]}"
		#echo -n ", same_uri_cache_r: ${#same_uri_cache_r[@]}"
		#echo -n ", same_attr_cache : ${#same_attr_cache[@]}"
		#echo -n ", attr_cache_y: ${#attr_cache_y[@]}"
		#echo -n ", other_uri_cache_n: ${#other_uri_cache_n[@]}"
		#echo -n ", other_uri_cache_r: ${#other_uri_cache_r[@]}"
		#echo    ", check : ${#check[@]}]"

		if [ ${#same[@]} -eq 0 ]; then
			echo ".......... $uri, $filename_hash, $host_name [Not Found]"
			continue
		fi

		filename_hash2=""
		host_name2=""
		SQLSET=""
		SQL2=""

		if [ ${#same_attr_cache[@]} -gt 0 ]; then
				echo "########### same_attr_cache: ${#same_attr_cache[@]} : ${same_attr_cache[0]}"
				filename_hash2=`echo ${same_attr_cache[0]} | cut -d '|' -f 2`
				host_name2=`echo ${same_attr_cache[0]} | cut -d '|' -f 8`

				SQLSET="UPDATE $META SET filename_hash = '$filename_hash2', host_name = '$host_name2'" 
				SQL2="$SQLSET WHERE filename_hash = '$filename_hash' AND deleted_yn = 'N' AND resource_type = 0 AND is_cache = '$is_cache' AND host_name = '$host_name' ;"
		elif [ ${#attr_cache_y[@]} -gt 0 ] ; then
				echo "########### attr_cache_y: ${#attr_cache_y[@]} : ${attr_cache_y[0]}"
				filename_hash2=`echo ${attr_cache_y[0]} | cut -d '|' -f 2`
				host_name2=`echo ${attr_cache_y[0]} | cut -d '|' -f 8`

				SQLSET="UPDATE $META SET deleted_yn = 'Y'" 
				SQL2="$SQLSET WHERE filename_hash = '$filename_hash2' AND deleted_yn = 'N' AND resource_type = 0 AND is_cache = 'Y' AND host_name = '$host_name2' ;"
		else 
			case "$is_cache" in
			"N") 
				#echo ".......... $line ['N' case]"
				if [ ${#same_uri_cache_r[@]} -gt $replication_count ]; then
					#echo "########### same_uri_cache_r: ${#same_uri_cache_r[@]} : ${same_uri_cache_r[0]}"
					filename_hash2=`echo ${same_uri_cache_r[0]} | cut -d '|' -f 2`
					host_name2=`echo ${same_uri_cache_r[0]} | cut -d '|' -f 8`

					SQLSET="UPDATE $META SET deleted_yn = 'Y'" 
					SQL2="$SQLSET WHERE filename_hash = '$filename_hash2' AND deleted_yn = 'N' AND resource_type = 0 AND is_cache = 'R' AND host_name = '$host_name2' ;"
				elif [ ${#other_uri_cache_r[@]} -gt $replication_count ] ; then
					#echo "########### other_uri_cache_r: ${#other_uri_cache_r[@]} : ${other_uri_cache_r[0]}"
					filename_hash2=`echo ${other_uri_cache_r[0]} | cut -d '|' -f 2`
					host_name2=`echo ${other_uri_cache_r[0]} | cut -d '|' -f 8`

					SQLSET="UPDATE $META SET deleted_yn = 'Y'" 
					SQL2="$SQLSET WHERE filename_hash = '$filename_hash2' AND deleted_yn = 'N' AND resource_type = 0 AND is_cache = 'R' AND host_name = '$host_name2' ;"
				fi
			;;
			#"R")
			#	echo ".......... $line ['R' case]"
			#;;
			*)
				echo "$line [Unknown error]"
				exit 1
			;;
			esac
		
		fi
		
		if [ -n "$SQL2" ]; then
			#echo "............ repair SQL : [$SQL2] "
			$PSQLRUN "$SQL2" $RCDBNAME
			if [ $? -eq 0 ]; then
				echo ".......... $uri, $filename_hash [OK]"
			else 
				echo ".......... $uri, $filename_hash [UPDATE] FAILED"
			fi
		else
			echo "..........  $uri, $filename_hash [rc_cmove]"
		fi
		
	done < $FHSNAME
	rm -f $FHSNAME
	echo "... MODE : repair [Done]"
fi
