#!/bin/sh
if [ $# -ne 2 ] ; then
echo use ./install_patch patch_dir dst_dir
exit
fi
patch_dir=$1
INSTALL_DIR=$2
if [ ! -d $patch_dir ] ; then
echo $patch_dir not found
exit
fi
if [ ! -d $INSTALL_DIR ] ; then
echo $INSTALL_DIR not found
exit
fi
SRC_PATH="$INSTALL_DIR/src.$$"
>$SRC_PATH
DST_TMP="$INSTALL_DIR/tmp.$$"
>$DST_TMP
DST_PATH_TMP="$INSTALL_DIR/dstpathtmp.$$"
>$DST_PATH_TMP
DST_PATH="$INSTALL_DIR/dstpath.$$"
>$DST_PATH
DST_DIR="$INSTALL_DIR/dstdir.$$"
>$DST_DIR
FILE_NAME="$INSTALL_DIR/filename.$$"
>$FILE_NAME
PATCH_DIR=`pwd`
PATCH_DIR=$PATCH_DIR/$patch_dir
# find file name include @ or +
find "$PATCH_DIR" -name "*[\@\+]*" >"$SRC_PATH"
echo "The files under patch:"
echo "------------------------------"
cat "$SRC_PATH"
echo "------------------------------"
# include + file count
first=`grep -c "+" "$SRC_PATH"`
# without + file count
second=`grep -c -v "+" "$SRC_PATH"`
# total file count
COUNT=`expr $first + $second `
echo "There are $COUNT patchfiles !"
echo ""
LINE=1
while [ "$LINE" -le "$COUNT" ]
do
POINTER_SRC_PATH=`sed -n "$LINE"p "$SRC_PATH"`
V_SRC_NAME=`basename "$POINTER_SRC_PATH"`
V_DST_TMP="$INSTALL_DIR/$V_SRC_NAME"
echo "$V_DST_TMP" >>"$DST_TMP"
LINE=`expr "$LINE" + 1`
done
# / instead of +
sed 's/\+/\//g' "$DST_TMP" >>"$DST_PATH_TMP"
# . instead of @
sed 's/@/\./g' "$DST_PATH_TMP" >>"$DST_PATH"
LINE=1
while [ "$LINE" -le "$COUNT" ]
do
V_DST_PATH=`sed -n "$LINE"p "$DST_PATH"`
dirname "$V_DST_PATH" >>"$DST_DIR"
basename "$V_DST_PATH" >>"$FILE_NAME"
LINE=`expr "$LINE" + 1`
done
LINE=1
while [ "$LINE" -le "$COUNT" ]
do
POINTER_DST_DIR=`sed -n "$LINE"p "$DST_DIR"`
POINTER_NAME=`sed -n "$LINE"p "$FILE_NAME"`
POINTER_SRC_PATH=`sed -n "$LINE"p "$SRC_PATH"`
if [ ! -d $POINTER_DST_DIR ]
then
echo "Warnning, $POINTER_DST_DIR does not exit! mkdir it first!"
mkdir -p $POINTER_DST_DIR
echo "Replacing $POINTER_DST_DIR/$POINTER_NAME"
cp -f "$POINTER_SRC_PATH" "$POINTER_DST_DIR/$POINTER_NAME"
else
echo "Replacing $POINTER_DST_DIR/$POINTER_NAME"
cp -f "$POINTER_SRC_PATH" "$POINTER_DST_DIR/$POINTER_NAME"
fi
LINE=`expr "$LINE" + 1`
done
rm -f "$SRC_PATH" rm -f "$DST_TMP" rm -f "$DST_PATH_TMP" rm -f "$DST_PATH" rm -f "$DST_DIR" rm -f "$FILE_NAME" echo "Patch has finished!"