echo "\nInstalling openldap server and ldap-utils using aptitude. You should run this script as root so that we have rights to install and configure ldap server[Enter]" 
read ok
aptitude install slapd ldap-utils

echo "\nRemoveing backup of all database /var/backup/unknown...ldapdb, type in [y]"
read removebackup
if [ "$removebackup" = "y" ];then 
 rm -r /var/backups/unknown-2.4*ldapdb
 echo "/var/backup/unknown-2.4*ldapdb removed backup files successfully.[Enter] "
 read ok 
else
 echo "Not removing the backup. This might prevent the dpkg-reconfigure sladp from sucessfully reconfiguring. The backup files are put there after first reconfiguration but reconfiguring the slapd again doesn't overwrite the backup files. That is the reason we want to remove the backup files so that reconfiguration is successfull[Enter]"
 read ok
fi

echo "\nReconfiguring slapd server to get the proper settings. Use the default choices, enter the domain/website you will use (example: mycompany.com) and enter the password that we will use. [Enter]"
read ok
dpkg-reconfigure slapd

echo "\nDid the reconfiguration finished successfully? Read the lines above?[Enter]"
read ok
echo "\nWe will try to start and restart ldap. You should see the starting messages. [Enter]"
read ok
/etc/init.d/slapd start
/etc/init.d/slapd restart

echo "\nWe will Run a basic search to see if everything is there. Replace mycompany.com with your website address."

echo "\nType in your website/domain name like this example: dc=mycompany,dc=com"
read domain_ldap
echo "We are going to do ldapsearch -x -b $domain_ldap [Enter]"
read ok
ldapsearch -x -b $domain_ldap
echo "\n This search should return domain and administrative username. You should see either cn=admin or cn=manager. Note which one is it.[default is admin]. If you don't see the username then try restarting this script.[Enter]"
read ok
echo "\nDownloading and unzipping migration tools.[Enter]"
read ok
wget -cN http://www.padl.com/download/MigrationTools.tgz
tar -xzvf MigrationTools.tgz

echo "Entering MigrationTools-47"
cd MigrationTools-47

echo "\nNow we will replace padl.com inside the migration_common.sh with your domain/website. \nI assume that .com will stay. Enter the domain name without the .com. \nExample: mycompany\n"
read replace_company
echo "replacing dc=padl,dc=com with dc=$replace_company,dc=com [Enter]"
read ok
/etc/init.d/slapd start
find ./migrate_common.ph -type f -print0 | xargs -0 sed -i "s/padl/$replace_company/g"

echo "[Required]Migrating Base settings from your Linux system to Ldap using migrate_base.pl [Enter]"
read ok
perl ./migrate_base.pl >base.ldif
ldapadd -h localhost -x -W -D "cn=admin,$domain_ldap" -c -f base.ldif

echo "[Required]Migrating Passwords from your Linux system to Ldap using migrate_passwd.pl. This will convert the users, their settings, and passwords.[Enter]"
read ok
perl ./migrate_passwd.pl /etc/passwd >passwd.ldif 
ldapadd -h localhost -x -W -D "cn=admin,$domain_ldap" -c -f passwd.ldif

echo "[Required]Migrating Group from your Linux system to Ldap using migrate_group.pl. This will convert the groups and list of users that belong to these groups. [Enter]"
read ok
perl ./migrate_group.pl /etc/group >group.ldif 
ldapadd -h localhost -x -W -D "cn=admin,$domain_ldap" -c -f group.ldif

echo "If all this went throug sucessfully then your ldap server should be ready to authenticate some clients. We only require base, users and groups to provide authentication via ldap. Now setup your other computer as a client and see how nice this works."
read ok

