class unifi {
  contain unifi::install
  contain unifi::service
}

class unifi::repos {
  apt::source { 'unifi':
    comment  => 'UniFi Repo',
    location => 'https://www.ui.com/downloads/unifi/debian',
    release  => 'stable',
    repos    => 'ubiquiti',
    key      => {
      'name'   => 'unifi.gpg',
      'source' => 'https://dl.ui.com/unifi/unifi-repo.gpg',
    },
    include  => {
      'deb' => true,
    },
  }

  apt::source { 'mongodb':
    comment  => 'MongoDB',
    location => 'http://repo.mongodb.org/apt/debian',
    release    => 'buster/mongodb-org/4.4',
    repos  => 'main',
    key      => {
      'name'   => 'mongodb.asc',
      'source' => 'https://www.mongodb.org/static/pgp/server-4.4.asc',
    },
    include  => {
      'deb' => true,
    },
  }
}

class unifi::libssl {
  # From https://archive.ubuntu.com/ubuntu/pool/main/o/openssl/
  file { '/opt/libssl1.1.deb':
    ensure => file,
    source => 'puppet:///modules/unifi/libssl1.1.deb' 
  } ~>
  package { 'libssl1.1':
    provider => dpkg,
    source => "/opt/libssl1.1.deb"
  }
}

class unifi::install {
  contain unifi::repos
  contain unifi::libssl


  $mongo = [ 'mongodb-org' ]
  package { $mongo:
    ensure => installed 
  }

  $unifi = ['openjdk-17-jre-headless', 'java-common', 'unifi']
  package { $unifi:
    ensure => installed 
  }
}

class unifi::service {
  service { 'mongod':
    ensure => running,
    enable => true
  }

  service { 'unifi':
    ensure => running,
    enable => true
  }
}
