# old version (without -$pkgrel): ${1%%-*}
# new version (without -$pkgrel): ${2%%-*}

PKGNAME=ubm-100-ublinux
PATH_UPGRADE=/mnt/upgrade
DATA_FILE_CONF="ublinux.ini"

# arg 1:  the new package version
ubm_pre_load(){
    true
}

# arg 1:  the new package version
ubm_post_load(){
    # Выполнять, если $1 > 2405-55
    if ! printf '%s\n' "$1" "2405-55" | sort --version-sort --check 2>/dev/null; then
        [[ -w $(find /memory/layer-base/ -maxdepth 2 -path "*" -name "${DATA_FILE_CONF}" | tail -1) ]] && /usr/bin/ubconfig update || true
        [[ -w $(find /memory/data/from/0 -maxdepth 1 -type d -name "boot" | tail -1) ]] && /usr/lib/ubboot/ubboot-skel update -udgt || true
    fi
}

# arg 1:  the old package version
ubm_pre_unload(){
    true
}

# arg 1:  the old package version
ubm_post_unload(){
    true
}

ubm_load() {
    true
}

ubm_unload() {
    true
}

# arg 1:  the new package version
pre_install() {
    pre_upgrade $@
}

# arg 1:  the new package version
post_install() {
    post_upgrade
}

# arg 1:  the new package version
# arg 2:  the old package version
pre_upgrade() {
    msg_error() {
	echo "  -> System upgrade disabled or write path unavailable ${PATH_UPGRADE} !"
	return 1
    }
    touch ${PATH_UPGRADE}/upgrade.conf &>/dev/null || msg_error
}

# arg 1:  the new package version
# arg 2:  the old package version
post_upgrade() {
    # Выполнять, если $1 <= 2405-55
    if printf '%s\n' "$1" "2405-55" | sort --version-sort --check 2>/dev/null; then
        [[ -w $(find /memory/layer-base/ -maxdepth 2 -path "*" -name "${DATA_FILE_CONF}" | tail -1) ]] && /usr/bin/ubconfig update || true
        [[ -w $(find /memory/data/from/0 -maxdepth 1 -type d -name "boot" | tail -1) ]] && /usr/lib/ubboot/ubboot-skel update -udgt --pkg-oldver "2.17" || true
    fi
}

# arg 1:  the old package version
pre_remove() {
    true
}

# arg 1:  the old package version
post_remove() {
    true
}
