#!/bin/bash

# Variablen initialisieren
IP=""
GW=""
HN=""
DN="lab.int"
NS="172.26.54.2"

# Optionen mit getopts verarbeiten
while getopts "i:g:h:d:n:" opt; do
  case $opt in
    i) IP="$OPTARG" ;;
    g) GW="$OPTARG" ;;
    h) HN="$OPTARG" ;;
    d) DN="$OPTARG" ;;
    n) NS="$OPTARG" ;;
    *) 
       echo "Usage: $0 -i <IP/Netmask> -g <Gateway> -h <Hostname> -d <Domain> -n <Nameserver>"
       exit 1
       ;;
  esac
done

# Überprüfen, ob alle erforderlichen Variablen gesetzt sind
if [[ -z "$IP" || -z "$GW" || -z "$HN" || -z "$DN" || -z "$NS" ]]; then
  echo "All options are required."
  echo "Usage: $0 -i <IP/Netmask> -g <Gateway> -h <Hostname> -d <Domain> -n <Nameserver>"
  exit 1
fi

# Konfigurationsdateien erstellen
cat <<EOF > /etc/network/interfaces
source /etc/network/interfaces.d/*
auto lo
iface lo inet loopback

auto enp0s3
iface enp0s3 inet static
 address $IP
 gateway $GW
EOF

cat <<EOF > /etc/hosts
# This is an autoconfigured IPv6 interface
127.0.0.1    localhost
127.0.1.1    $HN.$DN $HN

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
EOF

cat <<EOF > /etc/resolv.conf
domain $DN
search $DN.
nameserver $NS
EOF

# Hostname setzen
echo "$HN.$DN" > /etc/hostname

echo "Konfigurationsdateien wurden erfolgreich erstellt."
