#!/usr/bin/env zsh
#
# Copyright (c) 2016 Dyne.org Foundation
# ARM SDK is written and maintained by parazyd <parazyd@dyne.org>
#
# This file is part of ARM SDK
#
# This source code is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this source code. If not, see <http://www.gnu.org/licenses/>.

# setup zsh options
setopt pushdsilent

# load zsh modules
zmodload zsh/system
zmodload zsh/regex

# adopts Zsh extension "Zuper"
zkv=1
helpers=1
source arm/zuper

## declare global variables

# config variables
vars+=(os mirror release section version)
# global variables
vars+=(R sdk_loaded)
# script variables
vars+=(workdir strapdir writebase)
vars+=(loopdevice mappdevice)
vars+=(arm_build_device armdev)
# script locals
vars+=(device_name image_name arch size)
vars+=(inittab)

# we assume source sdk is always run from the source dir
# which is the "root" of the program, $R, or env DEVUAN_SDK
R=${ARM_SDK:-`pwd`}

# global maps (TODO in zuper)
maps+=(arm_map)

source config

# load all zlibs
source arm/init-arm

# complete zuper initialization
source arm/zuper.init

# unset DEBUG_BEFORE_CMD

# exit on error of any function
TRAPZERR() { zerr; return $? }

if [[ $sdk_loaded = 1 ]]; then
    act "ARM SDK reloaded"
else
    notice "ARM SDK loaded"
    typeset -h sdk_loaded
    sdk_loaded=1
    export RPROMPT="ARM SDK"
fi

b0rk-clean() {
	# TODO: figure out cleaning until the last step if toasting goes fubar
}
