# functions for AnNyung LInux bootstrap

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

#
# Backend API
#

an_osver()
{
	OSVER=$(cat /etc/redhat-release | sed 's/[^0-9.]//g')
	OSVER_MAJOR=$(cat /etc/redhat-release | sed 's/[^0-9.]//g' | head -c 1)
	OSVER_MINOR=$(cat /etc/redhat-release | sed 's/[^0-9.]//g' | cut -d "." -f 2)
}

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

an_print_msg()
{
	echo " * $1"
	[[ -z $2 ]] && 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_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_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_pwsort()
{
	mode=$1; shift

	case "$mode" in
		passwd)
			/usr/sbin/pwck -s
			;;
		*)
			/usr/sbin/grpck -s
	esac
}

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
}

#
# Frontend API
#

an_release()
{
	an_print_title "Installation AnNyung LInux release package"

	rpm -q annyung-release &> /dev/null
	CHK=$?

	rpm -q perl &> /dev/null

	(( CHK != 0 )) &&
		rpm -Uhv http://mirror.oops.org/pub/AnNyung/3/inst/annyung-release.rpm ||
		/sbin/annyung-init-banner
}

an_setlang()
{
	an_print_title "Language Setting to locale.conf"
	lang=$1
	[[ -z $lang ]] && lang="ko_KR.UTF-8"
	echo "LANG=\"$lang\"" > /etc/locale.conf
	an_print_msg "Set to $lang"
}

an_x86_64_fix()
{
	uname -a | grep x86_64 >& /dev/null
	# if not x86_64 system, skip
	(( $? == 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
	echo
}

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

an_remfirmware()
{
	an_print_title "Removing kernel firmware"
	firmwares="$(rpm -qa --qf '%{name}\n' | grep -- "-firmware" 2> /dev/null)"
	[[ -n $firmwares && $firmwares != "linux-firmware" ]] && yum -y remove $firmwares 2> /dev/null

	#
	# yum -C -y remove linux-firmware
	#
	echo
	echo
}

an_nozeroconf()
{
	an_print_title "Setting NO ZEROCONF configuration"
	cat /etc/sysconfig/network | grep NOZEROCONF >& /dev/null
	(( $? != 0 )) &&
		echo "NOZEROCONF=yes" >> /etc/sysconfig/network
	echo
	echo
}

an_ipv6_off()
{
	an_print_title "Disabling IPv6"

	echo "install ipv6 /bin/true" > /etc/modprobe.d/blacklist-ipv6.conf
	echo "NETWORKING_IPV6=no" >> /etc/sysconfig/network
	echo "IPV6INIT=no" >> /etc/sysconfig/network

	[[ ! -f /etc/sysctl.d/61-disable-ipv6.conf ]] &&
		echo 'net.ipv6.conf.all.disable_ipv6 = 1' > /etc/sysctl.d/61-disable-ipv6.conf
	sysctl -w 'net.ipv6.conf.all.disable_ipv6=1' >& /dev/null

	echo
	echo
}

an_selinux_off()
{
	an_print_title "Disabling selinux"
	cat /etc/selinux/config | sed 's/SELINUX=.*/SELINUX=disabled/g' > /tmp/selinux_off.txt
	mv -f /tmp/selinux_off.txt /etc/selinux/config
	setenforce 0 >& /dev/null
}

an_service()
{
	an_print_title "Turn On/Off Init Service"
	flag=$1; shift 1
	for service in $*
	do
		if [ -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
		else
			if [[ $flag == "on" ]]; then
				systemctl enable $service >& /dev/null
				an_print_msg "Turn on $service" noecho
			else
				systemctl disable $service >& /dev/null
				an_print_msg "Turn off $service" noecho
			fi
		fi
	done
	echo
}

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_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 conflict packagees 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 mariadb 'bind'
				do
					echo $i | grep "/$" >& /dev/null
					[ $? -ne 0 ] && checks="${i}\*" || checks=$(echo $i | sed 's!/$!!g')
					iadd=$(echo $checks | sed 's!\\!!g')
					checks="$checks\\( \\|$\\)"

					echo $line | grep "$checks" >& /dev/null
					[ $? -ne 0 ] && line="$line $iadd"
				done
				isexclude=1
			fi

			if [ "${line}" = "@blank@" ]; then
				[ $isexclude -eq 0 -a $start -eq 1 ] && \
					echo "exclude=php* pear* httpd* mysql* mariadb* bind*"
				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_yumcron ()
{
	/usr/bin/perl -pi -e 's/^(update_messages\s+=\s+).*/$1yes/g' /etc/yum/yum-cron.conf
	/usr/bin/perl -pi -e 's/^(download_updates\s+=\s+).*/$1yes/g' /etc/yum/yum-cron.conf
	/usr/bin/perl -pi -e 's/^(apply_updates\s+=\s+).*/$1yes/g' /etc/yum/yum-cron.conf
	/usr/bin/perl -pi -e 's/^(update_messages\s+=\s+).*/$1yes/g' /etc/yum/yum-cron-hourly.conf
}

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

	interface=`ifconfig -a -s | awk '/^(em|eth|enp)[0-9]/ {print $1}'`

	for i in $interface; do
		ifconfig $i | grep -c 'inet ' > /dev/null 2>&1
		if [ $? -eq 0 ]; then
			uuid="$(uuidgen $i)"
			eval `/sbin/ifconfig $i | grep 'inet ' | sed \
				-e 's/inet /ipaddr=/' \
				-e 's/broadcast /broadcast=/' \
				-e 's/netmask /netmask=/'`
			echo -e "NAME=$i\nDEVICE=$i\nONBOOT=yes\nNETBOOT=yes\nUUID=${uuid}\nBOOTPROTO=none\nTYPE=Ethernet\nIPADDR=$ipaddr\nNETMASK=$netmask\nBROADCAST=$broadcast\nDEFROUTE=yes\nIPV4_FAILURE_FATAL=no" \
				> $interface_cfg$i

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

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

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

	echo "${hostname}" > /etc/hostname
	#echo -e "NETWORKING=yes\nHOSTNAME=$hostname\nGATEWAY=$gateway" > $network_cfg
	echo -e "GATEWAY=$gateway" > $network_cfg

	if [ -f "/etc/centos-release" ]; then
		#ctr_ver=`cat /etc/centos-release | awk '{print $4}' | sed 's/\..\+//g'`
		#[ -z "$ctr_ver" ] && ctr_ver=0

		cat $network_cfg | grep "NOZEROCONF" >& /dev/null
		[ $? -ne 0 ] && echo "NOZEROCONF=yes" >> $network_cfg
		cat $network_cfg | grep "NETWORKING_IPV6" >& /dev/null
		[ $? -ne 0 ] && echo "NETWORKING_IPV6=no" >> $network_cfg
		cat $network_cfg | grep "IPV6INIT" >& /dev/null
		[ $? -ne 0 ] && echo "IPV6INIT=no" >> $network_cfg
	fi
}

an_end()
{
	an_print_title "Complete Installation"
	curl -s http://mirror.oops.org/pub/AnNyung/3/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:
#
