-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipkg_remove.sh
executable file
·158 lines (149 loc) · 4.53 KB
/
ipkg_remove.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/sh
#
# $Id: ipkg_remove.sh 336 2012-08-04 00:12:14Z jeremy.collake@gmail.com $
#
. "./shared.inc"
### 20110225-MCT The VERSION is set in the shared.inc file from a single external source now.
VERSION="${SHARED_VERSION}"
##################################################
#
# Title: ipkg_remove.sh
# Author: Jeremy Collake <jeremy.collake@gmail.com>
# Site: hhttp://code.google.com/p/firmware-mod-kit/
#
#
# USAGE: ipkg_remove.sh nano_1.3.8-1_mipsel.ipk WORKING_DIRECTORY/
#
#
# Example:
#
# ./ipkg_remove.sh dd-wrt.v23_generic.bin std_generic
#
#
echo "$0 v$VERSION, (c)2006-2012 Jeremy Collake"
echo " !!WARNING!!! This script is in early alpha stage of development"
##################################################
# CleanupTmp [base_dir] [package basename]
#
CleanupTmp()
{
rm -rf "$1/tmp" >> /dev/null 2>&1
rm "$/control.tar.gz" >> /dev/null 2>&1
rm "$/data.tar.gz" >> /dev/null 2>&1
rm "$1/$2" >> /dev/null 2>&1
}
###########################################################
# DeleteWithFolderTemplate ( target_dir , template_dir )
#
# deletes files and folders from a target folder
# based on their presence in a template folder
# (all files/dirs in template folder deleted from
# target folder).
#
# This function calls itself recursively.
#
# One would think there'd be an easier way to accomplish
# this, and maybe there is... I am not a bash guru.
#
DeleteWithFolderTemplate()
{
local TARGET_DIR=$1
local TEMPLATE_DIR=$2
#echo " Processing folder $TEMPLATE_DIR .."
##################################################
for i in `ls $TEMPLATE_DIR`; do
#echo " dbg: $TEMPLATE_DIR/$i"
if [ -d $TEMPLATE_DIR/$i ]; then
if [ -L $TEMPLATE_DIR/$i ]; then
echo " Removing symbolic link $TARGET_DIR/$i"
rm $TARGET_DIR/$i >> /dev/null 2>&1
else
DeleteWithFolderTemplate $TARGET_DIR/$i $TEMPLATE_DIR/$i
# now remove folder if empty
rmdir $TARGET_DIR/$i >> /dev/null 2>&1
fi
elif [ -f $TEMPLATE_DIR/$i ]; then
echo " Removing file at $TARGET_DIR/$i"
rm $TARGET_DIR/$i >> /dev/null 2>&1
else
echo " WARNING: Unknown file type at $TEMPLATE_DIR/$i"
fi
done
}
##################################################
if [ ! $# = "2" ]; then
echo " Invalid usage"
echo " USAGE: $0 PACKAGE_PATH WORKING_DIRECTORY"
exit 1
fi
BASE_NAME=`basename $1`
echo " Removing $BASE_NAME"
####################################################
if [ ! -e "$1" ]; then
echo " ERROR: $1 does not exist."
exit 1
fi
OLD_DIR=`pwd`
##################################################
# notes:
# OS X's tar utility doesn't like GZIP'd TARs .. (re: not TAR'd GZIPs ..),
# else we could just use tar (wh##################################################ich under linux accepts GZIPs as well).
#
mkdir -p "$2/installed_packages/" >> ipkg_remove.log 2>&1
cp $1 "$2/installed_packages/"
cd "$2/installed_packages"
INSIDE_NAME=`echo "$BASE_NAME" | sed "s/.ipk/ /"`
gunzip < "$BASE_NAME" > "$INSIDE_NAME"
echo " Assuming contents bore $INSIDE_NAME"
rm -rf tmp
mkdir tmp
tar -xf "$INSIDE_NAME" -C "tmp/" >> /dev/null 2>&1
if [ $? != 0 ]; then
echo " ERROR: Extraction failed or incompatible format."
#CleanupTmp "." "$INSIDE_NAME"
exit 1
fi
##################################################
echo " Removing files from $2/rootfs ..."
rm -rf "tmp/data" >> /dev/null 2>&1
mkdir -p "tmp/data" >> /dev/null 2>&1
tar -xzvf "tmp/data.tar.gz" -C "tmp/data" >> /dev/null 2>&1
if [ $? != 0 ]; then
echo " ERROR: Extraction failed of data.tar.gz (missing from IPK?)"
CleanupTmp "." "$INSIDE_NAME"
cd "$OLD_DIR"
exit 1
else
echo " Package removed successfully!"
fi
#
# using . as a template, delete from working rootfs
#
cd "$OLD_DIR"
DeleteWithFolderTemplate $2/rootfs "$2/installed_packages/tmp/data"
cd "$2/installed_packages"
##################################################
echo " --------------------------------------------"
echo " Examining control files $2/rootfs ..."
echo " Pay attention to the dependencies as you"
echo " may want to remove some of them if not"
echo " used by any other installed package."
echo
tar -xzf "tmp/control.tar.gz" -C "tmp/"
if [ $? != 0 ] || [ ! -e "tmp/control" ]; then
echo " ERROR: Extraction failed of control files (missing from IPK?)"
CleanupTmp "." "$INSIDE_NAME"
cd "$OLD_DIR"
exit 1
else
# todo: add proper dependency checking and more
cat "tmp/control"
# if successeful, remove the package from the installed_packages
# folder
CleanupTmp "." "$INSIDE_NAME"
echo " Removing package IPK from installed_packages ..."
cd "$OLD_DIR"
fi
echo " --------------------------------------------"
##################################################
exit 0