#!/bin/sh

po_domain="alterator-tc-reset"
alterator_api_version=1

. alterator-sh-functions
. shell-config

LOG="/tmp/alterator-tc-reset.log"

readonly RW="/.rw"
readonly PERSISTENT="persistent"

on_message() {
    case "$in_action" in
        read)
            ;;
        write)
            case "$in__objects" in
                /)
                    echo "$(date): system going to reset factory" >> "$LOG"
                    if [ "$in_reset" = "yes" ]; then
                        find "$RW" -maxdepth 1 -mindepth 1 \
                            -path "$RW/$PERSISTENT" \
                            -o -path "$RW/.wh..wh.aufs" \
                            -o -path "$RW/.wh..wh.plnk" \
                            -o -path "$RW/.wh..wh.orph" \
                            -o -path "$RW/.ro" \
                            -o -path "$RW/.rw" \
                            -o -path "$RW/lost+found" \
                            -prune \
                            -o -print0 | xargs -0 rm -rf
                        reboot
                    fi
                    ;;
            esac
            ;;
        list)
            ;;
    esac
}

message_loop
