#!/usr/local/bin/perl

use Getopt::Std;
use Term::ReadKey;
use NaServer;

getopts ('hl:');
$HOST_EQUIV = 0;
$HOST_EQUIV = 1 if ($opt_h);
if ($#ARGV != 2)
{
  print STDERR "Usage: ntap_symlink [-h] [-l user] controller path link_name\n";
  exit (1);
}
$FILER = $ARGV[0];
$PATH = $ARGV[1];
if ($PATH !~ /^\/vol\//)
{
  print STDERR "Path must being with /vol/\n";
  exit (2);
}
$LINK = $ARGV[2];
$USER = "root";
$USER = $opt_l if ($opt_l);
$s = NaServer->new ($FILER, 1, 1);
if (!$HOST_EQUIV)
{
  print "\nEnter the password for user $USER: ";
  ReadMode noecho;
  $PW = <STDIN>;
  chomp ($PW);
  ReadMode normal;
  print "\n";
  $s->set_style (LOGIN_PASSWORD);
  $s->set_admin_user ($USER, $PW);
}
else
{
  $s->set_style (HOSTS_EQUIV);
}
$out = $s->invoke ("file-create-symlink", "path", $LINK, "symlink", $PATH);
if ($out->results_errno != 0)
{
  my $reason = $out->results_reason();
  print STDERR "ERROR: $reason\n";
  exit (3);
}
exit (0);
