#!/usr/bin/env bash
#
# Copyright (c) 2003 VA Linux Systems Japan, K.K.
#
# LICENSE NOTICE
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
# 3. Neither the name of the company nor the names of its contributors
#   may be used to endorse or promote products derived from this software
#   without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

# This product includes software developed by Luke Howard.

#
# need ldapdiff and migrationtools
#

set -e

MIGRATIONDIR=/usr/share/migrationtools/

eval `ultrapossum-config init`
tmp=`tempfile`
conf=`tempfile`
add=`tempfile`
mod=`tempfile`
files="$tmp $conf $add $mod"
trap "/bin/rm -f $files; eval `ultrapossum-config term`" 0

chmod 600 $files
cat >$conf <<EOF
[global]
ldaphost: $LDAPMASTER
ldapport: 389
rootdn: $ROOTDN
rootpw: $ROOTPW
modfile: $mod
addfile: $add
onlineupdate: no
offlineupdate: yes
onlineerrfatal: yes
plugin: no
#pluginfile: /usr/local/src/ldapdiff-0.7.0/plugins/ldapskeleton.so
pluginfile: no
pluginfunction: ldifskeleton
iconv: yes
ldifcharset: ISO-8859-1
ldapcharset: UTF-8
schemacheck: yes
schemabase: cn=subschema
schemafilter: objectClass=*
schemaattribute: attributetypes
schemahack: yes

[ou]
basedn: $SUFFIX
filter: ou
group: objectClass=organizationalUnit
ignore: none
multi: none
noequality: none
mapalias: none=none
deleteentry: no
deleteattribute: no
profilebasedn: none

[passwd]
basedn: ou=People,$SUFFIX
filter: uid
group: objectClass=posixAccount
ignore: none
multi: none
noequality: none
mapalias: none=none
deleteentry: no
deleteattribute: no
basedn: none
profilebasedn: none

[group]
basedn: ou=Group,$SUFFIX
filter: cn
group: objectClass=posixGroup
ignore: none
multi: none
noequality: none
mapalias: none=none
deleteentry: no
deleteattribute: no
basedn: none
profilebasedn: none


EOF

export LDAP_BASEDN=$SUFFIX

cd $MIGRATIONDIR

# base
cat >$tmp <<EOF
dn: ou=People,$SUFFIX
objectClass: organizationalUnit
ou: People

dn: ou=Group,$SUFFIX
objectClass: organizationalUnit
ou: Group

EOF
ldapdiff -f $tmp -p ou -c $conf
cat $add $mod | ldapadd -x -h $LDAPMASTER -D "$ROOTDN" -w "$ROOTPW" 

# passwd
$MIGRATIONDIR/migrate_passwd.pl /etc/passwd > $tmp
ldapdiff -f $tmp -p passwd -c $conf
cat $add $mod | ldapadd -x -h $LDAPMASTER -D "$ROOTDN" -w "$ROOTPW"

# group
$MIGRATIONDIR/migrate_group.pl /etc/group > $tmp
ldapdiff -f $tmp -p group -c $conf
cat $add $mod | ldapadd -x -h $LDAPMASTER -D "$ROOTDN" -w "$ROOTPW"
