#!/bin/bash

### Use PKG_DIR as working directory.
PKG_DIR=/tmp/_test_spk
rm -rf $PKG_DIR
mkdir -p $PKG_DIR

source /mega/pkgscripts/include/pkg_util.sh

create_inner_tarball() {
    local inner_tarball_dir=/tmp/_inner_tarball

    ### Clear destination directory
    rm -rf $inner_tarball_dir && mkdir -p $inner_tarball_dir

    cp buildDMS/mega-cmd \
       buildDMS/mega-cmd-server \
       buildDMS/mega-exec \
       src/client/mega-* \
       "$inner_tarball_dir"

    ### Create package.txz: $1=source_dir, $2=dest_dir
    pkg_make_package $inner_tarball_dir "${PKG_DIR}"
}

get_platform_family() {
    plat_to_family "$PLATFORM"
}

create_spk() {
    pushd build/SynologyNAS/toolkit/source/MEGAcmd

    ### Copy conf and scripts.
    cp -av conf $PKG_DIR/.
    cp -av scripts $PKG_DIR/.

    ### Copy package icon
    cp -av PACKAGE_ICON.PNG $PKG_DIR
    cp -av PACKAGE_ICON_256.PNG $PKG_DIR

    ### Generate INFO file
    ./INFO.sh > INFO
    cp -av INFO ${PKG_DIR}

    ### Create the final spk
    mkdir -p "/image/${PLATFORM}"
    pkg_make_spk ${PKG_DIR} "/image/${PLATFORM}" $(__get_spk_name get_platform_family INFO)

    popd
}

create_inner_tarball
create_spk

