#!/bin/sh
#
# ncc_reset
# reset past ncc credentials and registrations
#
##########################################################################
#
# (C) 2011 SUSE Linux Products GmbH, Nuremberg, Germany
# Author: Fabian Herschel (fabian.herschel@suse.com)
#
##########################################################################
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
##########################################################################
#

ncc_cred="/etc/zypp/credentials.d/NCCcredentials"
ncc_cach="/var/cache/SuseRegister/lastzmdconfig.cache"
ncc_uppd="nu_novell_com"
ncc_reset=0

usage()
{
	echo "ncc_reset --help | [--ncc_uppd xx] [--ncc_cach xx] [--ncc_cred xx] --ncc_reset"
	exit 0
}

reset_ncc_credentials()
{
   if [ -n "$ncc_cred" -a -e "$ncc_cred" ]
   then 
	rm -I -- "$ncc_cred"
   fi
}


reset_ncc_suseregister_cache()
{
   if [ -n "$ncc_cach" -a -e "$ncc_cach" ]
   then
        rm -I -- "$ncc_cach"
   fi
}

check_ncc_uppd_channel()
{
    zypper ls | grep  -q "$1"
}

reset_ncc_uppd_channel()
{
   if [ -n "$ncc_uppd" ]
   then
	   if check_ncc_uppd_channel "$ncc_uppd"
	   then
		zypper removeservice "$ncc_uppd"
	   fi
   fi
}

while [ $# -gt 0 ]
do
	case "$1" in
		--help | -h ) usage
			 ;;

		--ncc_uppd )
			ncc_uppd=$2
			shift
			;;
		--ncc_cred )
			ncc_cred=$2
			shift
			;;
		--ncc_cach )
			ncc_cach=$2
			shift
			;;
		--ncc_reset )
			ncc_reset=1
			;;
	 	--version )
			echo 0.3
			exit 1
			;;
	esac
	shift
done

if [ $ncc_reset -eq 1 ]
then
	reset_ncc_uppd_channel

	check_ncc_uppd_channel

	reset_ncc_credentials

	reset_ncc_suseregister_cache
else
	usage
fi

