#!/usr/bin/perl

# /* LICENSE:
#   =========================================================================
#     CMPack'04 Source Code Release for OPEN-R SDK 1.1.5-r2 for ERS7
#     Copyright (C) 2004 Multirobot Lab [Project Head: Manuela Veloso]
#     School of Computer Science, Carnegie Mellon University
#     All rights reserved.
#   ========================================================================= */

use Getopt::Long;

$stop = 1;

if(-e "/sbin/cardctl") {
  $pc_card = 1;
} else {
  $pc_card = 0;
}

open(CARDCTL_OUT,"/sbin/cardctl status |");
while(<CARDCTL_OUT>) {
    /suspended/ && do {
	printf("card suspended, please reinsert\n");
	exit(8);
    };
}

printf("mounting stick\n");
$mounted=0;
open(MOUNT_OUT,"/bin/mount |");
while(<MOUNT_OUT>) {
    m&/memstick& && do {
	$mounted=1;
    };
}

if(!$mounted) {
    system("mount /memstick");
}

$dogroot = $ENV{"DOGROOT"};
system("cp $dogroot/agent/config/config/spout.cfg /memstick/config/");

if($stop) {
    printf("unmounting stick\n");
    system("/bin/umount /memstick");
    
    if($pc_card) {
	printf("stopping card\n");
	system("echo 'eject' >/var/pipes/stop_memstick");
    }
}
