#!/bin/bash
###############################################################
### alpine 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 "Alpine linux boostrap script.\n" \
  "Supports CUSTOM_ALPINE_REPO flag.\n" \
  "Example: $0 x86_64 v3.13 https://nl.alpinelinux.org/alpine /mnt htop"
fi

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

# Offline parameters verify
[[ $arch == "x86_64" || $arch == "aarch64" || $arch == "armhf" || $arch == "armv7" || $arch == "x86" ]] || return_err "Incorrect arch $arch! Supported: x86_64,x86,aarch64,armhf,armv7"
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 [[ $mirror =~ https* ]]; then
  msg_print warning "Detected https mirror! Will try to use http for initial install."
  # Due to new mechanism of download, we need to either ignore https or use temporary rootfs.
  check_url "${mirror/https\:/http\:}" || return_err "Mirror $mirror does NOT support http!"
fi
msg_print note "CUSTOM_ALPINE_REPO=[$CUSTOM_ALPINE_REPO]"
msg_print note "mirror=[$mirror]version=[$version]arch=[$arch]"
if [[ $CUSTOM_ALPINE_REPO == "1" ]]; then
  check_url "$mirror" || return_err "Incorrect mirror $mirror!"
  download_repo="$mirror/$arch"
  arg_repos="-X ${mirror/https\:/http\:}"
else
  check_url "$mirror/$version/main/$arch" || return_err "Incorrect arch $arch, version $version or mirror $mirror!"
  download_repo="$mirror/$version/main/$arch"
  arg_repos="-X ${mirror/https\:/http\:}/$version/main -X ${mirror/https\:/http\:}/$version/community"
fi
if [[ $arch == "$alpine_arch" ]]; then
  apk_static_run=""
else
  parse_arch "$arch"
  if qemu_chroot check "$qemu_arch" ok; then
    apk_static_run="qemu_run_bin $qemu_arch"
  else
    exit 1
  fi
fi

#Find, download and unpack apktools-static and install system with it.
create_tmp_dir alpine_apk
msg_print note "(1) Getting link for apktools-static $alpine_arch..."
# TODO: Replace html parsing with database parse.
msg_print note "get_file_s - $download_repo/ | get_file_list_html | grep apk-tools-static"
apktools_pkg=$(get_file_s - "$download_repo/" | get_file_list_html | grep apk-tools-static)
get_file_s - "$download_repo/$apktools_pkg" | tar -xzf - -C "$alpine_apk" "sbin/apk.static" &>/dev/null &
show_progress train $! "(2) Downloading and extracting apk-tools static..."

#export AP100_DBG_ON=1
function do_install() {
  msg_print note "$apk_static_run $alpine_apk/sbin/apk.static $arg_repos --allow-untrusted --root $dir --arch $arch --initdb add alpine-base $add_packages"
  $apk_static_run "$alpine_apk/sbin/apk.static" $arg_repos --allow-untrusted --root "$dir" --arch "$arch" --initdb add alpine-base $add_packages
  rm -rf "$alpine_apk"

  if [[ $CUSTOM_ALPINE_REPO != "1" ]]; then
    echo -e "#$mirror/$version/main\n#$mirror/$version/community" >> "$dir/etc/apk/repositories"
    [[ $version != "edge" ]] || echo -e "#$mirror/$version/testing" >> "$dir/etc/apk/repositories"
  fi
}
msg_print note "AP100_DBG_ON=[$AP100_DBG_ON]"
if [[ $AP100_DBG_ON == 1 ]]; then
  msg_print debug "(3) Installing to chosen directory..."
  do_install
else
  do_install &>/dev/null &
  show_progress train $! "(3) Installing to chosen directory..."
fi

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