#!/bin/bash
###############################################################
### voidlinux boostrap script
### Copyright (C) 2021 ALEXPRO100 (alealexpro100)
### License: GPL v3.0
###############################################################

set -e

#Use library
export ALEXPRO100_LIB_LOCATION="${ALEXPRO100_LIB_LOCATION:-./lib/alexpro100_lib.sh}"
# shellcheck disable=SC1091
source ./lib/common/lib_connect.sh

has_root || return_err "This script requries root permissions!"

# Check options.
if [[ -z $4 ]]; then
  echo_help "Void linux boostrap script.\n" \
  "Example: $0 x86_64 musl https://alpha.de.repo.voidlinux.org/current /mnt htop"
fi

arch=$1 version=$2 mirror=$3 dir="$(realpath "$4")"; shift 4; add_packages="$*"

# Offline parameters verify
[[ $arch == "x86_64" || $arch == "aarch64" || $arch == "armv6l" || $arch == "armv7l" || $arch == "i686" ]] || return_err "Incorrect arch $arch! Supported: x86_64,aarch64,armv6l,armv7l"
[[ $arch != "i686" ]] || return_err "Arch $arch is not supported!"
[[ $version == "musl" || $version == "glibc" ]] || return_err "Incorrect version $version! Supported: musl,glibc."
is_url "$mirror" || return_err "Link $mirror does not seem to be URL!"
[[ -d $dir ]] || return_err "Directory $dir does not exist!"

parse_arch "$(uname -m)"
if [[ $arch == "$void_arch" ]]; then
  arch_chroot_command="chroot_rootfs auto"
  xbps_static_run=""
else
  parse_arch "$arch"
  if qemu_chroot check "$qemu_arch" ok; then
    arch_chroot_command="qemu_chroot $qemu_arch"
    xbps_static_run="qemu_run_bin $qemu_arch"
  else
    exit 1
  fi
fi

install_arch=$arch
if [[ "$arch" == "aarch64" ]]; then
  repo_xbps="$mirror/$arch"
else
  repo_xbps="$mirror/musl"
fi
if [[ "$arch" == "aarch64" ]]; then
  mirror="$mirror/$arch"
  [[ "$version" == "musl" ]] && install_arch="$arch-musl"
elif [[ "$version" == "musl" ]]; then
  install_arch="$arch-musl"; mirror="$mirror/musl"
fi

check_url "$mirror/$install_arch-repodata" || return_err "Incorrect arch $arch, version $version or mirror $mirror!"
check_url "$repo_xbps/$arch-musl-repodata" || return_err "Check $repo_xbps/$arch-musl-repodata!"

#Find, download and unpack database, certificate and xbps-static and install system with it.
create_tmp_dir xbps_tmp
msg_print note "(1) Downloading and extracting static xbps..."
get_file_s - "$repo_xbps/$arch-musl-repodata" | arccat zst - | tar xf - -C "$xbps_tmp"
xbps_name=$(< "$xbps_tmp/index.plist" grep xbps-static- | sed -e 's/<string>//;s/<\/string>//;s/\t\t//')
check_url "$repo_xbps/$xbps_name.$arch-musl.xbps" || return_err "URL $repo_xbps/$xbps_name.$arch-musl.xbps is incorrect!"
get_file_s - "$repo_xbps/$xbps_name.$arch-musl.xbps" | arccat zst - | tar xf - -C "$xbps_tmp"

function do_install() {
  msg_print debug "(2.1) Installing..."
  yes | XBPS_TARGET_ARCH="$install_arch" SSL_NO_VERIFY_PEER=1 $xbps_static_run "$xbps_tmp/usr/bin/xbps-install.static" -r "$dir" -R "$mirror" -A -Suy base-voidstrap $add_packages
  rm -rf "$xbps_tmp"
  msg_print debug "(2.2) Configuring..."
  $arch_chroot_command "$dir" /usr/bin/xbps-reconfigure -af
  echo "#repository=$mirror" >> "$dir/etc/xbps.d/install_repo.conf"
}
if [[ $AP100_DBG_ON == 1 ]]; then
  msg_print debug "(2) Installing to chosen directory..."
  do_install
else
  do_install &>/dev/null &
  show_progress train $! "(2) Installing to chosen directory..."
fi

msg_print note "Voidlinux was succesfully bootstrapped to $dir."
