#!/bin/bash
# USAGE: install.sh -r sparky -u sparky -p sparky

rootpwd="sparky"
username="sparky"
userpwd="sparky"

# while getopts r:u:p: option
# do
# case "${option}"
# in
# r) rootpwd=${OPTARG};;
# u) username=${OPTARG};;
# p) userpwd=${OPTARG};;
# esac
# done

# Install LDAP Server
 sudo yum install openldap openldap-servers openldap-clients -y

# Create the config files
sudo cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
sudo cp /usr/share/openldap-servers/slapd.conf.obsolete /etc/openldap/slapd.conf

# Bounce LDAP
sudo service slapd restart

#
pwd_hash=$(slappasswd -s $rootpwd)

cat > /tmp/config.ldif <<EOF
dn: olcDatabase={0}config,cn=config
changetype: modify
add: olcRootPW
olcRootPW: $pwd_hash

dn: olcDatabase={2}bdb,cn=config
changetype: modify
add: olcRootPW
olcRootPW: $pwd_hash
-
replace: olcRootDN
olcRootDN: cn=dev,dc=example,dc=com
-
replace: olcSuffix
olcSuffix: dc=example,dc=com
EOF

sudo ldapadd -Y EXTERNAL -H ldapi:/// -f /tmp/config.ldif

cat > /tmp/accounts.ldif <<EOF 
dn: dc=example,dc=com
objectclass: domain
objectclass: top
dc: example

dn: ou=dev,dc=example,dc=com
objectclass: organizationalUnit
ou: dev
description: Container for developer entries

dn: uid=$username,ou=dev,dc=example,dc=com
uid: $username
objectClass: inetOrgPerson
userPassword: $userpwd
sn: $username
cn: dev
EOF

ldapadd -D "cn=dev,dc=example,dc=com" -w $rootpwd -f /tmp/accounts.ldif

sudo /usr/lib/spark/sbin/stop-thriftserver.sh
sudo /usr/lib/spark/sbin/start-thriftserver.sh --master yarn