#!/bin/bash

set -e

function usage {

    cat <<EOF

Create Debian userland filesystem for MNT Reform
---
This script create an userland system for the MNT Reform 2 laptop

USAGE:
make_userland.sh -o <output> -r <recipe> -m <mirror>

  -o <output> directory tha contain our userlan system. a subdirectory with 
   the recipe name will be created. Default: \tmp

  -r <recipe> : recipe to apply, the name must be a subdorectory of the recipe\
   directory. Default: default

  -m <mirror> : debian mirror where download packages. Default: ftp.debian.org
EOF
}

function error {
    >&2 printf "\e[31mE\e[0m %s\n" "$1"
}

function clean {
    rm -rf "$output"
}

function process_args {

    while :; do
        case $1 in
            -h|-\?|--help)
                usage
                exit 0
                ;;
            -r|--recipe)
                recipe_name="$2"
                shift
                ;;
            -m|--mirror)
                local http_response
                http_response=$(curl -o /dev/null -s -L -I -w "%{http_code}\n" "$2")
                if [ ! "$http_response" -eq 200 ]
                then
                    error "Mirror $1 seems to have problem."
                    exit 5
                fi
                debian_mirror="$2"
                shift
                ;;
            -o|--output)
                if [ ! -d "$2" ]
                then
                    error "output directory $2 not exist."
                    exit 5
                fi
                output_dir=$2
                ;;
            *)
            break
        esac
        shift
    done
}

function process_packagefile () {
    local packagesfile
    local packageslist
    packagesfile="$1"
    packageslist="$2"

    if [ ! -f "$packagesfile" ]
    then
        error "Package file $packagesfile not found"
        return
    fi
    while read -r pkg
    do
        if [[ -z $packageslist ]]
        then
            packageslist="$pkg"
        else
            packageslist="${packageslist},${pkg}"
        fi
    done < "$packagesfile"
    printf "%s" "$packageslist"
}

for c in curl mmdebstrap
do
    if ! command -v "$c" >/dev/null
    then
        error "This script need $c to work properly"
        exit 6
    fi
done

# default values
debian_mirror="http://ftp.debian.org/debian"
output_dir="/tmp"
recipe_name="default"

process_args "$@"
export RECIPE="recipes/$recipe_name"

printf "Make userland script, dir:%s recipe:%s" "$(pwd)" "$recipe"

if [ ! -d "$RECIPE" ]
then
    error "recipe folder $RECIPE not found"
    usage
    exit 2
fi

output="$output_dir/$recipe_name"
if [ -f "$output" ]
then
    error "$output directory exist, remove it and launch $0 again"
    exit 20
fi
trap clean ERR SIGINT

include=""
if [ -d "$RECIPE/packages" ]
then

    for f in "$RECIPE"/packages/*.list
    do
        printf "Add package list for mmdebstrap: %s\n" "$f"
        include="$( process_packagefile "$f" "$include")"
    done
fi

if [ -d "$RECIPE"/hook ]
then
    hook="--hook-directory=$(pwd)/${RECIPE}/hook"
fi

command="mmdebstrap --architectures=arm64 --components=main --variant=minbase"

[[ -n "$include" ]] && command="$command --include=$include"
[[ -n "$hook" ]] && command="$command $hook"
command="$command sid $output $debian_mirror" 

$command || exit 20

exit
#mkdir "$output/root/recipe" || exit 21 
#
#cp init_target.sh "$output/root/" || exit 40
#cp -r "$recipe"/* "$output/root/recipe" || exit 41
#
#chroot "$output" /root/init_target.sh
#
#exit 0
