#!/bin/sh /etc/rc.common

START=81
STOP=01
USE_PROCD=1

start_service() {
    local model=$(envctl factory get model)

    if [ ! -d /rae ]; then
        echo "Directory /rae dose not exist, do not launch rae."
        return 1
    fi
    local factory_mode=`envctl factory get factory_mode`
    if [ "$factory_mode" != "off" ]; then
        echo "rae factory_mode [$factory_mode]"
        return 3
    fi

    mkdir -p /rae/config
    mkdir -p /rae/bin
    mkdir -p /var/rae
    mkdir -p /var/rae/metadata
    mkdir -p /etc/config/data/rae

    if [ ! -f /rae/bin/rae ]; then
        echo "copy file /usr/sbin/rae to /rae/bin/rae"
        cp /usr/sbin/rae /rae/bin/rae
    fi

    if [ ! -f /rae/config/RAE_Policy.json ]; then
        echo "copy file /etc/rae/RAE_Policy.json to /rae/config/RAE_Policy.json"
        cp /etc/rae/RAE_Policy.json /rae/config/RAE_Policy.json
    fi

    procd_open_instance
    procd_set_param command /rae/bin/rae
    procd_set_param respawn ${respawn_threshold:-100}
    procd_close_instance

    echo "Start rae."
}

stop_service() {
    echo "Stop rae."
}
