#!/usr/bin/perl

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

# print $#ARGV; print @ARGV;

$slot = $ARGV[0];
$root = "/var/run/ejectd";
$file = "${root}/${slot}";

print "starting eject daemon for slot ${slot}\n";

unlink("${file}");
system("mkdir -p ${root}");
system("test -p ${file} || mknod ${file} p");
system("chmod a+rw ${file}");

while(1) {
  open(STOPMEMSTICK,"<${file}");
  while(<STOPMEMSTICK>) { }
  close(STOPMEMSTICK);

  system("/sbin/cardctl eject ${slot}");
  sleep(1);
}
