Skip to content

Commit

Permalink
try setup-subm
Browse files Browse the repository at this point in the history
  • Loading branch information
= mlm-games committed Sep 10, 2024
1 parent dfd85b2 commit c5af05c
Show file tree
Hide file tree
Showing 3 changed files with 375 additions and 16 deletions.
63 changes: 63 additions & 0 deletions kernel/setup-subm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/sh
set -eu

KERNEL_DIR=$(pwd)

display_usage() {
echo "Usage: $0 [--cleanup | <commit-or-tag>]"
echo " --cleanup: Cleans up previous modifications made by the script."
echo " <commit-or-tag>: Sets up or updates the KernelSU to specified tag or commit."
echo " -h, --help: Displays this usage information."
echo " (no args): Sets up or updates the KernelSU environment to the latest tagged version."
}

initialize_variables() {
if test -d "$KERNEL_DIR/common/drivers"; then
DRIVER_DIR="$KERNEL_DIR/common/drivers"
elif test -d "$KERNEL_DIR/drivers"; then
DRIVER_DIR="$KERNEL_DIR/drivers"
else
echo '[ERROR] "drivers/" directory not found.'
exit 127
fi

DRIVER_MAKEFILE=$DRIVER_DIR/Makefile
DRIVER_KCONFIG=$DRIVER_DIR/Kconfig
}

# Reverts modifications made by this script
perform_cleanup() {
echo "[+] Cleaning up..."
[ -L "$DRIVER_DIR/kernelsu" ] && rm "$DRIVER_DIR/kernelsu" && echo "[-] Symlink removed."
grep -q "kernelsu" "$DRIVER_MAKEFILE" && sed -i '/kernelsu/d' "$DRIVER_MAKEFILE" && echo "[-] Makefile reverted."
grep -q "drivers/kernelsu/Kconfig" "$DRIVER_KCONFIG" && sed -i '/drivers\/kernelsu\/Kconfig/d' "$DRIVER_KCONFIG" && echo "[-] Kconfig reverted."
if [ -d "$KERNEL_DIR/KernelSU" ]; then
rm -rf "$KERNEL_DIR/KernelSU" && echo "[-] KernelSU directory deleted."
fi
}

# Sets up or update KernelSU environment
setup_kernelsu() {
echo "[+] Setting up KernelSU..."
test -d "$KERNEL_DIR/KernelSU"
git submodule add https://github.com/mlm-games/KernelSU-Non-GKI KernelSU
git submodule update --init --recursive
# Add entries in Makefile and Kconfig if not already existing
grep -q "kernelsu" "$DRIVER_MAKEFILE" || printf "\nobj-\$(CONFIG_KSU) += kernelsu/\n" >> "$DRIVER_MAKEFILE" && echo "[+] Modified Makefile."
grep -q "source \"drivers/kernelsu/Kconfig\"" "$DRIVER_KCONFIG" || sed -i "/endmenu/i\source \"drivers/kernelsu/Kconfig\"" "$DRIVER_KCONFIG" && echo "[+] Modified Kconfig."
echo '[+] Done.'
}

# Process command-line arguments
if [ "$#" -eq 0 ]; then
initialize_variables
setup_kernelsu
elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
display_usage
elif [ "$1" = "--cleanup" ]; then
initialize_variables
perform_cleanup
else
initialize_variables
setup_kernelsu "$@"
fi
66 changes: 50 additions & 16 deletions kernel/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,60 @@ perform_cleanup() {

# Sets up or update KernelSU environment
setup_kernelsu() {
echo "[+] Setting up KernelSU..."
test -d "$GKI_ROOT/KernelSU" || git clone https://github.com/mlm-games/KernelSU-Non-GKI KernelSU && echo "[+] Repository cloned."
cd "$GKI_ROOT/KernelSU"
git stash && echo "[-] Stashed current changes."
if [ "$(git status | grep -Po 'v\d+(\.\d+)*' | head -n1)" ]; then
git checkout main && echo "[-] Switched to main branch."
local ksu_dir="$GKI_ROOT/KernelSU"
local current_tag

echo "[+] Setting up KernelSU environment..."

# Clone KernelSU if it doesn't exist
if [[ ! -d "$ksu_dir" ]]; then
git clone https://github.com/mlm-games/KernelSU-Non-GKI "$ksu_dir" || { echo "[-] Failed to clone KernelSU repository."; return 1; }
echo "[+] KernelSU repository cloned."
fi
git pull && echo "[+] Repository updated."
if [ -z "${1-}" ]; then
git checkout "$(git describe --abbrev=0 --tags)" && echo "[-] Checked out latest tag."

cd "$ksu_dir" || { echo "[-] Unable to change directory to $ksu_dir"; return 1; }

# Stash any local changes
git stash 2>/dev/null && echo "[-] Stashed current changes."

# Ensure we're on the main branch or a tagged version
if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
current_tag=$(git describe --tags --exact-match 2>/dev/null)
if [[ -z "$current_tag" ]]; then
git checkout main && echo "[-] Switched to main branch."
fi
else
git checkout "$1" && echo "[-] Checked out $1." || echo "[-] Checkout default branch"
echo "[-] Not in a git repository, skipping branch check."
fi

# Pull the latest changes
git pull --ff-only && echo "[+] Updated repository." || { echo "[-] Failed to update repository."; return 1; }

# Checkout to the specified or latest tag
if [[ -z "${1-}" ]]; then
git checkout $(git describe --tags $(git rev-list --tags --max-count=1)) && echo "[-] Checked out latest tag."
else
git checkout "$1" 2>/dev/null && echo "[-] Checked out $1." || { echo "[-] Failed to checkout $1, staying on current branch/tag."; }
fi

# Return to the driver directory
cd "$DRIVER_DIR" || { echo "[-] Unable to return to $DRIVER_DIR"; return 1; }

# Create symlink
ln -sfn "$(realpath --relative-to="$DRIVER_DIR" "$ksu_dir/kernel")" "kernelsu" && echo "[+] Symlink to kernelsu created."

# Modify Makefile if necessary
if ! grep -q "kernelsu" "$DRIVER_MAKEFILE"; then
echo -e "\nobj-\$(CONFIG_KSU) += kernelsu/\n" >> "$DRIVER_MAKEFILE"
echo "[+] Added kernelsu to Makefile."
fi

# Modify Kconfig if necessary
if ! grep -q "source \"drivers/kernelsu/Kconfig\"" "$DRIVER_KCONFIG"; then
sed -i '/endmenu/i\source "drivers/kernelsu/Kconfig"' "$DRIVER_KCONFIG" && echo "[+] Added kernelsu to Kconfig."
fi
cd "$DRIVER_DIR"
ln -sf "$(realpath --relative-to="$DRIVER_DIR" "$GKI_ROOT/KernelSU/kernel")" "kernelsu" && echo "[+] Symlink created."

# Add entries in Makefile and Kconfig if not already existing
grep -q "kernelsu" "$DRIVER_MAKEFILE" || printf "\nobj-\$(CONFIG_KSU) += kernelsu/\n" >> "$DRIVER_MAKEFILE" && echo "[+] Modified Makefile."
grep -q "source \"drivers/kernelsu/Kconfig\"" "$DRIVER_KCONFIG" || sed -i "/endmenu/i\source \"drivers/kernelsu/Kconfig\"" "$DRIVER_KCONFIG" && echo "[+] Modified Kconfig."
echo '[+] Done.'
echo '[+] KernelSU setup completed.'
}

# Process command-line arguments
Expand Down
Loading

0 comments on commit c5af05c

Please sign in to comment.