#!/bin/bash

DEFAULTSDIR="/lib/alterator/defaults"

print_help(){
	echo "default-restore - restores configuration files to default."
	echo
	echo "Usage:"
	echo "default-restore [-h | --help | help]"
	echo "                     - show this message."
	echo "default-restore smb  - restore default config file"
	echo "                       /etc/samba/smb.conf."
	echo "default-restore sssd - restore default config file"
	echo "                       /etc/sssd/sssd.conf."
	echo "default-restore krb5 - restore default config file"
	echo "                       /etc/krb5.conf."
	echo "default-restore all  - restore default config files"
	echo "                       (smb.conf, sssd.conf, krb5.conf)."
}

restore_smb(){
	if [ -f "$DEFAULTSDIR/samba/smb.conf" ]; then
		cp -f "$DEFAULTSDIR/samba/smb.conf" /etc/samba/smb.conf
		chown root:root /etc/samba/smb.conf
		chmod 644 /etc/samba/smb.conf
	else
		echo "File $DEFAULTSDIR/samba/smb.conf does not exist."
	fi

	if [ -f "$DEFAULTSDIR/samba/usershares.conf" ]; then
		cp -f "$DEFAULTSDIR/samba/usershares.conf" /etc/samba/usershares.conf
		chown root:root /etc/samba/usershares.conf
		chmod 644 /etc/samba/usershares.conf
	else
		echo "File $DEFAULTSDIR/samba/usershares.conf does not exist."
	fi
}

restore_sssd(){
	if [ -f "$DEFAULTSDIR/sssd/sssd.conf" ]; then
		cp -f "$DEFAULTSDIR/sssd/sssd.conf" /etc/sssd/sssd.conf
		chown root:root /etc/sssd/sssd.conf
		chmod 600 /etc/sssd/sssd.conf
	else
		echo "File $DEFAULTSDIR/sssd/sssd.conf does not exist."
	fi
}

restore_krb5(){
	if [ -f "$DEFAULTSDIR/krb5.conf" ]; then
		cp -f "$DEFAULTSDIR/krb5.conf" /etc/krb5.conf
		chown root:root /etc/krb5.conf
		chmod 644 /etc/krb5.conf
	else
		echo "File $DEFAULTSDIR/krb5.conf does not exist."
	fi

	rm -rf /etc/krb5.keytab
}

restore_all(){
	restore_smb
	restore_sssd
	restore_krb5
}

if [ "$#" -eq 0 ]; then
	print_help
elif [ "$#" -ne 1 ]; then
	echo "Wrong number of arguments."
	echo
	print_help
elif [ "$1" = '-h' -o "$1" = '--help' -o "$1" = 'help' ]; then
	print_help
elif [ "$1" = "smb" ]; then
	restore_smb
elif [ "$1" = "sssd" ]; then
	restore_sssd
elif [ "$1" = "krb5" ]; then
	restore_krb5
elif [ "$1" = "all" ]; then
	restore_all
else
	echo "Wrong argument."
	echo
	print_help
fi
