Commit Diff


commit - 884c735fd00a960ac7a1b6d7ee859e74cabdb5a9
commit + 809a9321ab94333aef2bf7d40994c767ee48b7f1
blob - 5a4efc039df2dfa375adb1a7c73b19f5f5767d52
blob + 45710ddfd8c766531457413a34a776436556ff5d
--- Alpine Linux/ap
+++ Alpine Linux/ap
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 # Alpine aports auto-update and selective rebuild script
 
 ### LICENSE ###
@@ -22,9 +22,9 @@
 # Isabella      <https://github.com/Bean6754/>
 ###############
 
-# Current deps: bash, rsync, git, alpine-sdk, abuild-rootbld
+# Current deps: rsync, git, alpine-sdk, abuild-rootbld
 
-args=("$@")
+args="$1"
 
 APORTS_DIR="$HOME/aports"
 CUSTOM_DIR="$HOME/aports-custom"
@@ -32,7 +32,25 @@ LOGFILE="$HOME/aports-update.log"
 
 
 ## Functions.
-function helpmsg() {
+enforce_noroot() {
+  # Test if user and exit 1 if root.
+  # Make sure root cannot run our script.
+  if [ "$(id -u)" -eq 0 ]; then
+    echo "Must be run as user, not root." 1>&2
+    exit 1
+  fi
+}
+
+enforce_root() {
+  # Test if root and exit 1 if not root.
+  # Make sure only root can run our script.
+  if [ "$(id -u)" -ne 0 ]; then
+    echo "Must be run as root." 1>&2
+    exit 1
+  fi
+}
+
+helpmsg() {
   printf "AP: Alpine Ports Package Manager\n"
   printf "Commands:\n"
   printf "  (h)elp,    --help,     -h:     Show help\n"
@@ -46,7 +64,7 @@ function helpmsg() {
   exit 1
 }
 
-function minihelp() {
+minihelp() {
   printf "AP: Alpine Ports Package Manager\n"
   printf "Basic usage: ap COMMAND <OPTIONS>\n"
   printf "To show help:\n"
@@ -55,7 +73,22 @@ function minihelp() {
   exit 1
 }
 
-function synctree() {
+synctree() {
+  # Do NOT allow root access.
+  enforce_noroot
+
+  # Check if aports dir exists, create if missing.
+  if [ ! -f /etc/alpine-release ]; then
+    echo "'/etc/alpine-release' not found."
+    exit 1
+  fi
+  if [ ! -d "$APORTS_DIR" ]; then
+    mkdir -p "$APORTS_DIR"
+    alpine_release=$(head -n1 /etc/alpine-release)
+    branch="v$alpine_release"
+    git clone --branch $branch https://gitlab.alpinelinux.org/alpine/aports "$APORTS_DIR"
+  fi
+
   # Ensure directories exist
   mkdir -p "$APORTS_DIR" "$CUSTOM_DIR"
 
@@ -74,7 +107,15 @@ function synctree() {
 }
 
 
-function update() {
+update() {
+  # Do NOT allow root access.
+  enforce_noroot
+
+  # Check if aports tree exists, run 'synctree' if missing.
+  if [ ! -d "$APORTS_DIR" ]; then
+    synctree
+  fi
+
   echo "=== Rebuilding selected packages ===" | tee -a "$LOGFILE"
   # List packages you want to rebuild
   PACKAGES="$(cat /etc/apk/world)"
@@ -90,15 +131,41 @@ function update() {
   echo "=== Done ===" | tee -a "$LOGFILE"
 }
 
+fixworld() {
+  # Test if root and exit 1 if not root.
+  enforce_root
+
+  # Test if sed exists as root.
+  if ! command -v sed >/dev/null 2>&1; then
+    echo "Error: 'sed' not found, exiting."
+    exit 1
+  fi
+
+  # This requires root as we are editing '/etc/apk/world' with sed.
+  echo "=== Rebuilding /etc/apk/world ===" | tee -a "$LOGFILE"
+  sed -i 's/\([a-zA-Z0-9._+-]\+\).*/\1/' /etc/apk/world
+
+  echo "=== Done ===" | tee -a "$LOGFILE"
+}
+
 ## Main.
-if [[ "$args" = "-h" || "$args" = "--help" || "$args" = "h" || "$args" = "help" ]]; then
-  helpmsg
-elif [[ "$args" = "" || "$args" = " " ]]; then
-  minihelp
-elif [[  "$args" = "-s" || "$args" = "--sync" || "$args" = "sync" || "$args" = "s" ]]; then
-  synctree
-elif [[  "$args" = "-u" || "$args" = "--update" || "$args" = "update" || "$args" = "up" ]]; then
-  update
-else
-  minihelp
-fi
+case "$args" in
+  -h|--help|h|help)
+    helpmsg
+    ;;
+  ""|" ")
+    minihelp
+    ;;
+  -s|--sync|sync|s)
+    synctree
+    ;;
+  -u|--update|update|up)
+    update
+    ;;
+  -fw|--fixworld|fixworld|fw)
+    fixworld
+    ;;
+  *)
+    minihelp
+    ;;
+esac