#!/bin/bash
LIST=/etc/apt/sources.list.d/metaconfig.list
BOOTSTRAP_URL=http://b.adora.dk/
#CERT_URL=$BOOTSTRAP_URL/deb.adora.dk.gpg.key
SVN_CERT=/etc/subversion/ca.adora.dk.crt
SPYE_DIR=/etc/spye
SPYE_DIR_METACONFIG=/etc/metaconfig
SPYE_DIR_METACONFIG_RES=/etc/metaconfig/res
ZSH_URL=https://svn.adora.dk/svn/config/projects/metaconfig/vineyard/zsh
NODE_ID=""
NODE_FQDN=""


WHIT="\033[01;m"
GREN="\033[01;32m"
NORM="\033[m"

function print()
{
  echo -e "* "${GREN}${*}${NORM}
}

set -e

## Check if this user already has a .subversion/servers configuration
## If not, then overwrite configuration to store passwords as plaintext later
if [[ -f ~/.subversion/servers ]]; then
    ## Check if md5sum is: 8f253f1970e97577e7f8ece2a9409c9a  /root/.subversion/servers
    ## IF it is, then it is default

    HAS_SVN_CONFIG=1
else
    HAS_SVN_CONFIG=0
fi

#print "Adding APT signing key..."
#wget -O- $CERT_URL 2> /dev/null | apt-key add -

print "Adding Adora source..."
echo "deb [signed-by=/etc/apt/keyrings/deb.adora.dk.gpg.key] http://deb.adora.dk/repos/apt/debian stable main" > $LIST
wget -O /etc/apt/keyrings/deb.adora.dk.gpg.key https://b.adora.dk/deb.adora.dk.gpg.key

print "Updating sources..."
apt-get update
print "Installing Subversion"
apt-get -y -o DPkg::Options::="--force-confold"  install subversion

if [[ ! -r $SVN_CERT ]]; then
  print "Downloading svn trust certificate"
  mkdir -p ${SVN_CERT%/*}
  print "Installing SVN certificate"
  wget -O $SVN_CERT http://b.adora.dk/AdoraCA-cacert.pem > /dev/null
  echo -e "[global]\nssl-authority-files = $SVN_CERT" > /etc/subversion/servers
fi

print "Installing spye, metaconfig and zsh..."
#apt-get -y -o DPkg::Options::="--force-confold" install spye metaconfig zsh
apt-get -y -o DPkg::Options::="--force-confold" install zsh

if [[ -d /etc/zsh/.svn ]]; then
  print "Subversioned zsh files are already installed"
else
  ##read -p "Delete current zsh rc files? [y/N] " res
  ##if [[ $res == "y" ]]; then
    print "Deleting current zsh rc files..."
    rm -rf /etc/zsh
    print "Checking out subversioned zsh files.."
    svn co -q $ZSH_URL /etc/zsh --username=$NODE_FQDN
    print "Checkout complete"
  ##fi
fi

for DIR in $SPYE_DIR $SPYE_DIR_METACONFIG $SPYE_DIR_METACONFIG_RES; do
  if [[ -d $DIR ]]; then
    print "Setting permissions for $DIR..."
    chmod 0700 $DIR
  else
    print "Creating $DIR..."
    mkdir -m 0700 -p $DIR
  fi
done

## If this user did not already have a svn config, overwrite the configuration which must be a default one
if [[ ! $HAS_SVN_CONFIG  ]]; then
    print "DID NOT HAVE SVN CONFIG. WOULD change config... FIXME FIXME"
    print "$HAS_SVN_CONFIG"
    ##echo "store-plaintext-passwords = yes" >> /root/.subversion/servers
else
    print "DID _ALREADY_ HAVE SVN CONFIG."
    print "$HAS_SVN_CONFIG"
fi



function check_svn()
{
  TARGET=$1
  SOURCE=$2

  if [[ $NODE_ID == "" ]]; then
    read -e -p"Please input node id (example: semillon)>"
    NODE_ID=$REPLY
  fi
  if [[ ! -d $TARGET/.svn ]]; then
    print "Checking out $SOURCE to $TARGET..."
    svn co --username config_$NODE_ID $SOURCE $TARGET
  else
    print "Already checked out $TARGET..."
  fi
}


if [[ $NODE_ID == "" ]]; then
    read -e -p"Please input node id (example: semillon)>"
    NODE_ID=$REPLY
fi

if [[ $NODE_FQDN == "" ]]; then
    read -e -p"Please input node fully qualified domain name (FQDN)  (example: semillon.adora.dk)>"
    NODE_FQDN=$REPLY
fi

print "Checking for existing node config"
if [[ -d $SPYE_DIR_METACONFIG/node ]]; then
    mv -v $SPYE_DIR_METACONFIG/node $SPYE_DIR_METACONFIG/node-bootstrap-backup
fi

print "Checking out node config"
cd /etc/metaconfig
svn co https://svn.adora.dk/svn/config/projects/metaconfig/vineyard/nodes/$NODE_ID node --username=$NODE_FQDN

print "Checking out resources"
cd /etc/metaconfig/res
svn co https://svn.adora.dk/svn/config/projects/metaconfig/vineyard/profiles --username=$NODE_FQDN
#svn co https://svn.adora.dk/svn/config/projects/metaconfig/vineyard/public
#svn co https://svn.adora.dk/svn/config/projects/metaconfig/vineyard/som 
svn co https://svn.adora.dk/svn/config/projects/metaconfig/vineyard/config

# Installing pylogtools is necessary for syslog-ng logwatch to work
print "Fetching pylogtools..."
PYLOGTOOLS_DIST_URL="https://b.adora.dk/pylogtools-0.4.tar.gz"
PYLOGTOOLS_INSTALL_DIR="/opt"


cd $PYLOGTOOLS_INSTALL_DIR
wget $PYLOGTOOLS_DIST_URL
tar -xvf "pylogtools-0.4.tar.gz"
cd pylogtools-0.4
./create_symlink


print "Fetching spye and metaconfig..."
SPYE_DIST_URL="https://b.adora.dk/spye-portable.tar.gz"
SPYE_INSTALL_DIR="/opt"
cd "$SPYE_INSTALL_DIR" 
wget "$SPYE_DIST_URL"
tar -xvf spye-portable.tar.gz

bash /opt/spye/doc/install-prereqs


export SPYE_ROOT="/opt/spye"
export PYENV_ROOT="$SPYE_ROOT/.pyenv"
export PYENV_VERSION=2.7.18

mkdir -p $SPYE_ROOT
cd $SPYE_ROOT

# Install pyenv
curl https://pyenv.run | bash

# Add pyenv to PATH
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"

# init pyenv
eval "$(pyenv init -)"

# init pyenv and virtualenv
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"

# install python 2.7.18
pyenv install $PYENV_VERSION

print "Adding $SPYE_INSTALL_DIR to PATH"
echo 'export PATH="/opt/spye/bin:$PATH"' >> ~/.bashrc
echo 'export PATH="/opt/spye/bin:$PATH"' >> ~/.zshrc

export PATH="/opt/spye/bin:$PATH"



print "Running metaconfig dry-run"
metaconfig


print "All done!"

print ""
print "Optionally enable spye-firewall using these commands:"
## Installing spye-firewall
#
#    mkdir -p /etc/spye/firewall
#        echo "reset" > /etc/spye/firewall/stop
#
#            ln -s /opt/spye/bin/spye /usr/bin/spye
#
#                cp /opt/spye/doc/spye-firewall.init /etc/init.d/spye-firewall
#
#                    update-rc.d spye-firewall defaults

## Installing spye-firewall

echo 'mkdir -p /etc/spye/firewall'
echo 'echo "reset" > /etc/spye/firewall/stop'
echo 'ln -s /opt/spye/bin/spye /usr/bin/spye'
echo 'cp /opt/spye/doc/spye-firewall.init /etc/init.d/spye-firewall'
echo 'chmod +x /etc/init.d/spye-firewall'
echo 'update-rc.d spye-firewall defaults'

