#!/usr/bin/python3
#
# lmn7-appliance
# thomas@linuxmuster.net
# 20190903
#

import getopt
import os
import sys
import urllib.request

# check if the user and go on
if not os.geteuid() == 0:
    sys.exit('Please start the script as root user.\n You can use the command "sudo -i" to achieve this.')

# help
def usage(rc):
    print('Downloads and installs linuxmuster-prepare package and initially prepares')
    print('a linuxmuster.net ubuntu appliance.\n')
    print('Usage: lmn7-appliance [options]')
    print('\n[options] are:\n')
    print('-t, --hostname=<hostname>   : Hostname to apply (optional, works only with')
    print('                              server profile).')
    print('-n, --ipnet=<ip/bitmask>    : Ip address and bitmask assigned to the host')
    print('                              (optional, default is 10.0.0.x/16, depending')
    print('                              on the profile).')
    print('-p, --profile=<profile>     : Host profile to apply, mandatory. Expected')
    print('                              values are "server", "opsi", "docker" or "ubuntu".')
    print('                              Profile name is also used as hostname, except for')
    print('                              "server" if set with -t.')
    print('-l, --pvdevice=<device>     : Device to use for lvm (server profile only).')
    print('-f, --firewall=<firewallip> : Firewall/gateway ip (default *.254).')
    print('-d, --domain=<domainname>   : Domainname (default linuxmuster.lan).')
    print('-u, --unattended            : Unattended mode, do not ask, use defaults.')
    print('-h, --help                  : Print this help.')
    sys.exit(rc)

# get cli args
try:
    opts, args = getopt.getopt(sys.argv[1:], "d:f:hl:n:p:t:u", ["domain=", "firewall=", "help", "pvdevice=", "ipnet=", "profile=", "hostname=", "unattended"])
except getopt.GetoptError as err:
    # print help information and exit:
    print(err) # will print something like "option -a not recognized"
    usage(2)

# default values
unattended = ''
profile = ''
hostname = ''
domainname = ''
firewallip = ''
ipnet = ''
pvdevice = ''
profile_list = ['server', 'opsi', 'docker', 'ubuntu']

# evaluate options
for o, a in opts:
    if o in ("-u", "--unattended"):
        unattended = ' -u'
    elif o in ("-p", "--profile"):
        profile = a
    elif o in ("-t", "--hostname"):
        hostname = ' -t ' + a
    elif o in ("-l", "--pvdevice"):
        pvdevice = ' -l ' + a
    elif o in ("-d", "--domain"):
        domainname = ' -d ' + a
    elif o in ("-n", "--ipnet"):
        ipnet = ' -n ' + a
    elif o in ("-f", "--firewall"):
        firewallip = ' -f ' + a
    elif o in ("-h", "--help"):
        usage(0)
    else:
        assert False, "unhandled option"
        usage(1)

if not profile in profile_list or profile == '':
    usage(1)

profile = ' -p ' + profile

# repo data
url = 'https://archive.linuxmuster.net/'
repokey = 'archive.linuxmuster.net.key'
repokey_remote = url + repokey
repokey_local = '/tmp/' + repokey
lmn7list_remote = url + 'lmn7/lmn7.list'
lmn7list_local = '/etc/apt/sources.list.d/lmn7.list'
pkgname = 'linuxmuster-prepare'
rmpkgs = 'lxd lxd-client lxcfs lxc-common snapd'

# return content of text file
def readTextfile(tfile):
    if not os.path.isfile(tfile):
        return False, None
    try:
        infile = open(tfile , 'r')
        content = infile.read()
        infile.close()
        return True, content
    except:
        print('Cannot read ' + tfile + '!')
        return False, None

print('### lmn7-prepare')

# install lmn7 repo
print('## install lmn7 repo')
urllib.request.urlretrieve(repokey_remote, repokey_local)
urllib.request.urlretrieve(lmn7list_remote, lmn7list_local)
os.system('apt-key add ' + repokey_local)

# get updates
print('## install software updates')
os.system('apt clean')
os.system('apt update')
os.system('DEBIAN_FRONTEND=noninteractive apt -y purge ' + rmpkgs)
os.system('DEBIAN_FRONTEND=noninteractive apt -y dist-upgrade')

# get lmn7 packages
print('## install linuxmuster-prepare package')
os.system('DEBIAN_FRONTEND=noninteractive echo -e "y\ny\ny\n" | apt -y install ' + pkgname)

# invoke script
print('## invoke linuxmuster-prepare')
os.system('linuxmuster-prepare -i' + unattended + profile + hostname + domainname + firewallip + ipnet + pvdevice)
