#!/bin/bash

#####################
# Main
####################
main() {
    # read config
    call_entry_point builder_read_config_pre
    builder_read_config
    call_entry_point builder_read_config_post
    
    # Check if the package is still build
    test ! -f ${OPSI_REPOS_PRODUCT_DIR}/${OPSI_REPOS_FILE_PATTERN} 
    builder_check_error "package ${OPSI_REPOS_PRODUCT_DIR}/${OPSI_REPOS_FILE_PATTERN} already generated"
    
    # download and process dist files
    call_entry_point  builder_download_dist_files_pre
    builder_download_dist_files
    call_entry_point  builder_download_dist_files_post
    
    # Start building
    call_entry_point builder_package_pre
    builder_package
    call_entry_point builder_package_post
    
    # Upload to repos
    call_entry_point builder_upload_pre
    builder_upload
    call_entry_point builder_upload_post

    # git commit
    call_entry_point builder_commit_pre
    builder_commit
    call_entry_point builder_commit_post
}


# read private build configuration
. $BUILD_LOCAL_CFG
. $BUILDER_DIR/builder-lib.sh

# get target directory
PRODUCT_DIR=$1

if [ -f "$PRODUCT_DIR/build-extension.sh" ] ; 
    . "$PRODUCT_DIR/build-extension.sh"
fi


builder_main

# Exit
exit 0