diff --git a/buildenv.sh b/buildenv.sh index 28f2ac5d..d8f27aff 100644 --- a/buildenv.sh +++ b/buildenv.sh @@ -20,6 +20,68 @@ set -o allexport +PACKAGES=( + libbrotli-dev liblz4-dev libzstd-dev protobuf-compiler libgtest-dev + ffmpeg webp zipalign clang build-essential cmake openjdk-17-jdk + libncurses-dev bison flex libssl-dev libelf-dev python3 + python-is-python3 cpio attr zip libprotobuf-dev make + libpcre2-dev ccache npm +) + +echo "Checking dependencies..." + +MISSING_PACKAGES=() +for PACKAGE in "${PACKAGES[@]}"; do + if ! dpkg -s "$PACKAGE" >/dev/null 2>&1; then + MISSING_PACKAGES+=("$PACKAGE") + fi +done + +if [ ${#MISSING_PACKAGES[@]} -eq 0 ]; then + echo "All dependencies are already installed." +else + echo "The following packages are missing: ${MISSING_PACKAGES[*]}" + echo "Attempting to install missing packages..." + sudo apt update + sudo apt install "${MISSING_PACKAGES[@]}" +fi + +echo "Checking for missing Git submodules..." + +if [ -f .gitmodules ]; then + UNINITIALIZED_SUBMODULES=$(git submodule status | grep '^-' || true) + + if [ -n "$UNINITIALIZED_SUBMODULES" ]; then + echo "The following submodules are missing or uninitialized:" + echo "$UNINITIALIZED_SUBMODULES" + echo "Initializing and cloning submodules..." + git submodule update --init --recursive + if [ $? -eq 0 ]; then + echo "Submodules initialized and cloned successfully." + else + echo "Failed to clone submodules. Please check your repository configuration." + exit 1 + fi + else + echo "All submodules are already initialized." + fi +else + echo "No submodules found in this repository." +fi + +#create venv if not exist +if [ ! -f "venv/bin/activate" ]; then + echo "Virtual environment not found. Creating a new one..." + python3 -m venv venv +else + echo "Virtual environment already exists." +fi + +source venv/bin/activate + +pip3 install git+https://github.com/ananjaser1211/samloader.git &> /dev/null + + # [ SRC_DIR="$(git rev-parse --show-toplevel)" OUT_DIR="$SRC_DIR/out" @@ -32,14 +94,30 @@ TOOLS_DIR="$OUT_DIR/tools/bin" PATH="$TOOLS_DIR:$PATH" -run_cmd() +unica() { local CMD=$1 local CMDS CMDS="$(find "scripts" -mindepth 1 -maxdepth 1 -type f -printf '%f\n' | sed "s/.sh//g")" + local YELLOW="\033[1;33m" + local CYAN="\033[1;36m" + local RESET_COLOR="\033[0m" + if [ -z "$CMD" ] || [ "$CMD" = "-h" ]; then - echo -e "Available cmds:\n$CMDS" + echo -e "Available cmds:" + for script in "scripts"/*.sh; do + CMD_NAME=$(basename "$script" .sh) + HELP_LINES=$(grep "^# CMD_HELP" "$script" | sed 's/^# CMD_HELP[[:space:]]*//') + + if [ -n "$HELP_LINES" ]; then + echo -e "${YELLOW}$CMD_NAME${RESET_COLOR}:" + echo -e "${CYAN}$HELP_LINES${RESET_COLOR}" + echo "" + else + echo -e "${YELLOW}$CMD_NAME${RESET_COLOR} - No help available" + fi + done return 1 elif ! echo "$CMDS" | grep -q -w "$CMD"; then echo "\"$CMD\" is not valid." @@ -50,31 +128,52 @@ run_cmd() bash -e "$SRC_DIR/scripts/$CMD.sh" "$@" fi } + # ] -TARGETS="$(ls "$SRC_DIR/target")" +TARGETS=($(ls "$SRC_DIR/target")) -if [ "$#" != 1 ]; then +if [ "$#" -ne 1 ]; then echo "Usage: source buildenv.sh " - echo -e "Available devices:\n$TARGETS" - return 1 -elif ! echo "$TARGETS" | grep -q -w "$1"; then - echo "\"$1\" is not valid target." - echo -e "Available devices:\n$TARGETS" - return 1 + echo "No target specified. Please choose from the available devices below:" + + select TARGET in "${TARGETS[@]}"; do + if [ -n "$TARGET" ]; then + echo "You selected: $TARGET" + export SELECTED_TARGET="$TARGET" + break + else + echo "Invalid selection. Please try again." + fi + done + + if [ -z "$SELECTED_TARGET" ]; then + echo "No valid target selected. Exiting." + return 1 + fi else - mkdir -p "$OUT_DIR" - run_cmd build_dependencies || return 1 - [ -f "$OUT_DIR/config.sh" ] && unset $(sed "/Automatically/d" "$OUT_DIR/config.sh" | cut -d= -f1) - bash "$SRC_DIR/scripts/internal/gen_config_file.sh" "$1" || return 1 - source "$OUT_DIR/config.sh" - - echo "==============================" - sed "/Automatically/d" "$OUT_DIR/config.sh" - echo "==============================" + if ! printf "%s\n" "${TARGETS[@]}" | grep -q -w "$1"; then + echo "\"$1\" is not a valid target." + echo "Available devices:" + printf "%s\n" "${TARGETS[@]}" + return 1 + fi + SELECTED_TARGET="$1" +fi + +mkdir -p "$OUT_DIR" +unica build_dependencies || return 1 + +if [ -f "$OUT_DIR/config.sh" ]; then + unset $(sed "/Automatically/d" "$OUT_DIR/config.sh" | cut -d= -f1) fi -unset TARGETS -set +o allexport +bash "$SRC_DIR/scripts/internal/gen_config_file.sh" "$SELECTED_TARGET" || return 1 +source "$OUT_DIR/config.sh" + +echo "==============================" +sed "/Automatically/d" "$OUT_DIR/config.sh" +echo "type "unica -h" for help" + return 0 diff --git a/scripts/apktool.sh b/scripts/apktool.sh index 9c5be7df..e012ebb6 100755 --- a/scripts/apktool.sh +++ b/scripts/apktool.sh @@ -16,6 +16,11 @@ # along with this program. If not, see . # +# CMD_HELP Usage: apktool d[ecode]/b[uild] (...) +# CMD_HELP APK/JAR path MUST not be full and match an existing file inside work_dir +# CMD_HELP Output files will be stored in ($APKTOOL_DIR) +# CMD_HELP Recompiled apk will be copied back to its original directory + set -eu shopt -s nullglob diff --git a/scripts/build_fs_image.sh b/scripts/build_fs_image.sh index 28c407ca..65ae4b07 100755 --- a/scripts/build_fs_image.sh +++ b/scripts/build_fs_image.sh @@ -16,6 +16,8 @@ # along with this program. If not, see . # +# CMD_HELP Builds the finished system.img... if already at that point. + set -eu # [ diff --git a/scripts/cleanup.sh b/scripts/cleanup.sh index f47a2921..8041198c 100755 --- a/scripts/cleanup.sh +++ b/scripts/cleanup.sh @@ -16,6 +16,14 @@ # along with this program. If not, see . # +# CMD_HELP Available options (multiple can be accepted): +# CMD_HELP "all" +# CMD_HELP "odin" +# CMD_HELP "fw" +# CMD_HELP "apktool" +# CMD_HELP "work_dir" +# CMD_HELP "tools" + set -eu ALL=false diff --git a/scripts/download_fw.sh b/scripts/download_fw.sh old mode 100755 new mode 100644 index d5b28747..ef26c1d1 --- a/scripts/download_fw.sh +++ b/scripts/download_fw.sh @@ -18,6 +18,8 @@ # shellcheck disable=SC2162 +# CMD_HELP Downloads the Firmware needed for selected device. + set -e # [ @@ -27,25 +29,62 @@ GET_LATEST_FIRMWARE() | grep latest | sed 's/^[^>]*>//' | sed 's/<.*//' } -DOWNLOAD_FIRMWARE() -{ +DOWNLOAD_FIRMWARE() { local PDR PDR="$(pwd)" - cd "$ODIN_DIR" - { samfirm -m "$MODEL" -r "$REGION" -i "$IMEI" > /dev/null; } 2>&1 \ - && touch "$ODIN_DIR/${MODEL}_${REGION}/.downloaded" \ - || exit 1 - [ -f "$ODIN_DIR/${MODEL}_${REGION}/.downloaded" ] && { - echo -n "$(find "$ODIN_DIR/${MODEL}_${REGION}" -name "AP*" -exec basename {} \; | cut -d "_" -f 2)/" - echo -n "$(find "$ODIN_DIR/${MODEL}_${REGION}" -name "CSC*" -exec basename {} \; | cut -d "_" -f 3)/" - echo -n "$(find "$ODIN_DIR/${MODEL}_${REGION}" -name "CP*" -exec basename {} \; | cut -d "_" -f 2)" - } >> "$ODIN_DIR/${MODEL}_${REGION}/.downloaded" + cd "$ODIN_DIR" || { echo "Failed to enter ODIN_DIR: $ODIN_DIR"; exit 1; } + + # Directory for firmware download + local TARGET_DIR="${ODIN_DIR}/${MODEL}_${REGION}" + + # Ensure the target directory exists + if [ ! -d "$TARGET_DIR" ]; then + mkdir -p "$TARGET_DIR" || { echo "Failed to create directory: $TARGET_DIR"; exit 1; } + fi + + # Determine whether to use -s or -i flag based on the length of $IMEI + local SAMLOADER_FLAG + if [ "${#IMEI}" -eq 11 ]; then + SAMLOADER_FLAG="-s $IMEI" + else + SAMLOADER_FLAG="-i $IMEI" + fi + + # Run samloader and check for success + echo "Downloading firmware for $MODEL in region $REGION..." + if ! samloader -m "$MODEL" -r "$REGION" $SAMLOADER_FLAG download -O "$TARGET_DIR"; then + echo "Firmware download failed." + exit 1 + fi + + # Mark download as successful + touch "$TARGET_DIR/.downloaded" + + # Find and unzip firmware zip + local ZIP_FILE + ZIP_FILE=$(find "$TARGET_DIR" -name "*.zip" | head -n 1) + if [ -f "$ZIP_FILE" ]; then + echo "Unzipping $ZIP_FILE..." + unzip -q "$ZIP_FILE" -d "$TARGET_DIR" || { echo "Failed to unzip $ZIP_FILE"; exit 1; } + rm "$ZIP_FILE" # Optional: Remove the ZIP file after extracting + else + echo "No ZIP file found in $TARGET_DIR" + exit 1 + fi + + # Write firmware details to .downloaded file + { + echo -n "$(find "$TARGET_DIR" -name "AP*" -exec basename {} \; | cut -d "_" -f 2)/" + echo -n "$(find "$TARGET_DIR" -name "CSC*" -exec basename {} \; | cut -d "_" -f 3)/" + echo -n "$(find "$TARGET_DIR" -name "CP*" -exec basename {} \; | cut -d "_" -f 2)" + } >> "$TARGET_DIR/.downloaded" echo "" - cd "$PDR" + cd "$PDR" || exit } + FIRMWARES=( "$SOURCE_FIRMWARE" "$TARGET_FIRMWARE" ) IFS=':' read -a SOURCE_EXTRA_FIRMWARES <<< "$SOURCE_EXTRA_FIRMWARES" if [ "${#SOURCE_EXTRA_FIRMWARES[@]}" -ge 1 ]; then diff --git a/scripts/extract_fw.sh b/scripts/extract_fw.sh index cce26f2e..2f1f52ad 100755 --- a/scripts/extract_fw.sh +++ b/scripts/extract_fw.sh @@ -18,6 +18,8 @@ # shellcheck disable=SC2162 +# CMD_HELP Extracts downloaded Firmware + set -e # [ diff --git a/scripts/make_rom.sh b/scripts/make_rom.sh index 68eb2f99..a8d95a76 100755 --- a/scripts/make_rom.sh +++ b/scripts/make_rom.sh @@ -16,6 +16,8 @@ # along with this program. If not, see . # +# CMD_HELP Executes series of commands. Downloads, extracts, patches and packages the Rom. + set -Eeuo pipefail START=$SECONDS diff --git a/scripts/print_modules_info.sh b/scripts/print_modules_info.sh index ac8960c0..ca09095f 100755 --- a/scripts/print_modules_info.sh +++ b/scripts/print_modules_info.sh @@ -18,6 +18,8 @@ # shellcheck disable=SC2001 +# CMD_HELP Prints the available module informations. + set -Ee # [