#!/usr/bin/perl -w
#Change the location of perl (if needed)

$PN = "PPPKit";
$PV = "v0.7";
$PA = "atomik";

print "$PN $PV by $PA is now loading...\n";

if ( $^O ne "linux") {
   print "\n\a";
   print "ERROR: This program requires the linux operating system!\n";
   print "\n";
   exit
}

if ( $> || $< ) {
   print "\a\n";
   print "ERROR: This program must be ran as root!\n";
   print "\n";
   exit   
}
  
if (-f "/var/log/.pppkit.installed") { 

   print "\n\a";
   print "ERROR: $PN is already on this system!\n";
   print "\n";
   print "Do you wish to Uninstall? (yes/no) [yes] ";
   $UNINS = <STDIN>;
   chop($UNINS);

   if ($UNINS  eq "") {
       $UNINS = "yes";
   } 
   
   if ($UNINS  eq "yes") {
       print "\n";	
       system "sh Uninstall";
       print "\n";
   }
   
   if ($UNINS eq "no") {
       print "Installation aborted.\n";
       exit     
   } 
}

print "Please read the README file for further questions...\n";
sleep(2);

system "clear";

DIALUP: {

print "What is your dialup number? ";
$PHONE = <STDIN>;
chop($PHONE);

        }

if ($PHONE  eq "") {
   print "\a\n";
   print "ERROR: You must specify a dialup number!\n";
   print "\n";
   goto DIALUP;

}

LOGIN:  {

print "What is the login for your isp? ";
$LOGIN = <STDIN>;
chop($LOGIN);

        }

if ($LOGIN  eq "") {
   print "\a\n";
   print "ERROR: You must specify a login!\n";
   print "\n";
   goto LOGIN;

}

PASS: {
system "stty -echo";
print "What is the password for your isp? ";
$PASS = <STDIN>;
chop($PASS);
print "\n";
system "stty echo";
      }

if ($PASS  eq "") {
   print "\a\n";
   print "ERROR: You must specify a password!\n";
   print "\n";
   goto PASS;

}

print "What is your connection speed? [115200] ";
$SPEED = <STDIN>;
chop($SPEED);

DOMAIN: {

print "What is your ISP's domain? ";
$DOMAIN = <STDIN>;
chop($DOMAIN);

        }

if ($DOMAIN  eq "") {
   print "\a\n";
   print "ERROR: You must specify your domain name!\n";
   print "\n";
   goto DOMAIN;

}

system "clear";
print "Specify a device:\n";
print "\n";
print "COM 1 in DOS = cua0\n";
print "COM 2 in DOS = cua1\n";
print "COM 3 in DOS = cua2\n";
print "COM 4 in DOS = cua3\n";
print "\n";
print "Example: type `cua1`if your modem is on COM 2 in DOS.\n";
print "\n";
print "What device is your modem on? [cua1] ";
$DEV = <STDIN>;
chop($DEV);

system "clear";
print "Enter the Local IP for your ISP.\n";
print "\n";
print "0.0.0.0 is dynamic.\n";
print "\n";
print "What is your Local IP? [0.0.0.0] ";
$LOCIP = <STDIN>;
chop($LOCIP);

system "clear";
print "Enter the Remote IP for your ISP.\n";
print "\n";
print "0.0.0.0 is dynamic.\n";
print "\n";
print "What is your Remote IP? [0.0.0.0] ";
$REMOTEIP = <STDIN>;
chop($REMOTEIP);

system "clear";
print "Enter your Netmask.\n";
print "\n";
print "Probably 255.255.255.0 unless you know otherwise.\n";
print "\n";
print "What is your Netmask? [255.255.255.0] ";
$NETMASK = <STDIN>;
chop($NETMASK);

system "clear";
print "Will you be accessing a nameserver? [yes/no] [yes] ";
$NS = <STDIN>;
chop($NS);

if ($NS  eq "") {
   $NS = "yes";
}

if ($NS  eq "yes") {
   
   NS1: {	
   
   print "What is your primary nameserver? ";
   $NS1 = <STDIN>;
   chop($NS1);   
   
        }
 
   if ($NS1  eq "") {
      print "\a\n";
      print "ERROR: You must specify a primary nameserver!\n";
      print "\n";
      goto NS1;

   }
}

if ($NS  eq "yes") {
   
   print "Will you be accessing a secondary nameserver? [yes/no] [yes] ";
   $NS3 = <STDIN>;  
   chop($NS3);

   if ($NS3 eq "") {
      $NS3 = "yes";
   }

   if ($NS3  eq "yes") {
   
      NS2: {

      print "What is your secondary nameserver? ";
      $NS2 = <STDIN>;
      chop($NS2);

           }
 
      if ($NS2  eq "") {
         print "\a\n";
         print "ERROR: You must specify a secondary nameserver!\n";
         print "\n";
         goto NS2;

      }
   }   
}

system "clear";
print "$PN now has enough information to create the scripts.\n";
print "Do you wish to install? [yes/no] [yes] ";
$INSTALL = <STDIN>;
chop($INSTALL);

if ($INSTALL  eq "") {
   $INSTALL = "yes";

}

if ($INSTALL  eq "yes") {
    print "Now Installing...\n";

    open (FH,">ppp-on");
    print FH "PHONE=$PHONE\n";
    close (FH);

    open (FH,">>ppp-on");
    print FH "LOGIN=$LOGIN\n";
    close (FH); 

    open (FH,">>ppp-on");
    print FH "PASS=$PASS\n";
    close (FH);

if ($DEV  eq "") {
   $DEV = "cua1";
}

    open (FH,">>ppp-on");
    print FH "DEVICE=$DEV\n";
    close (FH);

if ($SPEED  eq "") {
    open (FH,">>ppp-on");
    print FH "SPEED=115200\n";
    close (FH);

} else {

    open (FH,">>ppp-on");
    print FH "SPEED=$SPEED\n";
    close (FH);

}

    system "touch /etc/resolv.conf";
    rename('/etc/resolv.conf', '/etc/resolv.OLD');

    open (FH,">>/etc/resolv.conf");
    print FH "domain $DOMAIN\n";
    close (FH);

if ($NS  eq "yes") {
   open (FH,">>/etc/resolv.conf");
   print FH "nameserver $NS1\n";
   close (FH);
   
   if ($NS3  eq "yes") {
      open (FH,">>/etc/resolv.conf");
      print FH "nameserver $NS2\n";
      close (FH);

   }
}

if ($LOCIP  eq "") {
    open (FH,">>ppp-on");
    print FH "LOCIP=0.0.0.0\n";
    close (FH);

} else {

    open (FH,">>ppp-on");
    print FH "LOCIP=$LOCIP\n";
    close (FH);

}

if ($REMOTEIP  eq "") {
    open (FH,">>ppp-on");
    print FH "REMOTEIP=0.0.0.0\n";
    close (FH);

} else {

    open (FH,">>ppp-on");
    print FH "REMOTEIP=$REMOTEIP\n";
    close (FH);

}

if ($NETMASK  eq "") {
    open (FH,">>ppp-on");
    print FH "NETMASK=255.255.255.0\n";
    close (FH);

} else {

    open (FH,">>ppp-on");
    print FH "NETMASK=$NETMASK\n";
    close (FH);

}

    
    system "rm -f /dev/modem";
    system "ln -s /dev/$DEV /dev/modem";
    system "cp -f misc/ppp-off /usr/sbin";
    system "cp -f misc/ppp-info /usr/sbin";
    system "cp -f misc/pppdialer /usr/sbin";
    system "cat misc/pppon.1 >> ppp-on";
    system "touch /etc/ppp/ip-up";
    system "mv /etc/ppp/ip-up /etc/ppp/ip-up.OLD";
    system "touch /etc/ppp/ip-down";
    system "mv /etc/ppp/ip-down /etc/ppp/ip-down.OLD";
    system "cp -f misc/ip-up /etc/ppp/";
    system "cp -f misc/ip-down /etc/ppp/";
    system "touch /usr/sbin/ppp-on";
    system "mv /usr/sbin/ppp-on /usr/sbin/ppp-on.OLD";
    system "mv ppp-on /usr/sbin";
    chmod(0755, '/usr/sbin/ppp-on');
    chmod(0700, '/usr/sbin/ppp-off', '/usr/sbin/ppp-info'); 
    chmod(0700, '/usr/sbin/ppp-on', '/usr/sbin/pppdialer');
    system "touch /etc/ppp/options";
    system "mv /etc/ppp/options /etc/ppp/options.OLD";
    system "cp -f misc/options /etc/ppp";
    system "touch /var/log/.pppkit.installed";

    print "\n";
    print "You're done!\n";
    print "type `ppp-on` to connect.\n";
    print "type `ppp-off` to disconnect.\n";
    print "type `ppp-info` for info about your connection.\n";
    print "\n";
    exit
    
} else {
    
    print "Installation aborted.\n";
    exit

}
