-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathupgrade.sh
More file actions
183 lines (150 loc) · 5.64 KB
/
upgrade.sh
File metadata and controls
183 lines (150 loc) · 5.64 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/bin/bash
set -euo pipefail
# TeslaUSB Upgrade Script
# This script pulls the latest code from GitHub and runs setup
# Supports both git-cloned installations and manual installations
REPO_URL="https://github.com/mphacker/TeslaUSB"
ARCHIVE_BASE_URL="https://github.com/mphacker/TeslaUSB/archive/refs/heads"
# Auto-derive install directory from this script's location (run-in-place)
INSTALL_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BRANCH="main"
BACKUP_DIR=""
# Cleanup function for error handling
cleanup_on_error() {
local exit_code=$?
if [ $exit_code -ne 0 ] && [ -n "$BACKUP_DIR" ] && [ -d "$BACKUP_DIR" ]; then
echo ""
echo "============================================"
echo "ERROR: Upgrade failed with exit code $exit_code"
echo "============================================"
echo ""
echo "Restoring from backup: $BACKUP_DIR"
# Restore backed up files
if [ -f "$BACKUP_DIR/state.txt" ]; then
cp "$BACKUP_DIR/state.txt" "$INSTALL_DIR/" 2>/dev/null || true
fi
if [ -d "$BACKUP_DIR/thumbnails" ]; then
rm -rf "$INSTALL_DIR/thumbnails"
cp -r "$BACKUP_DIR/thumbnails" "$INSTALL_DIR/" 2>/dev/null || true
fi
echo "Backup restored."
echo "Removing backup directory..."
rm -rf "$BACKUP_DIR"
echo "Backup directory removed."
echo ""
echo "System restored to previous state."
exit $exit_code
fi
}
# Set trap for error handling (only for non-git path)
trap cleanup_on_error EXIT
echo "==================================="
echo "TeslaUSB Upgrade Script"
echo "==================================="
echo ""
# Store current mode state if it exists
if [ -f "$INSTALL_DIR/state.txt" ]; then
CURRENT_MODE=$(cat "$INSTALL_DIR/state.txt")
echo "Current mode: $CURRENT_MODE"
else
CURRENT_MODE="unknown"
fi
echo ""
# Check if this is a git repository
if [ -d "$INSTALL_DIR/.git" ]; then
echo "Git repository detected - using git pull method"
echo ""
cd "$INSTALL_DIR"
echo "Current directory: $(pwd)"
echo "Current branch: $(git branch --show-current)"
echo ""
# Fetch latest changes
echo "Fetching latest changes from GitHub..."
git fetch origin
# Reset any local changes to tracked files (including chmod changes)
echo "Resetting local changes to tracked files..."
git reset --hard origin/$BRANCH
# Clean up any untracked files (optional - commented out for safety)
# git clean -fd
else
echo "No git repository detected - using direct download method"
echo ""
# Create backup directory with timestamp
BACKUP_DIR="${INSTALL_DIR}_backup_$(date +%Y%m%d_%H%M%S)"
echo "Creating backup at: $BACKUP_DIR"
# Backup important files
mkdir -p "$BACKUP_DIR"
[ -f "$INSTALL_DIR/state.txt" ] && cp "$INSTALL_DIR/state.txt" "$BACKUP_DIR/"
[ -f "$INSTALL_DIR/usb_cam.img" ] && echo "Preserving usb_cam.img (not backed up due to size)"
[ -f "$INSTALL_DIR/usb_lightshow.img" ] && echo "Preserving usb_lightshow.img (not backed up due to size)"
[ -d "$INSTALL_DIR/thumbnails" ] && cp -r "$INSTALL_DIR/thumbnails" "$BACKUP_DIR/"
echo ""
echo "Downloading latest files from GitHub..."
TEMP_DIR=$(mktemp -d)
ARCHIVE_FILE="$TEMP_DIR/repo.tar.gz"
EXTRACT_DIR="$TEMP_DIR/src"
mkdir -p "$EXTRACT_DIR"
ARCHIVE_DOWNLOAD_URL="${ARCHIVE_BASE_URL}/${BRANCH}.tar.gz"
echo "Downloading archive: $ARCHIVE_DOWNLOAD_URL"
curl -fsSL "$ARCHIVE_DOWNLOAD_URL" -o "$ARCHIVE_FILE" || { echo "Failed to download repository archive"; exit 1; }
echo "Extracting archive..."
tar -xzf "$ARCHIVE_FILE" -C "$EXTRACT_DIR" --strip-components=1 || { echo "Failed to extract repository archive"; exit 1; }
echo "Copying files to $INSTALL_DIR..."
mkdir -p "$INSTALL_DIR"
cp -a "$EXTRACT_DIR/." "$INSTALL_DIR/" || { echo "Failed to copy files to install directory"; exit 1; }
# Restore state file if it was backed up
if [ -f "$BACKUP_DIR/state.txt" ]; then
cp "$BACKUP_DIR/state.txt" "$INSTALL_DIR/"
fi
# Clean up temp directory
rm -rf "$TEMP_DIR"
echo ""
echo "Files updated successfully!"
# Delete backup if we got here successfully
if [ -d "$BACKUP_DIR" ]; then
echo "Removing backup (upgrade successful)..."
rm -rf "$BACKUP_DIR"
echo "Backup removed."
fi
fi
# Disable error trap for git-based updates (they handle their own errors)
trap - EXIT
# Ensure scripts are executable
echo ""
echo "Setting execute permissions on scripts..."
chmod +x "$INSTALL_DIR/setup_usb.sh"
chmod +x "$INSTALL_DIR/cleanup.sh"
chmod +x "$INSTALL_DIR/upgrade.sh"
echo ""
echo "==================================="
echo "Code updated successfully!"
echo "==================================="
echo ""
# Ask user if they want to run setup
read -p "Do you want to run setup_usb.sh now? [y/n]: " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo ""
echo "Running setup_usb.sh..."
sudo ./setup_usb.sh
echo ""
echo "==================================="
echo "Upgrade complete!"
echo "==================================="
# Restore previous mode if it was in edit mode
if [ "$CURRENT_MODE" = "edit" ]; then
echo ""
echo "Previous mode was 'edit'. You may want to switch back to edit mode."
read -p "Switch to edit mode now? [y/n]: " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
sudo ./scripts/edit_usb.sh
fi
fi
else
echo ""
echo "Skipping setup. You can run it manually later with:"
echo " cd $INSTALL_DIR && sudo ./setup_usb.sh"
fi
echo ""
echo "Upgrade process finished!"