﻿#! /bin/bash

# !!! UNTESTED YET (2023/03/28) !!!

# uncomment next row to be verbose (debugging)
# set -x

if [ "$UID" -ne "0" ]
then
  echo "You are not root. Exiting.."
  exit 1
fi

apt update && apt -y upgrade

UNIXTIMENOW=$(date +%s)

# **Configure NIC interfaces:**

mv /etc/network/interfaces /etc/network/interfaces.$UNIXTIMENOW

cat  << 'BASTA' > /etc/network/interfaces
# This file describes the network interfaces available 
# on your system and how to activate them. For more 
# information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface

auto lo
iface lo inet loopback

# The primary network interface

allow-hotplug eth0
iface eth0 inet dhcp

# The secondary face

allow-hotplug eth1
    iface eth1 inet static
    address 10.0.10.1
    netmask 255.255.255.0
    dns-nameservers 10.0.10.1
BASTA

# **Host name**

HOSTNAME="gateway"

if [ ! -f "/etc/hostname.0" ]
then
  cp /etc/hostname /etc/hostname.$UNIXTIMENOW
fi
echo $HOSTNAME > /etc/hostname
hostname $HOSTNAME

apt install -y dnsmasq

# **DNSMasq**

cp /etc/dnsmasq.conf /etc/dnsmasq.conf.$UNIXTIMENOW
cat  << 'BASTA' >> /etc/dnsmasq.conf
interface=eth1
address=/ufo.mars/10.0.10.1
dhcp-range=eth1,10.0.10.20,10.0.10.25,12h
BASTA

# **Masquerade**

apt install -y iptables

if [ ! -f "/etc/network/if-up.d/001masquerade" ]
then
  mv /etc/network/if-up.d/001masquerade \
     /etc/network/if-up.d/masquerade.$UNIXTIMENOW
fi

cat << 'BASTA' > /etc/network/if-up.d/001masquerade
#!/bin/sh
# Reload the iptables rules and activate forwarding

# delete all existing rules.
iptables -Z             # zero counters
iptables -F             # flush (delete) rules
iptables -t mangle -F
iptables -X             # delete all extra chains

# If you want to clear the chains, then clear the chains:
iptables --policy INPUT   ACCEPT;
iptables --policy OUTPUT  ACCEPT;
iptables --policy FORWARD ACCEPT;

# Masquerade.
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

# Enable routing.
echo 1 > /proc/sys/net/ipv4/ip_forward

exit 0
BASTA
chmod +x /etc/network/if-up.d/001masquerade

# **Apache Web Server**

apt install -y apache2

if [ ! -f "/var/www/html/index.html" ]
then
  mv /var/www/html/index.html \
     /var/www/html/index.html.$UNIXTIMENOW
fi

cat << 'BASTA' > /var/www/html/index.html
<!DOCTYPE html>
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   <title>Welcome to Mars</title>
</head>
   <body>
     <span style="color:red"><h1>WELCOME TO MARS</h1></span>
     <img src="mars.jpg">
   </body>
</html>
BASTA

# **Wget**

apt -y install wget

wget -c https://starwalk.space/gallery/images/mars-the-ultimate-guide/1140x641.jpg
cp 1140x641.jpg /var/www/html/mars.jpg

# **All done**

echo "PLEASE REBOOT NOW. BE SURE you have 2 NIC's."
