#! /usr/bin/perl
# mountpix: mount either or both of the memory stick or Zio SM adaptor.
# Hotplug can't (yet?) distinguish multiple usb-storage devices,
# or ensure that any particular ubs-storage device always gets
# mapped to the same scsi device, though there's a rumour that
# the 2.5 kernel may handle this better.
#
# Requires /proc support to be on in the kernel.
#
#    Copyright 2001 by Akkana Peck
#    You may use and distribute this under the GPL.

# Usage: mountpix [type]
# where type is in {stick, zio}
# No arguments will try to mount all known dirs.

my ($type) = shift;

try_mount_scsi($type, 0);
try_mount_scsi($type, 1);

#
# try_mount_scsi(N) mounts the Nth scsi device on the proper mount point;
# if first argument is a non-null string, it will only mount that type.
#
sub try_mount_scsi
{
  my ($type) = shift;
  my ($id) = shift;

  my ($procfile) = "/proc/scsi/usb-storage-" . $id . "/" . $id;
  my ($scsidev) = "/dev/sd" . chr(ord("a") + $id) . "1";
  my ($mntpt) = "/pix";
  my ($mntopt) = "";

  print "try_mount_scsi: $type\n";

  # print "dev is $scsidev, looking in $procfile\n";
  $product = `grep Product $procfile`;
  # print "Product is $product\n";
  if ($product =~ /Memory Stick/) {
    if ($type ne "" && $type ne "stick") {
      return;
    }
    $mntpt = "/stick";
    $mntopt = "-t vfat";
  }
  elsif ($product =~ /SmartMedia/) {
    if ($type ne "" && $type ne "zio") {
      return;
    }
    $mntpt = "/zio";
    $mntopt = "-r -t vfat";
  }

  # XXX figure out here whether a device is really inserted

  $cmd = "mount " . $mntopt . " " . $scsidev . " " . $mntpt;
  print "Trying: $cmd\n";
  system($cmd);
}

