Commit Diff


commit - 809a9321ab94333aef2bf7d40994c767ee48b7f1
commit + 5684f566062d49b663345a2b2b72170fa82684c7
blob - 45710ddfd8c766531457413a34a776436556ff5d
blob + 4412b6cb31a3c2f000c60a8fda217fcb16acf8b7
--- Alpine Linux/ap
+++ Alpine Linux/ap
@@ -26,9 +26,11 @@
 
 args="$1"
 
-APORTS_DIR="$HOME/aports"
-CUSTOM_DIR="$HOME/aports-custom"
-LOGFILE="$HOME/aports-update.log"
+APORTS_DIR="/usr/ap/aports"
+CUSTOM_DIR="/usr/ap/aports-custom"
+LOGFILE="/var/log/aports-update.log"
+APORTS_PKG="/usr/ap/home/packages"
+APORTS_HOME="/usr/ap/home"
 
 
 ## Functions.
@@ -51,31 +53,67 @@ enforce_root() {
 }
 
 helpmsg() {
-  printf "AP: Alpine Ports Package Manager\n"
+  printf "AP: Alpine Ports helper\n"
   printf "Commands:\n"
-  printf "  (h)elp,    --help,     -h:     Show help\n"
-  printf "  (s)ync,    --sync,     -s:     Sync the ~/aports tree\n"
-  printf "  (u)pdate,  --update,   -u:     Upgrade packages from the ~/aports tree\n"
+  printf "  help,    h,    --help,     -h:     Show help\n"
+  printf "  setup,   S,    --setup     -S:     Run initial setup script\n"
+  printf "  sync,    s,    --sync,     -s:     Sync the ~/aports tree\n"
+  printf "  update,  up,   --update,   -u:     Upgrade packages from the ~/aports tree\n"
   printf "\n"
   printf "Example usage:\n"
-  printf "  Update local-ports tree (~/aports):       ap sync\n"
-  printf "  Upgrade packages from local-ports tree:   ap update\n"
+  printf "  Run initial setup:                     ap setup\n"
+  printf "  Update ports tree (/usr/ap/aports):    ap sync\n"
+  printf "  Upgrade packages from ports tree:      ap update\n"
 
-  exit 1
+  exit
 }
 
 minihelp() {
-  printf "AP: Alpine Ports Package Manager\n"
+  printf "AP: Alpine Ports helper\n"
   printf "Basic usage: ap COMMAND <OPTIONS>\n"
   printf "To show help:\n"
   printf "  ap help\n"
 
-  exit 1
+  exit
 }
 
+setup_ap() {
+  # Only allow root access.
+  enforce_root
+  mkdir -p $APORTS_DIR $CUSTOM_DIR $APORTS_PKG
+  # Create Alpine build non-root user if not already exists.
+  adduser -D -h "$APORTS_HOME" -s /bin/sh -G abuild _ap
+  # Add build flags.
+  mkdir -p "$APORTS_HOME/.abuild"
+  echo "ABUILD_DEFAULT_OPTS='options=!check'" >> "$APORTS_HOME/.abuild/abuild.conf"
+  chown -R _ap:abuild $APORTS_HOME
+  # Generate abuild keys.
+  su _ap -c "abuild-keygen -a"
+  # Take ownership of directories for build-user.
+  chown -R _ap:abuild $APORTS_DIR $CUSTOM_DIR $APORTS_PKG
+
+  exit
+}
+sanity_check() {
+  # Check folders exist.
+  if [ ! -d $APORTS_DIR ] || [ ! -d $CUSTOM_DIR ] || [ ! -d $APORTS_PKG ]; then
+    echo "Error: Crucial directories do not exist."
+    echo "Please run 'ap setup' as root."
+    exit 1
+  fi
+  # Check user account.
+  if ! grep -q '_ap' /etc/passwd; then
+    echo "Error: Build-user account does not exist."
+    echo "Please run 'ap setup' as root."
+    exit 1
+  fi
+}
+
 synctree() {
-  # Do NOT allow root access.
-  enforce_noroot
+  # Only allow root access.
+  enforce_root
+  # Sanity check.
+  sanity_check
 
   # Check if aports dir exists, create if missing.
   if [ ! -f /etc/alpine-release ]; then
@@ -106,10 +144,30 @@ synctree() {
   echo "=== Done ===" | tee -a "$LOGFILE"
 }
 
+fixworld() {
+  # Only allow root acces.
+  enforce_root
+  # Sanity check.
+  sanity_check
 
+  # 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"
+}
+
 update() {
-  # Do NOT allow root access.
-  enforce_noroot
+  # Only allow root access.
+  enforce_root
+  # Sanity check.
+  sanity_check
 
   # Check if aports tree exists, run 'synctree' if missing.
   if [ ! -d "$APORTS_DIR" ]; then
@@ -125,29 +183,20 @@ update() {
     if [ -n "$PKG_PATH" ]; then
       echo "Building $pkg..." | tee -a "$LOGFILE"
       cd "$PKG_PATH" || continue
-      abuild rootbld || echo "Failed to build $pkg" | tee -a "$LOGFILE"
+      # Compile.
+      su _ap -c "abuild rootbld" || echo "Failed to build $pkg" | tee -a "$LOGFILE"
+      # Install.
+      PKG_FILE=$(find "$APORTS_PKG" -type f -name "${pkg}-[0-9]*.apk" | head -n 1)
+      if [ -n "$PKG_FILE" ]; then
+        apk add --allow-untrusted "$PKG_FILE"
+      else
+        echo "Package for $pkg not found" | tee -a "$LOGFILE"
+      fi
     fi
   done
   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.
 case "$args" in
   -h|--help|h|help)
@@ -156,6 +205,9 @@ case "$args" in
   ""|" ")
     minihelp
     ;;
+  -S|--setup|setup|S)
+    setup_ap
+    ;;
   -s|--sync|sync|s)
     synctree
     ;;