#! /bin/sh
# Copyright (c) 2000-2002 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# Author: Burchard Steinbild, 1996-97
#         Bernhard Kaindl <bk@suse.de>, 1999
#         Rüdiger Oertel <ro@suse.de>, 2000-01
#
# This module sorts the passwd and group files

# check if we are started as root
# only one of UID and USER must be set correctly
if test "$UID" != 0 -a "$USER" != root; then
    echo "You must be root to start $0."
    exit 1
fi

r=$ROOT

test -f $r/lib/YaST/SuSEconfig.functions || {
    echo "ERROR - can not find $r/lib/YaST/SuSEconfig.functions!!"
    echo "This should not happen.  Exit..."
    exit 1
}
. $r/lib/YaST/SuSEconfig.functions

for i in $r/etc/sysconfig/suseconfig ; do
  if test ! -f $i ; then
    echo "No $i found."
    exit 1
  fi

  . $i
done

if test -n "$ENABLE_SUSECONFIG" -a "$ENABLE_SUSECONFIG" = "no" ; then
    echo "SuSEconfig is disabled in $r/etc/sysconfig/suseconfig."
    echo "Exit..."
    exit 0
fi

if test "$SORT_PASSWD_BY_UID" = yes ; then
    for i in passwd group ; do
        cat $r/etc/$i | grep "^root:" > $r/etc/$i.t
        cat $r/etc/$i | grep -v "^+" | grep -v ":-[0-9]:-[0-9]:" | \
            grep -v "^root:" | sort -n -t : -k 3,3 >> $r/etc/$i.t
        test -s $r/etc/$i.t && {
            cat $r/etc/$i | grep ":-[0-9]:-[0-9]:" >> $r/etc/$i.t
            cat $r/etc/$i | grep "^+" >> $r/etc/$i.t
            cmp -s $r/etc/$i $r/etc/$i.t || {
                echo "Sorting /etc/$i. Saving old to /etc/$i.unsorted."
                mv $r/etc/$i $r/etc/$i.unsorted
                cp $r/etc/$i.t $r/etc/$i
            }
        }
        rm $r/etc/$i.t
    done
fi


