#!/usr/athena/bin/perl -w

# Setup (if necessary) and run Linux 'nap' Napster client
# KJW 8/29/2000 Revision 0.1

use strict;

chdir $ENV{'HOME'};

sub quiz ($) {
    my $question = shift;
    my $answer;

    print $question, " ";
    chomp( $answer = <STDIN> );

    return $answer;
}

my ( $account_already_exists, $nap_already_setup ) = ( 1, 0 );

unless ( -e "$ENV{HOME}/.nap.conf" ) {
    # If user has no nap.conf file, make one.

    $nap_already_setup = 0;

    my ( $username, $password,
	 $upload, $download, $email );
    
    print "You do not appear to have a .nap.conf file in your home directory.\n";
    print "I will set one up for you now.\n\n";

    $account_already_exists = 0 unless
	( (quiz "Do you already have a Napster account [y/n]?") =~ /^y/i );

    if ( $account_already_exists ) {
	$username = quiz "What is your Napster username?";
	$password = quiz "What is your Napster password?";
    } else {
	$username = quiz "What is your preferred Napster username?";
	$password = quiz "What is your preferred Napster password?";
    }

    $upload = quiz "What is your upload directory?";
    $download = quiz "What is your download directory?";
    $email = quiz "What email address do you wish to advertise to Napster?";

    print "Writing configuration file to your ~/.nap.conf... ";
    
    open NAP_CONF, ">$ENV{HOME}/.nap.conf"
	or die "Could not open ~/.nap.conf for writing: $!\n";
    
    print NAP_CONF "user=$username\npass=$password\nupload=$upload\n";
    print NAP_CONF "download=$download\ndataport=6699\nconnection=10\n";
    print NAP_CONF "email=$email\n";

    close NAP_CONF or die "Could not close ~/.nap.conf: $!\n";

    print "done\n";
} else {
    $nap_already_setup = 1;
}

chomp( my $binary_dir = `athdir /afs/sipb/project/napster` );

my $real_nap = $binary_dir . '/nap-real';

my @arguments;

unless ( $nap_already_setup ) {
    push @arguments, '-b';
}

unless ( $account_already_exists ) {
    push @arguments, '-m';
}

exec $real_nap, $real_nap, @arguments;

