#!/bin/bash

#echo ./configure --prefix=$(pwd)/../../rootfs/usr > ECO_INSTALL
#chmod 755 ECO_INSTALL
cat >ECO_INSTALL <<EOF
#!/bin/sh

export INSTROOT=\$(pwd)/../../rootfs
EOF
chmod 755 ECO_INSTALL

./configure --prefix=/usr

find . -name Makefile -exec \
	sed -i	-e'1a\CROSS_COMPILE := arm-linux-' \
		-e"1a\CFLAGS := -I$(pwd)/../../rootfs/usr/include" \
		-e"1a\LDFLAGS := -L$(pwd)/../../rootfs/usr/lib" \
		-e'1a\CC := $(CROSS_COMPILE)gcc $(CFLAGS) $(LDFLAGS)' \
	{} \;

sed -i -e's|DESTDIR = /usr|DESTDIR = $(INSTROOT)/usr|' pppd/plugins/pppol2tp/Makefile

### Apply some patches

## DNS patch:
## only accept DNS addresses, when we get a valid IP address
## This patch prevents DNS addresses 10.11.12.13 and 10.11.12.14
cat >dns.patch <<EOF
diff -ruN a/pppd/ipcp.c b/pppd/ipcp.c
--- a/pppd/ipcp.c	2009-09-18 12:45:11.000000000 +0200
+++ b/pppd/ipcp.c	2009-09-18 12:45:58.000000000 +0200
@@ -1105,19 +1105,11 @@
 	      );
 
     NAKCIDNS(CI_MS_DNS1, req_dns1,
-	     if (treat_as_reject) {
-		 try.req_dns1 = 0;
-	     } else {
-		 try.dnsaddr[0] = cidnsaddr;
-	     }
+	     try.dnsaddr[0] = cidnsaddr;
 	     );
 
     NAKCIDNS(CI_MS_DNS2, req_dns2,
-	     if (treat_as_reject) {
-		 try.req_dns2 = 0;
-	     } else {
-		 try.dnsaddr[1] = cidnsaddr;
-	     }
+	     try.dnsaddr[1] = cidnsaddr;
 	     );
 
     /*
EOF

patch -p1 < dns.patch
rm dns.patch

## This patch reverses revision 1.114 of the pppd/sys-linux.c file.
## The default gateway is needed by the openswan's %defaultroute.
cat >defaultroute.patch <<EOF
diff -Nru ppp-2.4.4.orig/pppd/sys-linux.c ppp-2.4.4/pppd/sys-linux.c
--- ppp-2.4.4.orig/pppd/sys-linux.c	2006-10-05 09:58:39.000000000 +0300
+++ ppp-2.4.4/pppd/sys-linux.c	2006-10-05 10:06:37.000000000 +0300
@@ -209,7 +209,7 @@
 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
 
 static int	if_is_up;	/* Interface has been marked up */
-static int	have_default_route;	/* Gateway for default route added */
+static u_int32_t default_route_gateway;	/* Gateway for default route added */
 static u_int32_t proxy_arp_addr;	/* Addr for proxy arp entry added */
 static char proxy_arp_dev[16];		/* Device for proxy arp entry */
 static u_int32_t our_old_addr;		/* for detecting address changes */
@@ -346,8 +346,8 @@
 /*
  * Delete any routes through the device.
  */
-    if (have_default_route)
-	cifdefaultroute(0, 0, 0);
+    if (default_route_gateway != 0)
+	cifdefaultroute(0, 0, default_route_gateway);
 
     if (has_proxy_arp)
 	cifproxyarp(0, proxy_arp_addr);
@@ -1615,17 +1615,17 @@
     struct rtentry rt;
 
     if (defaultroute_exists(&rt) && strcmp(rt.rt_dev, ifname) != 0) {
-	if (rt.rt_flags & RTF_GATEWAY)
-	    error("not replacing existing default route via %I",
-		  SIN_ADDR(rt.rt_gateway));
-	else
-	    error("not replacing existing default route through %s",
-		  rt.rt_dev);
+	u_int32_t old_gateway = SIN_ADDR(rt.rt_gateway);
+
+	if (old_gateway != gateway)
+	    error("not replacing existing default route to %s [%I]",
+		  rt.rt_dev, old_gateway);
 	return 0;
     }
 
-    memset (&rt, 0, sizeof (rt));
-    SET_SA_FAMILY (rt.rt_dst, AF_INET);
+    memset (&rt, '\0', sizeof (rt));
+    SET_SA_FAMILY (rt.rt_dst,     AF_INET);
+    SET_SA_FAMILY (rt.rt_gateway, AF_INET);
 
     rt.rt_dev = ifname;
 
@@ -1634,14 +1634,16 @@
 	SIN_ADDR(rt.rt_genmask) = 0L;
     }
 
-    rt.rt_flags = RTF_UP;
+    SIN_ADDR(rt.rt_gateway) = gateway;
+
+    rt.rt_flags = RTF_UP | RTF_GATEWAY;
     if (ioctl(sock_fd, SIOCADDRT, &rt) < 0) {
 	if ( ! ok_error ( errno ))
 	    error("default route ioctl(SIOCADDRT): %m");
 	return 0;
     }
 
-    have_default_route = 1;
+    default_route_gateway = gateway;
     return 1;
 }
 
@@ -1654,7 +1656,7 @@
 {
     struct rtentry rt;
 
-    have_default_route = 0;
+    default_route_gateway = 0;
 
     memset (&rt, '\0', sizeof (rt));
     SET_SA_FAMILY (rt.rt_dst,     AF_INET);
@@ -1665,7 +1667,9 @@
 	SIN_ADDR(rt.rt_genmask) = 0L;
     }
 
-    rt.rt_flags = RTF_UP;
+    SIN_ADDR(rt.rt_gateway) = gateway;
+
+    rt.rt_flags = RTF_UP | RTF_GATEWAY;
     if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) {
 	if (still_ppp()) {
 	    if ( ! ok_error ( errno ))
EOF

patch -p1 < defaultroute.patch
rm -f defaultroute.patch

## Default metric patch
## Allow new parameter defaultmetric which sets the metric of the default route
cat >defaultmetric.patch <<EOF
diff -Nru ppp-2.4.4.orig/pppd/options.c ppp-2.4.4/pppd/options.c
--- ppp-2.4.4.orig/pppd/options.c	2007-08-07 22:19:31.000000000 +0300
+++ ppp-2.4.4/pppd/options.c	2007-08-07 22:22:51.000000000 +0300
@@ -94,6 +94,7 @@
 int	kdebugflag = 0;		/* Tell kernel to print debug messages */
 int	default_device = 1;	/* Using /dev/tty or equivalent */
 char	devnam[MAXPATHLEN];	/* Device name */
+int defaultmetric = 0;		/* Metric of the default route */
 bool	nodetach = 0;		/* Don't detach from controlling tty */
 bool	updetach = 0;		/* Detach once link is up */
 int	maxconnect = 0;		/* Maximum connect time */
@@ -289,6 +290,10 @@
       "Number of seconds to wait for child processes at exit",
       OPT_PRIO },
 
+    { "defaultmetric", o_int, &defaultmetric,
+      "The metric of the default route",
+      OPT_LIMITS, 0, 32766 },
+
 #ifdef HAVE_MULTILINK
     { "multilink", o_bool, &multilink,
       "Enable multilink operation", OPT_PRIO | 1 },
diff -Nru ppp-2.4.4.orig/pppd/pppd.8 ppp-2.4.4/pppd/pppd.8
--- ppp-2.4.4.orig/pppd/pppd.8	2007-08-07 22:19:31.000000000 +0300
+++ ppp-2.4.4/pppd/pppd.8	2007-08-07 22:21:33.000000000 +0300
@@ -121,6 +121,9 @@
 This entry is removed when the PPP connection is broken.  This option
 is privileged if the \fInodefaultroute\fR option has been specified.
 .TP
+.B defaultmetric \fIn
+The metric of the default route configured by pppd; default is 0.
+.TP
 .B disconnect \fIscript
 Execute the command specified by \fIscript\fR, by passing it to a
 shell, after
diff -Nru ppp-2.4.4.orig/pppd/pppd.h ppp-2.4.4/pppd/pppd.h
--- ppp-2.4.4.orig/pppd/pppd.h	2007-08-07 22:19:31.000000000 +0300
+++ ppp-2.4.4/pppd/pppd.h	2007-08-07 22:24:09.000000000 +0300
@@ -276,6 +276,7 @@
 extern int	kdebugflag;	/* Tell kernel to print debug messages */
 extern int	default_device;	/* Using /dev/tty or equivalent */
 extern char	devnam[MAXPATHLEN];	/* Device name */
+extern int defaultmetric;		/* Metric of the default route */
 extern int	crtscts;	/* Use hardware flow control */
 extern bool	modem;		/* Use modem control lines */
 extern int	inspeed;	/* Input/Output speed requested */
diff -Nru ppp-2.4.4.orig/pppd/sys-linux.c ppp-2.4.4/pppd/sys-linux.c
--- ppp-2.4.4.orig/pppd/sys-linux.c	2007-08-07 22:19:31.000000000 +0300
+++ ppp-2.4.4/pppd/sys-linux.c	2007-08-07 22:21:33.000000000 +0300
@@ -1441,7 +1441,7 @@
 FILE *route_fd = (FILE *) 0;
 static char route_buffer[512];
 static int route_dev_col, route_dest_col, route_gw_col;
-static int route_flags_col, route_mask_col;
+static int route_flags_col, route_mask_col, route_metric_col;
 static int route_num_cols;
 
 static int open_route_table (void);
@@ -1484,6 +1484,7 @@
     route_dest_col = 1;
     route_gw_col = 2;
     route_flags_col = 3;
+    route_metric_col = 6;
     route_mask_col = 7;
     route_num_cols = 8;
 
@@ -1503,6 +1504,8 @@
 		route_gw_col = col;
 	    else if (strcasecmp(q, "flags") == 0)
 		route_flags_col = col;
+	    else if (strcasecmp(q, "metric") == 0)
+		route_metric_col = col;
 	    else if (strcasecmp(q, "mask") == 0)
 		route_mask_col = col;
 	    else
@@ -1545,6 +1548,7 @@
 
     rt->rt_flags = (short) strtoul(cols[route_flags_col], NULL, 16);
     rt->rt_dev   = cols[route_dev_col];
+    rt->rt_metric = (short) strtoul(cols[route_metric_col], NULL, 16);
 
     return 1;
 }
@@ -1567,6 +1571,8 @@
 
 	if (kernel_version > KVERSION(2,1,0) && SIN_ADDR(rt->rt_genmask) != 0)
 	    continue;
+	if (rt->rt_metric != defaultmetric) /* consider only routes with the same metric */
+	    continue;
 	if (SIN_ADDR(rt->rt_dst) == 0L) {
 	    result = 1;
 	    break;
@@ -1637,6 +1643,7 @@
     SIN_ADDR(rt.rt_gateway) = gateway;
 
     rt.rt_flags = RTF_UP | RTF_GATEWAY;
+    rt.rt_metric = defaultmetric + 1; /* +1 for binary compatibility */
     if (ioctl(sock_fd, SIOCADDRT, &rt) < 0) {
 	if ( ! ok_error ( errno ))
 	    error("default route ioctl(SIOCADDRT): %m");
@@ -1670,6 +1677,7 @@
     SIN_ADDR(rt.rt_gateway) = gateway;
 
     rt.rt_flags = RTF_UP | RTF_GATEWAY;
+    rt.rt_metric = defaultmetric + 1; /* +1 for binary compatibility */
     if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) {
 	if (still_ppp()) {
 	    if ( ! ok_error ( errno ))
EOF

patch -p1 < defaultmetric.patch
rm -f defaultmetric.patch

