#!/bin/sh
#

# PROVIDE: mountvar
# BEFORE:  var

. /etc/rc.subr

name="mountvar"

#command="/usr/sbin/daemon"
start_cmd="mountvar_start"

filelist="var/empty /var/run /var/db /var/at /var/at/jobs /var/spool/clientmqueue"
corrupt=0

mountvar_start()
{
        # Fix any issues
        fsck -y /dev/mmcsd0s2b
        # Check again to see if good
        fsck -y /dev/mmcsd0s2b
        result=$?
        if [ $result -eq 0 ]; then
            mount /dev/mmcsd0s2b /var
            result=$?
        fi
        if [ $result -ne 0 ]; then
                echo failed
                newfs -U -f 4096 /dev/mmcsd0s2b > /dev/null
                mount /var
                /usr/sbin/mtree -deiU -f /etc/mtree/BSD.var.dist -p /var > /dev/null
        fi
        for f in $filelist ; do
                if [ ! -e $f ] ; then
                        corrupt=1
                fi
        done

        if [ $corrupt -eq 1 ]; then
                /usr/sbin/mtree -deiU -f /etc/mtree/BSD.var.dist -p /var > /dev/null
        fi

}

run_rc_command "$1"
