NicolasWebDev / reinstall-magisk-on-lineageos

Small bash script to reinstall magisk after each LineageOS update
72 stars 15 forks source link

Some possible additions to the script #35

Open bfg01 opened 1 year ago

bfg01 commented 1 year ago

I came with some few ideas for the script: --Automatically detect ADB serial number for device --A menu for listing all detected devices in case of more than one detected, and give the user to choose which one --Alternate way of getting the needed stuff from the LineageOS website without forcibly needing jq --Support for the last 4 LOS versions (which come to be the ones always at the LOS downloads page anyway)

# List detected devices
DEV_LIST_FULL="$(adb devices -l | tail -n +2)"

[ -n "$DEV_LIST_FULL" ] || { echo "No devices detected. Exiting."; exit 2; }

if [ $(echo "$DEV_LIST_FULL" | wc -l) -eq 1 ]; then
  DEV_SERIAL="$(echo $DEV_LIST_FULL | awk '{ print $1 }')"
  # Alternate way : echo $DEV_LIST_FULL | cut -d $'\t' -f 1
  echo "Using only detected device '$DEV_SERIAL'"
else
  echo "Listing detected devices:"
  echo
  # Sub-routine for numbering lines of device list
  count=1
  NUM_LIST="$(while read -r line; do \
    printf '%s   %s\n' "$((count++))" "$line"; \
  done <<< $DEV_LIST_FULL)"
  unset count
  # End of sub-routine
  echo "$NUM_LIST"
  echo
  echo -n "Select number of device to use; press '0' to just cancel and exit: "
  while read -r -n 1 num; do
    if [ $num = 0 ]; then
      printf "\n%s\n" "Exiting"
      exit 0
    elif echo "$NUM_LIST" | grep -q "^$num"; then
      DEV_SERIAL=$(echo "$NUM_LIST" | grep "^$num" | awk '{ print $2 }')
      printf "\n%s\n" "Using selected device '$DEV_SERIAL'"
      break
    else
      printf "\n%s" "Invalid option, try again (option '0' to exit): "
    fi
  done
fi

DEVICE_NAME="$(adb -s "$DEV_SERIAL" shell getprop ro.lineage.device)"
LOS_VERSION="$(adb -s "$DEV_SERIAL" shell getprop ro.lineage.version)"

ZIP_DNL_URL=$(curl -sL https://download.lineageos.org/api/v2/devices/"$DEVICE_NAME"/builds | \
grep -io "https://mirrorbits[^\"]*$LOS_VERSION-signed\.zip")

ZIP_NAME="$(basename $ZIP_DNL_URL)"

ZIP_SHA256=$(curl -sL https://download.lineageos.org/api/v2/devices/"$DEVICE_NAME"/builds | \
grep -ioP "^.*?\K$LOS_VERSION-signed\.zip.*?\K[0-9a-f]{64}.*?")

IMG_DNL_URL=$(curl -sL https://download.lineageos.org/api/v2/devices/"$DEVICE_NAME"/builds | \
grep -ioP "^.*?\K$LOS_VERSION-signed\.zip.*?\Khttps://mirrorbits[^\"]*boot\.img.*?")

IMG_SHA256=$(curl -sL https://download.lineageos.org/api/v2/devices/"$DEVICE_NAME"/builds | \
grep -ioP "^.*?\K$LOS_VERSION-signed\.zip.*?\Kboot\.img.*?\K[0-9a-f]{64}.*?")

# Alternate way to "curl -sL" with wget: "wget -qO -"
# Alternate way to "wget -c" with curl: "curl -LJOC -"

For now, for starters, would some of this be ok enough?

Thanks.

bfg01 commented 1 year ago

On second thought, the numbering lines whole sub-routine could be done much simpler with awk: NUM_LIST="$(awk '{ printf "%s %s\n", NR, $0 }' <<< $DEV_LIST_FULL)"