-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTo_DEB.sh
More file actions
executable file
·314 lines (262 loc) · 8.28 KB
/
To_DEB.sh
File metadata and controls
executable file
·314 lines (262 loc) · 8.28 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/bin/bash
# Bash to DEB Package Converter
# This script converts a bash script into a Debian package
set -euo pipefail # Exit on error, unset variables, and pipe failures
# Default values
SCRIPT_FILE=""
PACKAGE_NAME=""
SCRIPT_NAME="" # New variable for the actual script name
VERSION="1.0.0"
DESCRIPTION="Converted bash script"
MAINTAINER=""
DEPENDS=""
INSTALL_PATH="/usr/local/bin"
ARCHITECTURE="all"
SECTION="utils"
PRIORITY="optional"
OUTPUT_DIR="./deb_packages"
TEMP_DIR="/tmp/deb_build_$$"
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to display usage
usage() {
cat << EOF
Usage: $0 -s SCRIPT_FILE -n PACKAGE_NAME [OPTIONS]
Required options:
-s, --script FILE Path to the bash script to convert
-n, --name NAME Package name (lowercase, no spaces)
Optional options:
-v, --version VERSION Package version (default: 1.0.0)
-d, --description DESC Package description
-m, --maintainer EMAIL Maintainer name and email (e.g., "Name <email>")
-D, --depends DEPS Dependencies (comma-separated, e.g., "curl,wget")
-p, --path PATH Install path (default: /usr/local/bin)
-a, --arch ARCH Architecture (default: all)
-o, --output DIR Output directory (default: ./deb_packages)
-S, --script-name NAME Name for the installed script (default: derived from package name)
-h, --help Show this help message
Example:
$0 -s my_script.sh -n my-package -v 1.2.3 -d 'My awesome script'
$0 -s fakeap.sh -n fakeap-tool -S fakeap
EOF
}
# Function to log messages
log() {
local level=$1
shift
local message="$*"
case $level in
"INFO")
echo -e "${BLUE}[INFO]${NC} $message"
;;
"SUCCESS")
echo -e "${GREEN}[SUCCESS]${NC} $message"
;;
"WARNING")
echo -e "${YELLOW}[WARNING]${NC} $message"
;;
"ERROR")
echo -e "${RED}[ERROR]${NC} $message" >&2
;;
esac
}
# Function to validate package name
validate_package_name() {
local name=$1
# Debian policy: numbers, lowercase letters, +, -, .
# Must start with an alphanumeric character.
if [[ ! $name =~ ^[a-z0-9][a-z0-9+.-]*$ ]]; then
log "ERROR" "Invalid package name: '$name'"
log "INFO" "Package names must differ strictly by case, start with an alphanumeric character, and contain only lowercase letters, digits, plus (+), minus (-), and periods (.)."
return 1
fi
return 0
}
# Function to derive script name from package name
derive_script_name() {
local pkg_name=$1
local clean_name="$pkg_name"
clean_name="${clean_name%.deb}"
clean_name="${clean_name%-tool}"
clean_name="${clean_name%-script}"
clean_name="${clean_name%-app}"
echo "$clean_name"
}
# Function to attempt to guess maintainer info
guess_maintainer() {
local git_name=""
local git_email=""
if command -v git &> /dev/null; then
git_name=$(git config user.name || true)
git_email=$(git config user.email || true)
fi
if [[ -n "$git_name" ]] && [[ -n "$git_email" ]]; then
echo "$git_name <$git_email>"
else
echo "$(whoami) <$(whoami)@$(hostname)>"
fi
}
# Function to check if required tools are installed
check_dependencies() {
local missing_tools=()
for tool in dpkg-deb fakeroot; do
if ! command -v "$tool" &> /dev/null; then
missing_tools+=("$tool")
fi
done
if [ ${#missing_tools[@]} -ne 0 ]; then
log "ERROR" "Missing required tools: ${missing_tools[*]}"
log "INFO" "Install with: sudo apt-get install dpkg-dev fakeroot"
return 1
fi
return 0
}
# Function to calculate installed size (in KB)
calculate_installed_size() {
local build_dir=$1
# du -sk returns size in blocks (usually 1KB blocks)
# Using --exclude to avoid counting DEBIAN overhead if needed, but strict control usually includes everything in data.tar.gz
# However, strictly speaking, Installed-Size is the size of the unpacked files.
# We will approximate it by du -sk of the whole build dir excluding DEBIAN.
du -sk --exclude="$build_dir/DEBIAN" "$build_dir" | awk '{print $1}'
}
# Function to create directories
create_directories() {
local build_dir=$1
mkdir -p "$build_dir/DEBIAN"
mkdir -p "$build_dir$INSTALL_PATH"
}
# Function to copy and prepare the script
prepare_script() {
local build_dir=$1
local target_script="$build_dir$INSTALL_PATH/$SCRIPT_NAME"
# Copy the script
if ! cp "$SCRIPT_FILE" "$target_script"; then
log "ERROR" "Failed to copy script file"
return 1
fi
# Make executable (755)
chmod 755 "$target_script"
# Check shebang
if ! head -n 1 "$target_script" | grep -q "^#!"; then
log "WARNING" "Script missing shebang. Prepending '#!/bin/bash'"
sed -i '1i#!/bin/bash' "$target_script"
fi
}
# Function to finalize control file and md5sums
finalize_debian_metadata() {
local build_dir=$1
local debian_dir="$build_dir/DEBIAN"
# Calculate size
local installed_size
installed_size=$(calculate_installed_size "$build_dir")
# Create control file
cat > "$debian_dir/control" << EOF
Package: $PACKAGE_NAME
Version: $VERSION
Section: $SECTION
Priority: $PRIORITY
Architecture: $ARCHITECTURE
Maintainer: $MAINTAINER
Installed-Size: $installed_size
Description: $DESCRIPTION
EOF
if [ -n "$DEPENDS" ]; then
echo "Depends: $DEPENDS" >> "$debian_dir/control"
fi
# Generate md5sums
log "INFO" "Generating md5sums..."
pushd "$build_dir" > /dev/null
find . -type f ! -path "./DEBIAN/*" -exec md5sum {} + > "DEBIAN/md5sums"
chmod 644 "DEBIAN/md5sums"
popd > /dev/null
}
create_maintainer_scripts() {
local build_dir=$1
local debian_dir="$build_dir/DEBIAN"
# Basic maintainer scripts if needed.
# Currently just logging, as dpkg handles permissions.
}
# Function to build the package
build_package() {
local build_dir=$1
local output_file="$OUTPUT_DIR/${PACKAGE_NAME}_${VERSION}_${ARCHITECTURE}.deb"
mkdir -p "$OUTPUT_DIR"
log "INFO" "Building package..."
if fakeroot dpkg-deb --build "$build_dir" "$output_file"; then
log "SUCCESS" "Package created: $output_file"
log "INFO" "Package Info:"
dpkg-deb --info "$output_file"
return 0
else
log "ERROR" "Failed to build package"
return 1
fi
}
cleanup() {
if [ -d "$TEMP_DIR" ]; then
rm -rf "$TEMP_DIR"
fi
}
trap cleanup EXIT
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-s|--script) SCRIPT_FILE="$2"; shift 2 ;;
-n|--name) PACKAGE_NAME="$2"; shift 2 ;;
-v|--version) VERSION="$2"; shift 2 ;;
-d|--description) DESCRIPTION="$2"; shift 2 ;;
-m|--maintainer) MAINTAINER="$2"; shift 2 ;;
-D|--depends) DEPENDS="$2"; shift 2 ;;
-p|--path) INSTALL_PATH="$2"; shift 2 ;;
-a|--arch) ARCHITECTURE="$2"; shift 2 ;;
-o|--output) OUTPUT_DIR="$2"; shift 2 ;;
-S|--script-name) SCRIPT_NAME="$2"; shift 2 ;;
-h|--help) usage; exit 0 ;;
*) log "ERROR" "Unknown option: $1"; usage; exit 1 ;;
esac
done
# Validation
if [ -z "$SCRIPT_FILE" ] || [ -z "$PACKAGE_NAME" ]; then
usage
log "ERROR" "Missing required parameters (-s and -n)"
exit 1
fi
if [ ! -f "$SCRIPT_FILE" ]; then
log "ERROR" "Script file not found: $SCRIPT_FILE"
exit 1
fi
if [ -z "$SCRIPT_NAME" ]; then
SCRIPT_NAME=$(derive_script_name "$PACKAGE_NAME")
fi
if [ -z "$MAINTAINER" ]; then
MAINTAINER=$(guess_maintainer)
fi
if ! validate_package_name "$PACKAGE_NAME"; then
exit 1
fi
if ! check_dependencies; then
exit 1
fi
log "INFO" "Configuration:"
log "INFO" " Script: $SCRIPT_FILE"
log "INFO" " Package: $PACKAGE_NAME"
log "INFO" " Version: $VERSION"
log "INFO" " Output: $OUTPUT_DIR"
log "INFO" " Maintainer: $MAINTAINER"
# Create Build Directory
mkdir -p "$TEMP_DIR"
# 1. Structure (Create dirs)
create_directories "$TEMP_DIR"
# 2. Prepare Script (Copy files)
prepare_script "$TEMP_DIR"
# 3. Finalize Metadata (Control file, md5sums)
finalize_debian_metadata "$TEMP_DIR"
# 4. Build
if ! build_package "$TEMP_DIR"; then
exit 1
fi