#!/bin/bash ################################################################ # Tools for formatting / styling # Define terminal color codes RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[0;33m' BLUE='\033[0;36m' NC='\033[0m' # No Color # Echo with colors colorecho() { color="$1" text="$2" printf "${color}${text}${NC}\n" } # Sudo override sudo () { local command=$@ if [ "$(id -u)" -eq 0 ]; then #echo "Running in root." command "$@" else #echo "Running with sudo." command sudo "$@" fi } ################################################################ # Configure variables gh_repo="kwankiu/archlinux-installer" file_prefix="" file_suffix="-archlinux-installer" file_extension=".img.xz" ################################################################ # Loop through the arguments for arg in "$@"; do case "$arg" in -h | --help) colorecho "$BLUE" "Get Installer" echo "Usage: $0 " colorecho "$GREEN" "Options" echo "-h / --help : Usage and Infomation of this Tool." echo "--tag : Specify a release tag for image downloads." echo "--device : Specify a target device for image downloads." exit 1 ;; --tag=*) release_tag="${arg#*=}" ;; --device=*) install_target="${arg#*=}" ;; --disk=*) install_disk="${arg#*=}" ;; -*) colorecho "$RED" "Invalid command or argument." exit 1 ;; esac done ################################################################ # Get latest release tag if [ -z "$release_tag" ]; then release_tag="$(curl -s "https://api.github.com/repos/${gh_repo}/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')" fi # Auto detect install target from devicetree if [ -z "$install_target" ]; then if [ -f "/proc/device-tree/compatible" ]; then install_target="$(sed 's/\x00/ /g; s/,/ /g' /proc/device-tree/compatible | awk '{print $1, $2}' | tr ' ' '-' | head -n 1)" fi fi ################################################################ if [ -n "$install_target" ]; then # Download target image colorecho "$GREEN" "Looking for the latest image for $install_target ..." case $install_target in radxa-*) ;; khadas-*) install_target="$(echo "${install_target}" | awk -F'-' '{print substr($2,1)}')-khadas" ;; rockchip-rk3588*) install_target="${install_target#*-*-}" case "$install_target" in *12c*|*fydetab*) install_target="fydetab-duo" ;; esac ;; friendlyelec*) install_target="${install_target#*-}" ;; *) colorecho "$RED" "Unable to determine target device" exit 1 ;; esac case "$release_tag" in UEFI*) image_url="https://github.com/${gh_repo}/releases/download/${release_tag}/${install_target}${file_suffix}-UEFI${file_extension}" ;; *) image_url="https://github.com/${gh_repo}/releases/download/${release_tag}/${install_target}${file_suffix}${file_extension}" ;; esac curl -LO $image_url elif [ -n "$install_disk" ]; then # Download generic image colorecho "$GREEN" "Unable to retrieve device model, looking for the latest generic image ..." install_target="generic" image_url="https://github.com/${gh_repo}/releases/download/UEFI-${release_tag}/${install_target}${file_suffix}-UEFI${file_extension}" curl -LO $image_url elif command -v bash >/dev/null; then # Download the Installer to current root colorecho "$GREEN" "Downloading the Installer to current root ..." colorecho "$GREEN" "Tips: To start the installer, run 'installer' (as root)." # Download growpart sudo curl -L https://raw.githubusercontent.com/canonical/cloud-utils/main/bin/growpart -o /usr/bin/growpart sudo chmod +x /usr/bin/growpart # Download the Installer curl https://raw.githubusercontent.com/kwankiu/archlinux-installer/${release_tag}/arch-installer | bash -s -- --reinstall else colorecho "$GREEN" "Error bash is not available on your system." fi if [ -f "/opt/oowow" ]; then # Get downloaded image name image_name="$(basename $image_url)" # Flash with OOWOW colorecho "$GREEN" "Flashing image $image_name using OOWOW" image_write2emmc $image_name elif [ -n "$install_disk" ]; then # Get downloaded image name image_name="$(basename $image_url)" # Flash to disk colorecho "$GREEN" "Extracting image $image_name ..." unxz $image_name colorecho "$GREEN" "Flashing image ${image_name%.xz} to $install_disk" sudo dd if="${image_name%.xz}" of="$install_disk" bs=1M fi