-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbb_install.sh
147 lines (125 loc) · 4.12 KB
/
bb_install.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
# boot-buddy.sh [-s /path/to/sdcard] [--uninstall]
# Boot Buddy v1.3.2
# Licensed GNU GPL v2, Author Sepero - sepero 111 @ gmx . com
# https://github.com/Sepero/bootbuddy/
# An Error function incase we need to abort the script.
exit_error () {
echo "$@" >&2
exit 127
}
# An Exit function to successfully complete the script.
exit_success () {
sync
echo "Remounting /system as read only"
$BUSYB mount -o remount,ro /system
$BUSYB echo -e "\nFinished"
exit
}
### Start setting up global variables.
# Try to verify location of shell.
SHELL="/system/xbin/sh"
[ -f $SHELL ] || SHELL="/system/bin/sh"
[ -f $SHELL ] || exit_error "System shell could not be found: $SHELL"
# Verify busybox location.
BUSYB="/system/bin/busybox"
[ -f $BUSYB ] || BUSYB="/system/xbin/busybox"
[ -f $BUSYB ] || exit_error "Busybox was not found"
# Get user id number.
ID=$(id | sed 's/[^=]*=//;s/\([0-9]*\).*/\1/')
# Name of the BB directory for running scripts.
BB_DIR="boot_buddy_scripts"
# The BB executable script file. This will run user scripts.
BB_FILE="/data/boot_buddy.sh"
# Location of the Install Recovery file. This file is used to run the BB executable.
IR_FILE="/system/etc/install-recovery.sh"
# Text to insert into the Install Recovery file.
IR_HEAD="# bootbuddy boot script start"
IR_MAIN="chown 0.0 $BB_FILE
chmod 755 $BB_FILE
busybox sh $BB_FILE"
IR_TAIL="# bootbuddy boot script end"
# Try to find location of the real sdcard. It should be in $EXTERNAL_STORAGE,
# but some manufacturers put it under other names.
# "$EXTERNAL_STORAGE" # Kyocera, Motorola
# "$EXTERNAL_STORAGE2" # ?
# "$EXTERNAL_ADD_STORAGE" # LG Esteem, LG L9
# "$SECONDARY_STORAGE" # Samsung Galaxy S4
# "$SECOND_VOLUME_STORAGE" # Insignia Flex 8
# "$USBHOST_STORAGE" # ?
# "$PHONE_STORAGE" # ?
for i in "$EXTERNAL_STORAGE" \
"$EXTERNAL_STORAGE2" \
"$EXTERNAL_ADD_STORAGE" \
"$SECONDARY_STORAGE" \
"$SECOND_VOLUME_STORAGE" \
"$USBHOST_STORAGE" \
"$PHONE_STORAGE" \
; do
[ -n "$i" ] && SDCARD="$i" # If the variable is set, then use it as our sdcard.
done
if [ "$1" == "-s" ]; then # Change $SDCARD to user specified sdcard location.
SDCARD="$2"
shift; shift
fi
### End setting up global variables.
# Check for root.
[ "$ID" == "0" ] || exit_error "You're not root"
# Remount the system as writable.
echo "Remounting /system as writable"
busybox mount -o remount,rw /system
# If uninstalling, then remove file traces and exit.
if [ "$1" == "--uninstall" ]; then
echo "Deleting $BB_FILE"
rm $BB_FILE 2> /dev/null
if [ -e $IR_FILE ]; then
echo "Cleaning $IR_FILE"
match=$(sed "/$IR_HEAD/,/$IR_TAIL/d" $IR_FILE)
remain=$(echo "$match" | busybox grep -v "^[[:space:]]$")
if [ "$remain" == "#!$SHELL" ]; then
rm $IR_FILE
else
echo "$match" > $IR_FILE
fi
fi
echo "== UNINSTALLED BOOT BUDDY =="
exit_success
fi
# Install script files.
text="$IR_HEAD\n$IR_MAIN\n$IR_TAIL"
echo "Setting up $IR_FILE"
if [ ! -e $IR_FILE ]; then
busybox echo -e "#!$SHELL\n" > $IR_FILE
busybox echo -e "$text" >> $IR_FILE
else
# Remove text if it's already in the file.
match=$(sed "/$IR_HEAD/,/$IR_TAIL/d" $IR_FILE)
# Re-add text at end of file.
busybox echo -e "$match\n\n$text" > $IR_FILE
fi
chown 0.0 $IR_FILE
chmod 755 $IR_FILE
# Creates or overwrites the exectuable file /data/boot_buddy.sh ($BB_FILE)
echo "Setting up $BB_FILE"
echo "#!$SHELL
BB_LOG=\"/data/boot_buddy.log\"
rm \$BB_LOG
delay_for_sd () {
for i in \$(seq 0 180); do
if [ -e \"$SDCARD/$BB_DIR/\" ]; then
break
fi
busybox sleep 1
done
cd \"$SDCARD/$BB_DIR/\"
ls * | sort | while read i; do
echo \"RUNNING \$i:\" >> \$BB_LOG
busybox sh \"\$i\" >> \$BB_LOG 2>> \$BB_LOG
echo \"\" >> \$BB_LOG
done
}
delay_for_sd&" > $BB_FILE
# Finished writing the file $BB_FILE
echo "Setting up folder $SDCARD/$BB_DIR"
mkdir -p "$SDCARD/$BB_DIR"
$BUSYB echo -e "\nPut scripts you want to run at boot in the folder $BB_DIR"
exit_success