#!/bin/bash
# Version 1.2
# create by OE-A
if [ $# -ne 1 ]; then
echo "Usage: $0 <input_tar.xz>"
exit 1
fi
input_file="$1"
if [ ! -f "$input_file" ]; then
echo "Input file does not exist: $input_file"
exit 1
fi
# Determine the folder name(s) based on the input file name
if [[ "$input_file" == *"-dreambox-"* ]]; then
folder_names=("dreamone" "dreamtwo")
aio="-AIO"
elif [[ "$input_file" == *"-dreamone-"* ]]; then
folder_names=("dreamone")
aio=""
elif [[ "$input_file" == *"-dreamtwo-"* ]]; then
folder_names=("dreamtwo")
aio=""
else
echo "Input file name should contain '-dreambox-,' '-dreamone-,' or '-dreamtwo-'"
exit 1
fi
# Determine the Namen and the Version based on the input file name
if [[ "$input_file" == *"dream-elite"* ]]; then
name="Dream Elite"
version="2.6"
elif [[ "$input_file" == *"dreambox-image"* ]]; then
name="Dreambox"
version="2.6"
elif [[ "$input_file" == *"gemini4.2-unstable"* ]]; then
name="Gemini"
version="4.2"
elif [[ "$input_file" == *"Merlin_OE-2.6"* ]]; then
name="Merlin"
version="2.6"
elif [[ "$input_file" == *"newnigma2-deb"* ]]; then
name="Newnigma2"
version="2.6"
elif [[ "$input_file" == *"openpli-enigma2"* ]]; then
name="OpenPLI"
version="8xstar"
elif [[ "$input_file" == *"peterpan-"* ]]; then
name="PeterPan"
version="2.6"
else
name="Opendreambox"
version="2.6"
fi
# Determine the AIO TYPE based on the input file name
if [[ "$input_file" == *"-aio-"* ]]; then
aio="-AIO"
fi
# Get the directory where the script is located
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Create a temporary directory in the same folder as the script
temp_dir=$(mktemp -d "$script_dir/tempdir.XXXXXXXXXX")
# Function to create a folder if it doesn't exist
create_folder_if_not_exists() {
local folder="$1"
if [ ! -d "$folder" ]; then
echo "Creating folder: $folder"
mkdir -p "$folder"
fi
}
# Function to delete an existing folder
delete_existing_folder() {
local folder="$1"
if [ -d "$folder" ]; then
echo "Deleting existing folder: $folder"
rm -rf "$folder"
fi
}
# Extract the tar.xz file as a tar into the temporary directory
echo "Extracting $input_file in a temporary directory..."
tar -xf "$input_file" -C "$temp_dir"
# create Multiboot Imagename
echo "${name} ${version}${aio} \n \l" > ${temp_dir}/etc/issue
echo "\n \l" >> ${temp_dir}/etc/issue
for folder_name in "${folder_names[@]}"; do
delete_existing_folder "$folder_name"
create_folder_if_not_exists "$folder_name"
echo "copy $temp_dir/boot/Image.gz-4.9 to $folder_name/kernel.bin"
cp "$temp_dir/boot/Image.gz-4.9" "$folder_name/kernel.bin"
echo "Creating imageversion file in $folder_name..."
echo "$input_file" > "$folder_name/imageversion"
done
# Repack the extracted content as tar.bz2
echo "Repacking extracted content as rootfs.tar.bz2..."
tar -cjf "$script_dir/rootfs.tar.bz2" -C "$temp_dir" .
# Move the tar.bz2 file to each folder
for folder_name in "${folder_names[@]}"; do
# Copy the repacked tar.bz2 file to the folder
echo "Copying rootfs.tar.bz2 to $folder_name..."
cp "$script_dir/rootfs.tar.bz2" "$folder_name/rootfs.tar.bz2"
# Get the base name of the input file (without path)
base_filename=$(basename "$input_file")
# Zip each folder with the full input filename
output_zip="${base_filename%.tar.xz}_${folder_name}.zip"
echo "Zipping $folder_name to $output_zip..."
zip -r "$output_zip" "$folder_name"
delete_existing_folder "$folder_name"
if [ $? -eq 0 ]; then
echo "Task completed successfully for $folder_name. Output saved as $output_zip"
else
echo "Failed to create the zip file for $folder_name."
fi
done
# Clean up the temporary directory
rm "$script_dir/rootfs.tar.bz2"
rm -rf "$temp_dir"