系统升级版定制程序

#!/bin/bash
# filename: mktools.sh
# function: create patch of android system and bak
# author: shenfeng
# date: 2012/05/17


NEW_SYS_PATCH=./input/patch/newSystem
OLD_SYS_PATCH=./input/patch/oldSystem

NEW_SYS_BAK=./input/bak/newSystem
OLD_SYS_BAK=./input/bak/oldSystem

PATCH_PATH=./output/patch
PATCH_UPDATE_LIST=${PATCH_PATH}/update.list

BAK_PATH=./output/bak
BAK_UPDATE_LIST=${BAK_PATH}/update.list


WHICH="/usr/bin/which"
LS=`${WHICH} ls`
STAT=`${WHICH} stat`
ECHO=`${WHICH} echo`
GREP=`${WHICH} grep`
EGREP=`${WHICH} egrep`
SED=`${WHICH} sed`
AWK=`${WHICH} awk`
MD5SUM=`${WHICH} md5sum`
DIFF=`${WHICH} diff`" -a"
CP=`${WHICH} cp`" -Raf"
RM=`${WHICH} rm`" -fr"
TAR=`${WHICH} tar`
ZIP=`${WHICH} zip`
JAVA=`${WHICH} java`
DIRNAME=`${WHICH} dirname`
MKDIR=`${WHICH} mkdir` 

PUBLIC_KEY=./tools/testkey.pk8
PRIVATE_KEY=./tools/testkey.x509.pem
SIGN_TOOL=./tools/signapk.jar

EXT_NAME=".zip"
BASE_NAME="system"
PATCH_PACK_NAME="${BASE_NAME}""${EXT_NAME}"
PATCH_SIGNED_PACK_NAME="${BASE_NAME}_signed${EXT_NAME}"

GZIP_EXT_NAME=".tgz"
BAK_PACK_NAME="system""${GZIP_EXT_NAME}"

PATCH_PACK1_NAME="${BASE_NAME}""${GZIP_EXT_NAME}"

chkfiletype()
{
		local filename=$1
		local filetype=`${STAT} "${filename}" |${GREP} Size |${SED} 's/://g' |${AWK} '{print $8}'`

		case ${filetype} in
		regular)
#${ECHO} "0"
				${ECHO} "regular"
				;;
		directory)
#${ECHO} "1"
				${ECHO} "directory"
				;;
		symbolic)
#${ECHO} "2"
				${ECHO} "symbolic"
				;;
		character)
#${ECHO} "3"
				${ECHO} "character"
				;;
		block)
#${ECHO} "4"
				${ECHO} "block"
				;;
		socket)
#${ECHO} "5"
				${ECHO} "socket"
				;;
		fifo)
#${ECHO} "6"
				${ECHO} "fifo"
				;;
		*)
#${ECHO} "100"
				${ECHO} "other"
				;;
		esac
}

mkupdatelist()
{
		local filename="$1"
		local pathname=" "
		local fileop=$2
		local file_list=$3
		local RE1=$4
		local RE2=$5
		local fileops=""


		filtervar=`${STAT} "${filename}" |${GREP} Uid |${SED} 's/[()/]/ /g'`

		pathname=`${ECHO} ${filename#*${RE1}/}`
		if [ ${fileop} = '0' ] ;then
				fileops="remove"
		else
				fileops="create"
		fi

		local mode=`${ECHO} ${filtervar} |${AWK} '{print $2}'`
		local uid=`${ECHO} ${filtervar} |${AWK} '{print $5}'`
		local gid=`${ECHO} ${filtervar} |${AWK} '{print $8}'`
	   	local filetype=$(chkfiletype "${filename}")

		if [ -f "${filename}" ]; then
				local md5chksum=`${MD5SUM} "${filename}" |${AWK} '{print $1}'`	
		fi

		pathname=`echo ${pathname} |sed 's/_/__/g'`
		pathname=`echo ${pathname} |sed 's/ /_/g'`

		${ECHO} "mode:${mode}" "uid:${uid}" "gid:${gid}" "fileop:${fileops}" "filetype:${filetype}" \
	   			"pathname:${pathname}" "md5chksum:${md5chksum}" >>${file_list}
}

createupdatefile()
{
		local filename="$1"
		local updateop=$2
		local patchdir="$3"
		local RE1=$4

		local pathname=""

		if [ ${updateop} != '1' ]; then
				return 0;
		fi

		pathname=`${ECHO} ${filename#*${RE1}/}`
		pathname=`${DIRNAME} "${pathname}"`
		patchdir="${patchdir}/${pathname}"

		if [ ! -d "${patchdir}" ]; then
				mkdir -p "${patchdir}"
				if [ $? != '0' ]; then
					${ECHO} "create directory ${patchdir} failure!!!"
					return 1
				fi
		fi

		${CP} "${filename}" "${patchdir}"
		if [ $? != '0' ]; then
			${ECHO} "cp "${pathname}" failure!!!"
			return 1
		fi

		return 0
}

comparefile()
{
		local src=$1
		local dst=$2
		
		local sftype=`${STAT} "${src}" |${GREP} Size |${SED} 's/://g' |${AWK} '{print $8}'`
		local dftype=`${STAT} "${dst}" |${GREP} Size |${SED} 's/://g' |${AWK} '{print $8}'`

		if [ ${sftype} != ${dftype} ]; then
				return 1
		fi

		if [ -f "${src}" ]; then
				${DIFF} "${src}" "${dst}"
				if [ $? != '0' ] ; then
						return 1
				fi
		fi

		return 0
}

isemptydir()
{
		local dir=$1
		local count=0

		if [ ! -d "${dir}" ] ;then
				return 1
		fi

		count=`ls -al "${dir}"|wc -l`
		
		if [ ${count} -eq 3 ]; then
				return 1
		fi	

		return 0
}

lookupdiff()
{
		local src=$1
		local dst=$2
		local cmpdrct=$3
		local savedir=$4
		local uplist=$5
		local RE1=$6 
		local RE2=$7 
		local cpflag=$8
		local need_update=0

		isemptydir "${src}"
		if [ $? = '1' ]; then
			return 0	
		fi

		if [ -d ${src} ]; then
				for item in ${src}/*
				do
					local oldsysfile="${item}"
					local newsysfile="${item}"

					oldsysfile=${item#*${RE1}/}
					oldsysfile=${RE2}/${oldsysfile}

					need_update=0

					if [ ! -e "${oldsysfile}" ] ; then
						need_update=1
					else
							if [ ${cmpdrct} = '1' ]; then
								comparefile "${newsysfile}" "${oldsysfile}"
								if [ $? != '0' ]; then
										need_update=1
								fi 
							fi
					fi

					if [ ${need_update} = '1' ]; then
						if [ ${cpflag} = '1' ]; then
						createupdatefile "${newsysfile}" ${cmpdrct} "${savedir}" "${RE1}"
							if [ $? != '0' ]; then
								${ECHO} "createupdate failure!!!"
								return 1;
							fi
						fi
							mkupdatelist "${newsysfile}" ${cmpdrct} "${uplist}" "${RE1}" "${RE2}"
							if [ $? != '0' ]; then
								${ECHO} "mkupdatelist failure!!!"
								return 1;
							fi
					fi

							lookupdiff "${newsysfile}" "${oldsysfile}" ${cmpdrct} "${savedir}" "${uplist}" "${RE1}" "${RE2}"  ${cpflag}
					if [ $? != '0' ]; then
						${ECHO} "lookupdiff failure!!!"
						return 1;
					fi

				done	
		fi

		return 0
}

mkbak()
{
		local ret=0
		local filename=$1

		if [ ! -d "${filename}" ]; then
				${ECHO} ""${filename}" directory no exist!!!"
				return 1
		fi

		cd "${filename}"

		${RM}  ${BAK_PACK_NAME}

		${TAR} -zcf ${BAK_PACK_NAME} `ls`
		if [ $? != '0' ]; then
				${ECHO} "tar failure!!!"
				ret=1
		fi

		cd -

		return ${ret}
}

mkpatch()
{
		local ret=0
		local filename=$1


		if [ ! -d "${filename}" ]; then
				${ECHO} ""${filename}" directory no exist!!!"
				return 1
		fi

		cd "${filename}"

		${RM} ${PATCH_PACK_NAME}
		${RM} ${PATCH_PACK1_NAME}
		${RM} ${PATCH_SIGNED_PACK_NAME}

		local file_list="`ls`"
		${ZIP} -r ${PATCH_PACK_NAME} ${file_list}
		${TAR} -zcvf ${PATCH_PACK1_NAME} ${file_list}

		if [ $? != '0' ]; then
				${ECHO} "zip failure!!!"
				ret=1
		fi

		cd - 
		return ${ret}
}


signpacket()
{
	local pathname="$1"
	local patchname="${pathname}/${PATCH_PACK_NAME}"
	local temp=${patchname%%${EXT_NAME}}

    echo
    echo "Starting to Sign Your Rom, Please Waitting...."
    echo "Signing ${patchname} ......"
    ${JAVA} -jar ${SIGN_TOOL} ${PRIVATE_KEY} ${PUBLIC_KEY} "${patchname}" "${temp}"_signed"${EXT_NAME}"
	if [ $? = '0' ]; then
			echo
			echo "Congratulations,Signature Completed!"
			echo
			return 0
	else
			echo
			echo "Oops!!! Signature Failure!"
			echo
			return 1
	fi

}

generatebak()
{
	local newsys 
	local oldsys 
	local RE1 
	local RE2
	local bakpath
   	local baklist
	local packname
	local var
	
	if [ $# -ne 3 ]; then
		 newsys=${NEW_SYS_BAK}
		 oldsys=${OLD_SYS_BAK}
		 RE1=${NEW_SYS_BAK}
		 RE2=${OLD_SYS_BAK}
		 bakpath=${BAK_PATH}
		 baklist=${BAK_UPDATE_LIST}
	else
		 newsys=$1
		 oldsys=$2
		 RE1=$1
		 RE2=$2
		 bakpath=$3
		 baklist=$3/update.list
	fi
			
	>${baklist}
   	${RM} "${bakpath}"
	${MKDIR} -p "${bakpath}"

	lookupdiff "${newsys}" "${oldsys}" 1 "${bakpath}" "${baklist}" "${RE1}" "${RE2}"  1
	if [ $? != '0' ]; then
		${ECHO} "looupdiff failure!!!"
		return 1
	fi

	mkbak "${bakpath}"
	if [ $? != '0' ]; then
		${ECHO} "mkbak failure!!!"
		return 1
	fi

	return 0
}

generatepatch()
{
	local newsys
	local oldsys
	local RE1
	local RE2
	local var
	local patchpath
	local uplist
	local packname

	if [ $# -ne 3 ]; then
			newsys=${NEW_SYS_PATCH}
			oldsys=${OLD_SYS_PATCH}
			RE1=${NEW_SYS_PATCH}
			RE2=${OLD_SYS_PATCH}
			patchpath=${PATCH_PATH}
			uplist=${PATCH_UPDATE_LIST}
	else
			newsys=$1
			oldsys=$2 
			RE1=$1 
			RE2=$2
			patchpath=$3
			uplist=$3/update.list
	fi
			
	${RM} "${uplist}"
   	${RM} "${patchpath}"
   	${MKDIR} -p "${patchpath}"

	lookupdiff "${newsys}" "${oldsys}" 1 "${patchpath}" "${uplist}" "${RE1}" "${RE2}" 1
	if [ $? != '0' ]; then
		${ECHO} "looupdiff failure!!!"
		return 1
	fi

	lookupdiff "${oldsys}" "${newsys}" 0 "${patchpath}" "${uplist}" "${RE2}" "${RE1}" 0
	if [ $? != '0' ]; then
		${ECHO} "looupdiff failure!!!"
		return 1
	fi

	mkpatch "${patchpath}"
	if [ $? != '0' ]; then
		${ECHO} "mkpatch failure!!!"
		return 1
	fi

	signpacket "${patchpath}"
	if [ $? != '0' ]; then
		${ECHO} "mkpatch failure!!!"
		return 1
	fi

	return 0
}

usage()
{
		${ECHO} "you need extra paramter"
		${ECHO} "eg mktools.sh makepatch(make patch file)"
		${ECHO} "eg mktools.sh makebak(make bak file)"
}

restore_env()
{
		LANG=${LANGBAK}
		export LANG
}

initenv() 
{
		LANGBAK=$LANG
		LANG=en.UTF-8
		export LANG
}

gitclear()
{
		${RM} ${NEW_SYS_PATCH}/*
		${RM} ${OLD_SYS_PATCH}/*
		${RM} ${NEW_SYS_BAK}/*
		${RM} ${OLD_SYS_BAK}/*

		${RM} ${PATCH_PATH}/*
		${RM} ${BAK_PATH}/*
}


arg1=$1
arg2=$2
arg3=$3
arg4=$4
#init shell envirment
initenv

tips()
{
    echo
    echo "Starting to Create update, Please Waitting...."
	echo "you need wait a few minuins"
	echo
}

case ${arg1} in
	makepatch)
	tips
	if [ $# -ne 4 ]; then
		generatepatch
	else
		generatepatch "${arg2}" "${arg3}" "${arg4}"
	fi

	if [ $? != '0' ]; then
		${ECHO} "generate patch failure!!!"
	else
		${ECHO} "generate patch success!!!"
	fi
	;;
	makebak)
	tips
	if [ $# -ne 4 ]; then
		generatebak
	else
		generatebak "${arg2}" "${arg3}" "${arg4}"
	fi

	if [ $? != '0' ]; then
		${ECHO} "generate bak failure!!!"
	else
		${ECHO} "generate bak success!!!"
	fi
	;;
	clear)
	gitclear
	;;
	*)
	usage
	;;
esac

#restore shell envirment
restore_env


转载于:https://my.oschina.net/daleshen128/blog/80704

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值