#
# 将本脚本文件放到与 setup.exe 相同的目录下，然后执行之
# Put this script file to the directory which contains setup.exe, and execute it
#
PrintUsage ()
{
	echo "################################################################################"
	echo "                   清理 Cygwin 旧安装包                                         "
	echo "            Clean Old Install Packages of Cygwin                                "
	echo "                                                                                "
	echo "           by LiuYan<lovetide＠qq.com> 2009-12-02 00:55                         "
	echo "################################################################################"
	echo
	echo "Usage 用法: "
	echo "  $0 [-d] [-g][-l] [-p]"
	echo "    -d  Delete file if it is old"
	echo "        删除旧文件"
	echo "    -g, -l  Generate old file deletion script file"
	echo "            生成删除旧文件的执行脚本文件"
	echo "    -p  Treat files listed in [prev] section of setup.ini as old"
	echo "        将 setup.ini 中 [prev] 部分所列出的文件当作旧文件"
	echo
}
if [ $# -eq 0 ]
then
	PrintUsage
fi

o_delete=false
o_list=false
o_treat_prev_section_as_old=false
while getopts "dglp" option
do
	case $option in
		d)
			o_delete=true
			;;
		g|l)
			o_list=true
			rm_command_list_file=rm_old_packages_`date +%F`.sh
			rm -f "$rm_command_list_file"
			;;
		p)
			o_treat_prev_section_as_old=true
			;;
	esac
done

SCRIPT_BASE_DIR=`dirname $0`
#echo "SCRIPT_BASE_DIR=$SCRIPT_BASE_DIR"
BeginTime=`date "+%F %T %N"`

# 查找所有子目录下的 setup.ini 和 setup-2.ini 文件
# Search setup.exe or setup-2.exe in sub-directories

setup_ini_file_list=`find "$SCRIPT_BASE_DIR" -name setup.ini -or -name setup-2.ini`
echo "$setup_ini_file_list" > cop_setup_ini_file_list.txt

# 对比 setup.ini 中的“最新文件列表”与本地目录中的文件，清理不在“最新文件列表”中的旧文件
# Compair the "Newest File List" in setup.ini and the local files, clean old files which not exist in "Newest File List"

total_old_file_count=0
for setup_ini_file in $setup_ini_file_list
do
	MIRROR_BASE_DIR=`dirname $setup_ini_file`
	setup_ini_file_short_name=`basename $setup_ini_file`
	
	if [ $setup_ini_file_short_name == "setup-2.ini" ]
	then
		PACKAGE_BASE_DIR="$MIRROR_BASE_DIR/release-2/"
	else
		PACKAGE_BASE_DIR="$MIRROR_BASE_DIR/release/"
	fi

	echo "INI file : $setup_ini_file"
	echo "Directory: $PACKAGE_BASE_DIR"
	
	# 得到 setup.ini 中的“最新文件列表”
	# Get the "Newest File List" from setup.ini

	if $o_treat_prev_section_as_old
	then
		newest_file_list=`gawk ' \
		BEGIN { \
			b_new_file=0; \
		} \
		/^install:|^source:|^\[prev\]|^@ / \
		{ \
			if (substr($0,1,2)=="@ ") \
			{ \
				b_new_file=1; \
				next; \
			} \
			else if ($0=="[prev]") \
			{ \
				b_new_file=0; \
			} \
			if (b_new_file==1) \
			{ \
				print $2; \
			} \
		} \
		' "$setup_ini_file"`
	else
		newest_file_list=`gawk '/^install:|^source:/{print $2}' $setup_ini_file`
	fi
	newest_file_list_file=cop_newest_file_list${setup_ini_file//\//__}.txt
	echo "$newest_file_list" > $newest_file_list_file
	
	# 遍历安装包文件夹，删除不在“最新文件列表”中的文件
	# Search install package directory recursively, delete files not exist in "Newest File List"
	
	local_file_list=`find "$PACKAGE_BASE_DIR" -type f`
	package_old_file_count=0
	for local_file in $local_file_list
	do
		local_file_in_setup_ini_format=${local_file#${MIRROR_BASE_DIR}/}
		
		file_is_fresh=false
		## via grep
		grep -i "$local_file_in_setup_ini_format" $newest_file_list_file > /dev/null
		if [ $? -eq 0 ]
		then
			file_is_fresh=true
		fi

		# via bash conditional expression (=~ binary operator)
		#if [[ $newest_file_list =~ $local_file_in_setup_ini_format ]]
		#then
		#	file_is_fresh=true
		#fi
		
		if $file_is_fresh
		then
			echo "  $local_file_in_setup_ini_format"
		else
			echo "X $local_file_in_setup_ini_format"
			(( total_old_file_count ++ ))
			(( package_old_file_count ++ ))

			if $o_list
			then
				echo "rm -f \"$local_file\"" >> $rm_command_list_file
			fi

			if $o_delete
			then
				rm -f "$local_file"
				exit_code=$?
				echo -n "Delete $local_file ..."
				if [ $exit_code -eq 0 ]
				then
					echo Done!
				else
					echo Failed with code $exit_code!
				fi

			fi
		fi
	done
	echo "$package_old_file_count old files in $PACKAGE_BASE_DIR"
	echo
done

EndTime=`date "+%F %T %N"`
echo "Total $total_old_file_count old files"
echo "BeginTime: $BeginTime"
echo "EndTime  : $EndTime"

