#!/bin/bash
#----------------------------------------------------------------------------
# /var/install/bin/samba-save-sid - save Samba SID to /root/MACHINE.SID
#
# Copyright (c) 2002-2013 Thomas Bork, tom(at)eisfair(dot)net
#
# usage: /var/install/bin/samba-save-sid
#    or: /var/install/bin/samba-save-sid noninteractive
#
# Creation   : 2003-05-02 tb
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------
# set -x
. /var/install/include/eislib

doit='false'
netbin='/usr/bin/net'
sidfile='/root/MACHINE.SID'

case $# in
0)
    interactive='true'
    ;;
1)
    if [ "$1" = "noninteractive" ]
    then
        interactive='false'
        doit='true'
    fi
    ;;
*)
    echo "usage: /var/install/bin/samba-save-sid" >&2
    echo "   or: /var/install/bin/samba-save-sid noninteractive" >&2
    exit 1
    ;;
esac

if [ "$interactive" = "true" ]
then
    clrhome
    mecho --info "Save Samba SID to $sidfile"
    echo
    if /var/install/bin/ask "Do you really want to save Samba SID to $sidfile" "yes"
    then
        doit='true'
    fi
fi

echo
if [ "$doit" = "true" ]
then
    . /etc/config.d/cui-samba
    sid=`$netbin getlocalsid $SAMBA_WORKGROUP 2>/dev/null | cut -d' ' -f6`

    if [ -n "$sid" ]
    then
        echo "$sid" >"$sidfile"
        if [ $? -eq 0 ]
        then
            mecho --info "Samba SID"
            echo
            echo "$sid"
            echo
            mecho --info "saved to $sidfile."
        else
            mecho --error "Could not save Samba SID"
            echo
            echo "$sid"
            echo
            mecho --error "to $sidfile!"
        fi
    else
        mecho --error "Could not fetch Samba SID for Domain $SAMBA_WORKGROUP!"
        mecho --error "Is Samba configured as PDC?"
    fi
else
    echo "Samba SID not saved."
fi

if [ "$interactive" = "true" ]
then
    anykey
fi
