|
| 1 | +#!/bin/sh |
| 2 | +set -eu |
| 3 | + |
| 4 | +GKI_ROOT=$(pwd) |
| 5 | +OWNER="KernelSU-Next" |
| 6 | +REPO="$OWNER" |
| 7 | + |
| 8 | +display_usage() { |
| 9 | + echo "Usage: $0 [--cleanup | <commit-or-tag>]" |
| 10 | + echo " --cleanup: Cleans up previous modifications made by the script." |
| 11 | + echo " <commit-or-tag>: Sets up or updates the KernelSU-Next to specified tag or commit." |
| 12 | + echo " -h, --help: Displays this usage information." |
| 13 | + echo " (no args): Sets up or updates the KernelSU-Next environment to the latest tagged version." |
| 14 | +} |
| 15 | + |
| 16 | +initialize_variables() { |
| 17 | + if test -d "$GKI_ROOT/common/drivers"; then |
| 18 | + DRIVER_DIR="$GKI_ROOT/common/drivers" |
| 19 | + elif test -d "$GKI_ROOT/drivers"; then |
| 20 | + DRIVER_DIR="$GKI_ROOT/drivers" |
| 21 | + else |
| 22 | + echo '[ERROR] "drivers/" directory not found.' |
| 23 | + exit 127 |
| 24 | + fi |
| 25 | + |
| 26 | + DRIVER_MAKEFILE=$DRIVER_DIR/Makefile |
| 27 | + DRIVER_KCONFIG=$DRIVER_DIR/Kconfig |
| 28 | +} |
| 29 | + |
| 30 | +# Reverts modifications made by this script |
| 31 | +perform_cleanup() { |
| 32 | + echo "[+] Cleaning up..." |
| 33 | + [ -L "$DRIVER_DIR/kernelsu" ] && rm "$DRIVER_DIR/kernelsu" && echo "[-] Symlink removed." |
| 34 | + grep -q "kernelsu" "$DRIVER_MAKEFILE" && sed -i '/kernelsu/d' "$DRIVER_MAKEFILE" && echo "[-] Makefile reverted." |
| 35 | + grep -q "drivers/kernelsu/Kconfig" "$DRIVER_KCONFIG" && sed -i '/drivers\/kernelsu\/Kconfig/d' "$DRIVER_KCONFIG" && echo "[-] Kconfig reverted." |
| 36 | + if [ -d "$GKI_ROOT/$REPO" ]; then |
| 37 | + rm -rf "$GKI_ROOT/$REPO" && echo "[-] $REPO directory deleted." |
| 38 | + fi |
| 39 | +} |
| 40 | + |
| 41 | +# Sets up or update KernelSU-Next environment |
| 42 | +setup_kernelsu() { |
| 43 | + echo "[+] Setting up $REPO..." |
| 44 | + test -d "$GKI_ROOT/$REPO" || git clone "https://github.com/$OWNER/$REPO" && echo "[+] Repository cloned." |
| 45 | + cd "$GKI_ROOT/$REPO" |
| 46 | + git stash && echo "[-] Stashed current changes." |
| 47 | + |
| 48 | + BRANCH="$(git rev-parse --abbrev-ref origin/HEAD | sed 's@^origin/@@')" |
| 49 | + if [ "$(git status | grep -Po 'v\d+(\.\d+)*' | head -n1)" ]; then |
| 50 | + git checkout $BRANCH && echo "[-] Switched to $BRANCH branch." |
| 51 | + fi |
| 52 | + |
| 53 | + git pull && echo "[+] Repository updated." |
| 54 | + if [ -z "${1-}" ]; then |
| 55 | + git checkout legacy && echo "[-] Checked out legacy branch." |
| 56 | + else |
| 57 | + git checkout "$1" && echo "[-] Checked out $1." || echo "[-] Checkout default branch" |
| 58 | + fi |
| 59 | + cd "$DRIVER_DIR" |
| 60 | + ln -sf "$(realpath --relative-to="$DRIVER_DIR" "$GKI_ROOT/$REPO/kernel")" "kernelsu" && echo "[+] Symlink created." |
| 61 | + |
| 62 | + # Add entries in Makefile and Kconfig if not already existing |
| 63 | + grep -q "kernelsu" "$DRIVER_MAKEFILE" || printf "\nobj-\$(CONFIG_KSU) += kernelsu/\n" >> "$DRIVER_MAKEFILE" && echo "[+] Modified Makefile." |
| 64 | + grep -q "source \"drivers/kernelsu/Kconfig\"" "$DRIVER_KCONFIG" || sed -i "/endmenu/i\source \"drivers/kernelsu/Kconfig\"" "$DRIVER_KCONFIG" && echo "[+] Modified Kconfig." |
| 65 | + echo '[+] Done.' |
| 66 | +} |
| 67 | + |
| 68 | +# Process command-line arguments |
| 69 | +if [ "$#" -eq 0 ]; then |
| 70 | + initialize_variables |
| 71 | + setup_kernelsu |
| 72 | +elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then |
| 73 | + display_usage |
| 74 | +elif [ "$1" = "--cleanup" ]; then |
| 75 | + initialize_variables |
| 76 | + perform_cleanup |
| 77 | +else |
| 78 | + initialize_variables |
| 79 | + setup_kernelsu "$@" |
| 80 | +fi |
| 81 | + |
| 82 | +echo 'CONFIG_KSU=y' >> arch/arm64/configs/nemo_defconfig |
| 83 | +echo 'CONFIG_KSU_MANUAL_HOOK=y' >> arch/arm64/configs/nemo_defconfig |
| 84 | + |
| 85 | +echo "Done, now apply the ksu hooks from root/patches/ksu_next_hooks/* while being in the kernel root directory" |
0 commit comments