#!/usr/bin/env python3
import os

try:
    os.remove("axelspringer-hosts")
    os.remove("ios-adguard.txt")
except Exception:
    pass


hostsfile = open("axelspringer-hosts", "w+")
iosAdguardFile = open("ios-adguard.txt", "w+")

hostsfile.write("# This is an autogenerated file by ./generate_hostfile.py with the files \"www-hostnames\" and \"hostnames\"\n\n")

wwwhostnames = open("www-hostnames", "r")

for line in wwwhostnames.readlines():
    if (line[0] == "\n" or line[0] == "#"):
        continue
    hostsfile.write("0.0.0.0 " + line)
    iosAdguardFile.write(line)
    if (line[-1] != "\n"):
        hostsfile.write("\n")
        iosAdguardFile.write("\n")
    hostsfile.write("0.0.0.0 www." + line)
    if (line[-1] != "\n"):
        hostsfile.write("\n")
        iosAdguardFile.write("\n")

hostsfile.write("\n")
iosAdguardFile.write("\n")
wwwhostnames.close()

hostnames = open("hostnames", "r")

for line in hostnames.readlines():
    if (line[0] == "\n" or line[0] == "#"):
        continue
    hostsfile.write("0.0.0.0 " + line)
    iosAdguardFile.write(line)
    if (line[-1] != "\n"):
        hostsfile.write("\n")
        iosAdguardFile.write("\n")

hostnames.close()
hostsfile.flush()
hostsfile.close()
iosAdguardFile.flush()
iosAdguardFile.close()
