#!/bin/bash

### This script will allow you to own your Cloudkey Gen2 and Gen2+.

### This is a work in progress version 0.3... more features to come.

### IF YOU USE THIS IT IS AT YOUR OWN RISK. IT WILL VOID YOUR WARRANTY!!!

### I will add more features to this repo, like creating a single
### partition drive, and mounting internal / external drives when
### rebooting.

echo "About to remove almost all the UniFi software."
echo "THIS WILL VOID ANY WARRANTY!!! You act at your own choice."
echo "I am not responsible if anything goes wrong."
echo
echo "Type: \"I understand.\""
read check

if [[ $check == "I understand." ]]; then
  echo "Here goes... hold thumbs..."
  echo "You may see errors with the process. I am attempting"
  echo "to remove all known packages for all versions of UniFi"
  echo "that I have tested this on... process will start in 5sec"
else
  echo "Challenge was typed incorrectly or omitted. Exiting..."
  exit 1
fi

sleep 5s

apt update
apt -y upgrade
apt -y remove mongodb-clients
apt -y remove mongodb-server-core
apt -y remove mongodb-server
apt -y remove uid-agent
apt -y remove unifi-pion-gw
apt -y remove node18
apt -y remove unifi-core
apt -y remove ucore-setup-listener
apt -y remove uck-tools
apt -y remove ubnt-zram-swap
apt -y remove ubnt-unifi-setup
apt -y remove ubnt-ucp4cpp
apt -y remove ubnt-systemhub
apt -y remove ubnt-opencv4-libs
apt -y remove ubnt-disk-smart-mon
apt -y remove ustd
apt -y remove ubnt-binmecpp
apt -y remove ubnt-archive-keyring
apt -y remove python3-unifi-console-protos
apt -y remove simple-pid
apt -y remove ui-snmp
apt -y remove unifi-assets-uckp
apt -y remove unifi-email-templates-all
apt -y remove unifi-identity-update
apt -y remove uos-agent
apt -y remove uos
apt -y remove nginx
apt -y remove node20
apt -y remove unifi-core
apt -y remove node22
apt -y remove openjdk-17-jre-headless
apt -y remove nodejs
mkdir /usr/share/postgresql/9.6/man/man1
touch /usr/share/postgresql/9.6/man/man1/psql.1.gz
apt -y remove postgresql-9.6
apt -y remove postgresql-client-9.6
apt -y remove postgresql-contrib-9.6
mkdir /usr/share/postgresql/14/man/man1
touch /usr/share/postgresql/14/man/man1/psql.1.gz
apt -y remove postgresql-14
apt -y remove postgresql-client-14
mkdir /usr/share/postgresql/16/man/man1
touch /usr/share/postgresql/16/man/man1/psql.1.gz
apt -y remove postgresql-16
apt -y remove postgresql-client-16
apt -y remove postgresql
apt -y remove postgresql-common
apt -y remove postgresql-client-common

echo "Paste your rsa_id.pub key and press enter..."
read authorizedkey

mkdir /root/.ssh
echo "$authorizedkey" > /root/.ssh/authorized_keys

touch /etc/ssh/sshd_config.d/99-allow-keys.conf
echo "PermitRootLogin yes" >> /etc/ssh/sshd_config.d/99-allow-keys.conf
echo "PubkeyAuthentication yes" >> /etc/ssh/sshd_config.d/99-allow-keys.conf
echo "AuthorizedKeysFile	.ssh/authorized_keys .ssh/authorized_keys2" >> /etc/ssh/sshd_config.d/99-allow-keys.conf

echo "Edit /etc/ssh/sshd_config.d/99-allow-keys.conf to disable"
echo "Root Login with Password using SSH. I wouldn't suggest it"
echo "as if you get locked out of your CloudKey there is no screen and keyboard to get back in. Instead you can give root a much stronger password using..."
echo
echo "    # passwd root"
echo
echo "Press ENTER to reboot..."

read check

reboot now
