# functions for AnNyung LInux bootstrap

PATH='/bin:/usr/bin:/sbin:/usr/sbin'
export PATH

an_print_title()
{
	echo "###################################################################"
	echo "# $*"
	echo "###################################################################"
	echo
}

an_print_msg()
{
	echo " * $1"
	if [ -z "$2" ]; then
		echo
	fi
}

an_release()
{
	an_print_title "Installation AnNyung LInux release package"
	rpm -q annyung-release &> /dev/null
	CHK=$?

	[ $CHK -ne 0 ] && \
		rpm -Uhv http://mirror.oops.org/pub/AnNyung/2/inst/annyung-release.rpm || \
		an_print_msg "Already installed"
}

an_setlang()
{
	an_print_title "Language Setting to i18n"
	lang=$1
	[ -z "$lang" ] && lang=ko_KR.UTF-8
	echo "LANG=$lang" > /etc/sysconfig/i18n
	echo "SYSFONT=latarcyrheb-sun16" >> /etc/sysconfig/i18n
	an_print_msg "Set to $lang"
}

an_remfirmware()
{
	an_print_title "Removed kernel firmware"
	yum -y remove a*firmware* b43-* b*firmware* i*firmware* l*firmware* \
				q*firmware* r*firmware* x*firmware* z*firmware*
	echo
	echo
}

an_remove()
{
	an_print_title "Remove Files"
	position=$1; shift

	if [ -d "$position" ]; then
		pushd $position &> /dev/null
		rm -rf $*
		popd &> /dev/null
		an_print_msg "Remove $*"
	else
		an_print_msg "No target directory"
	fi
}

_an_pkgrem()
{
	#for pkg in $*; do rpm -e --nodeps --allmatches $pkg; done
	yum -y remove $*
	echo
	echo
}

an_pkgrem()
{
	an_print_title "Remove Packages"
	_an_pkgrem $*
}

an_removecron()
{
	an_print_title "Remove overload cronjob"
	crondaily='/etc/cron.daily'
	cronweekly='/etc/cron.weekly'

	for file in makewhatis.cron slocate.cron mlocate.cron
	do
		[ -f "$crondaily/$file" ] && \
			mv -f $crondaily/$file $crondaily/.$file &> /dev/null && \
			an_print_msg "move $crondaily/$file to $crondaily/.$file" noecho
	done

	[ -f "$cronweekly/makewhatis.cron" ] && \
		mv -f $cronweekly/makewhatis.cron $cronweekly/.makewhatis.cron &> /dev/null && \
		an_print_msg "move $cronweekly/makewhatis.cron to $cronweekly/.makewhatis.cron"
}

an_service()
{
	an_print_title "Turn On/Off Init Sservice"
	flag=$1; shift 1
	for service in $*
	do
		if [ -f "/etc/rc.d/init.d/$service" ]; then
			chkconfig --level 35 $service $flag
			[ "$flag" == "on" ] && \
				an_print_msg "Turn on $service" noecho || \
				an_print_msg "Turn off $service" noecho
		elif [ -f "/etc/xinetd.d/$service" ]; then
			chkconfig $service $flag
			[ "$flag" == "on" ] && \
				an_print_msg "Turn on $service" noecho || \
				an_print_msg "Turn off $service" noecho
		fi
	done
	echo
}

_an_pkginst()
{
	[ -f /etc/yum.def.conf ] && addconf="-c /etc/yum.def.conf" || addconf=
	yum $addconf -y install $*
}

an_pkginst()
{
	an_print_title "Additional Pakcage Installation"
	_an_pkginst $*
	echo
}

an_pkgupdate()
{
	an_print_title "Pakcage Update"
	curl -o /etc/yum.repos.d/AnNyung.repo http://mirror.oops.org/pub/AnNyung/2/AnNyung.repo
	[ -f /etc/yum.def.conf ] && addconf="-c /etc/yum.def.conf" || addconf=
	yum clean all >& /dev/null
	yum $addconf -y update &> /dev/null
	echo
	echo
}

an_pwsort()
{
	mode=$1; shift

	case "$mode" in
		passwd)
			/usr/sbin/pwunconv
			/bin/cat /etc/passwd | \
				/bin/awk -F: '{print $3":"$1":"$2":"$4":"$5":"$6":"$7}' | \
				/bin/sort -n | \
				/bin/awk -F: '{print $2":"$3":"$1":"$4":"$5":"$6":"$7}' > \
				/tmp/passwd.$$
			/bin/mv /tmp/passwd.$$ /etc/passwd
			/usr/sbin/pwconv
			;;
		*)
			/usr/sbin/grpunconv
			/bin/cat /etc/group | \
				/bin/awk -F: '{print $3":"$1":"$2":"$4}' | \
				/bin/sort -n | \
				/bin/awk -F: '{print $2":"$3":"$1":"$4}' > \
				/tmp/group.$$
			/bin/mv /tmp/group.$$ /etc/group
			/usr/sbin/grpconv
	esac
}

an_userdel()
{
	an_print_title "Delete User"
	for user in $*
	do
		an_print_msg "Remove user '$user'" noecho
		/usr/sbin/userdel -r $user
	done
	echo

	an_pwsort passwd
	an_print_msg "Sort passwd file"
}

an_grpdel()
{
	an_print_title "Delete Group"
	for grp in $*
	do
		an_print_msg "Remove group '$grp'" noecho
		/usr/sbin/groupdel $grp
	done
	echo

	an_pwsort group
	an_print_msg "Sort group file"
}

an_ipv6_off()
{
	an_print_title "Turn off IPv6"
	_an_pkgrem iptables-ipv6
	echo "install ipv6 /bin/true" > /etc/modprobe.d/blacklist-ipv6.conf
	echo "NETWORKING_IPV6=no" >> /etc/sysconfig/network
}

an_varlibphpbin_link()
{
	cd /var/lib/php/bin >& /dev/null
	for l in $*
	do
		fname=`echo $l | sed 's!.*/!!g'`
		ln -sf $l $fname
	done
	cd - >& /dev/null
}

an_nozeroconf()
{
	cat /etc/sysconfig/network | grep NOZEROCONF >& /dev/null
	[ $? -ne 0 ] && \
		echo "NOZEROCONF=yes" >> /etc/sysconfig/network
}

an_selinux_off()
{
	an_print_title "Disable selinux"
	cat /etc/selinux/config | sed 's/SELINUX=.*/SELINUX=disabled/g' > /tmp/selinux_off.txt
	mv /tmp/selinux_off.txt /etc/selinux/config
}

an_x86_64_fix()
{
	uname -a | grep x86_64 >& /dev/null
	# if not x86_64 system, skip
	[ $? -eq 1 ] && return

	an_print_title "Remove Non 64bit packages"
	for i in $(rpm -qa --qf '%{name}-%{version}-%{release}.%{arch}\n' | grep "i[36]86")
	do
		an_print_msg "Removed ix86 package $i" noecho
		rpm -e --nodeps $i >& /dev/null
	done
	#rpm -e 'openssl.i686' >& /dev/null
	echo
}

an_network()
{
	interface_cfg='/etc/sysconfig/network-scripts/ifcfg-'
	network_cfg='/etc/sysconfig/network'

	[ -z "$em" ] && em=0

	if [ $em -eq 1 ]; then
		interface=`ifconfig -a -s | awk '/^em/ { print $1 }'`
	else
		interface=`ifconfig -a -s | awk '/^eth/ { print $1 }'`
	fi

	for i in $interface; do
		ifconfig $i | grep -c 'inet addr' > /dev/null 2>&1
		if [ $? -eq 0 ]; then
			eval `/sbin/ifconfig $i | grep 'inet addr' | sed \
				-e 's/inet addr:/ipaddr=/' \
				-e 's/Bcast:/broadcast=/' \
				-e 's/Mask:/netmask=/'`
			echo -e "DEVICE=$i\nONBOOT=yes\nBOOTPROTO=static\nTYPE=Ethernet\nIPADDR=$ipaddr\nNETMASK=$netmask\nBROADCAST=$broadcast" \
				> $interface_cfg$i

			# add Mac Address
			eval `/sbin/ifconfig $i | grep 'HWaddr ' | sed \
				-e 's/.*HWaddr /hwaddr=/'`
			echo -e "HWADDR=$hwaddr" >> $interface_cfg$i
		else
			eval `/sbin/ifconfig $i | grep 'HWaddr ' | sed \
				-e 's/.*HWaddr /hwaddr=/'`
			echo -e "DEVICE=$i\nONBOOT=no\nHWADDR=$hwaddr\nTYPE=Ethernet" \
				> $interface_cfg$i
		fi

		echo "NM_CONTROLLED=yes" >> $interface_cfg$i
	done

	gateway=`netstat -nr | awk '/UG/ { print $2 }'`
	hostname=`hostname`

	echo -e "NETWORKING=yes\nHOSTNAME=$hostname\nGATEWAY=$gateway" > $network_cfg

	if [ "${ctr_os}" = "centos" -a ${ctr_ver} -ge 4 ]; then
		echo -e "NETWORKING_IPV6=no\nNOZEROCONF=yes" >> $network_cfg
	fi
}

an_yumblock()
{
	local yumfile="/etc/yum.conf"
	local yumcent="/etc/yum.repos.d/CentOS-Base.repo"
	local isexclude=0

	an_print_title "Prevent install/upgrade 32bit package on yum.conf"

	grep "^[[:blank:]]*exclude[[:blank:]]*=" $yumfile >& /dev/null
	[ $? -eq 0 ] && isexclude=1 || isexclude=0

	cat $yumfile | sed 's/^$/@blank@/g' | {
		IFS="\r\n" read line
		[ -n "$line" ] && echo "$line"

		while [ -n "$line" ]
		do
			read line

			if [ $isexclude -eq 1 ]; then
				echo $line | grep "^exclude" >& /dev/null
				if [ $? -eq 0 ]; then
					echo $line | grep "\*.i\*86" >& /dev/null
					[ $? -eq 1 ] && line="$line *.i*86"
				fi
			else
				echo $line | grep "^plugins" >& /dev/null
				[ $? -eq 0 ] && excludes="exclude=*.i*86"
			fi

			[ "$line" = "@blank@" ] && echo && continue
			[ -n "$line" ] && echo "$line"
			[ -n "$excludes" ] && echo "$excludes" && excludes=
		done
	} > /tmp/yum.conf.block


	# No action on 32bit system
	uname -a | grep x86_64 >& /dev/null
	[ $? -eq 1 ] && rm -f /tmp/yum.conf.block

	if [ -f /tmp/yum.conf.block ]; then
		diffc=$(diff -urNp $yumfile /tmp/yum.conf.block 2> /dev/null)

		[ -n "$diffc" ] && \
			mv $yumfile $yumfile.rpmnew && \
			mv /tmp/yum.conf.block $yumfile
	fi

	an_print_title "Prevent httpd/php/mysql package on CentOS repository"
	isexclude=0
	local start=0

	cat $yumcent | sed 's/^$/@blank@/g' | {
		IFS="\r\n" read line
		[ -n "$line" ] && echo "$line"

		while [ -n "$line" ]
		do
			read line

			if [ $start -eq 0 ]; then
				echo $line | grep "^\[" >& /dev/null
				[ $? -eq 0 ] && start=1
			fi

			echo $line | grep "^exclude" >& /dev/null
			if [ $? -eq 0 ]; then
				for i in php pear httpd mysql
				do
					echo $line | grep "$i\*" >& /dev/null
					[ $? -ne 0 ] && line="$line $i*"
				done
				isexclude=1
			fi

			if [ "${line}" = "@blank@" ]; then
				[ $isexclude -eq 0 -a $start -eq 1 ] && \
					echo "exclude=php* pear* httpd* mysql*"
				isexclude=0
				echo
				continue
			fi
			[ -n "${line}" ] && echo "${line}"
		done
	} > /tmp/CentOS-Base.repo.block

	if [ -f /tmp/CentOS-Base.repo.block ]; then
		diffc=$(diff -urNp $yumcent /tmp/CentOS-Base.repo.block 2> /dev/null)

		[ -n "$diffc" ] && \
			mv $yumcent $yumcent.rpmnew && \
			mv /tmp/CentOS-Base.repo.block $yumcent
	fi
}

an_end()
{
	an_print_title "Complete Installation"
	curl -s http://mirror.oops.org/pub/AnNyung/2/inst/install-done &> /dev/null
}

#
# Local variables:
# tab-width: 4
# c-basic-offset: 4
# End:
# vim: set filetype=sh noet sw=4 ts=4 fdm=marker:
# vim<600: noet sw=4 ts=4:
#
