# !/bin/bash -

source ./log_helpers

if [ ${news_to_ismas_sourced:-1} = "1" ]; then    # include only once
    readonly APISM_DB_PORT=7777
    news_to_ismas () {
        local data="#M=APISM#C=CMD_EVENT#J=
        {
            \"REASON\":\"SW_UP\",
            \"Timestamp\":\"$(date +%Y-%m-%dT%T.000%z)\",
            \"EVENT\":\"$1\",
            \"EVENTSTATE\":1,
            \"PARAMETER\":\"Testdaten\"
        }"
        echo $((echo "$data"; sleep 1) | nc localhost $APISM_DB_PORT)
    }
   
    # WAIT
    update_psa_activated () {
        news_to_ismas "U0010" 
        return $?
    }
   
    # git pull 
    update_psa_pull_customer_repository () {
        # text im PARAMETER?
        news_to_ismas "U0011" "git pull customer repository" 
        return $?
    }

    update_psa_backup () {
        news_to_ismas "U0011" "backup before update steps"
        return $?
    }

    update_psa_check_hash_values () {
        news_to_ismas "U0011" "checking file integrity"
        return $?
    }
    
    update_psa_check_hardware_compatibility () {
        news_to_ismas "U0011" "check hardware compatiblity" 
        return $?
    }
   
    # conf/ini files 
    update_psa_copy_files () {
        news_to_ismas "U0011"  "copy conf/ini-files" 
        return $?
    }
    
    # exec opkg-commands (dry-run)
    update_psa_install_packages_dry_run () {
        news_to_ismas "U0011"   "install opkg-packages (dry-run)" 
        return $?
    }
    
    # exec opkg-commands
    update_psa_install_packages () {
        news_to_ismas "U0011"   "install opkg-packages" 
        return $?
    }
    
    # remove backup in case of success
    update_psa_cleanup () {
        news_to_ismas "U0011" "cleanup" 
        return $?
    }
    
    update_psa_finished () {
        news_to_ismas "U0012"
        return $?
    }

    # results    
    update_psa_succeeded () {
        news_to_ismas "U0001" "updates installed"
        return $?
    }
    
    reset_update_trigger () {
        news_to_ismas "U0002" "updates activated" 
        return $?
    }

    update_psa_canceled_no_updates_available () {
        # text im PARAMETER?
        news_to_ismas "U0003" # "No updates available, but 'WAIT' active. Missing check-in?"
        return $?
    }

    update_psa_backup_failed () {
        news_to_ismas "U0003" # "Backup failed"
        return $?
    }
    
    update_psa_wrong_hash_values () {
        news_to_ismas "U0003" # "Wrong hash value for file <xxx>. Missing check-in?" 
        return $?
    }
    
    update_psa_hardware_not_compatible () {
        news_to_ismas "U0003" "conf/ini-file <xxx> and psa hardware <yyy> not compatible" 
        return $?
    }
    
    update_psa_opkg_command_failure () {
        news_to_ismas "U0003" "opkg-command <xxx> failed" 
        return $?
    }

    update_psa_failed () {
        news_to_ismas "U0003" "Unknown error"
        return $?
    }

    # only for testing 
    set_updates_available () {
        news_to_ismas "U0099"
        return $?
    }
    
    set_update_active () {
        reset_update_trigger
        return $?
    }
fi