#!/bin/sh

. /usr/local/etc/udx.d/static/common.sh

log "running fix failed upgrade ..."

ud_function_exists()
{
        x=$(type "$1" 2>/dev/null | grep "function")
        if [ -z "$x" ]
        then
                false
                return
        fi
        true
}

#$1 path to rc.conf
check_rc_conf()
{
	if [ ! -f $1 ] 
	then
		log "rc.conf does not exist ..."
		false
		return
	fi

	mbu=$(grep "###MAINTAINED BY UDX####" "$1")
	if [ -z "$mbu" ]
	then
		log "rc.conf does not have modified by udx ..."
		false
		return
	fi

	dnm=$(grep "###DO NOT MODIFY###" "$1")
	if [ -z "$dnm" ]
	then
		log "rc.conf does not have do not modify ..."
		false
		return
	fi

	log "$1 is AOK"
	true
	return
}


fix_rc_conf()
{
	if ud_function_exists "rc_conf_sanity_check";
	then
		log "rc conf sanity check already done by udx ..."
		return
	fi

	if ! check_rc_conf "/etc/rc.conf"; then
		log "recreating rc.conf from pkg site ..."
		curl -s https://pkg.isy.io/script/eisy.rc.conf > /tmp/eisy.rc.conf
		if check_rc_conf "/tmp/eisy.rc.conf"; then
			cat /tmp/eisy.rc.conf > /etc/rc.conf 
			chown root:wheel /etc/rc.conf
			chmod 644 /etc/rc.conf
			log "updating rc.conf successful ... rebooting"
			reboot
		fi
		log "error ... failing updating /etc/rc.conf" 	 
	fi

}

cloud_os_pkgs_sanity_check()
{
        upgraded_os="no"
        log "sanity checking os packages  ..."
        if [ ! -f /usr/bin/sort ]
        then
                log "missing /usr/bin/sort: upgrade issues need to be fixed ..."
                if is_polisy;
                then
                        log "reinstalling FreeBSD-kernel-polisy ..."
                        pkg install -yf FreeBSD-kernel-polisy 2>&1 > /dev/null
                        if [ $? -eq 0 ]
                        then
                                upgraded_os="yes"
                        else
                                log "error installing FreeBSD-kernel-polisy ..."
                        fi
                elif is_eisy;
                then
                        log "reinstalling FreeBSD-kernel-eisy ..."
                        pkg install -yf FreeBSD-kernel-eisy 2>&1 > /dev/null
                        if [ $? -eq 0 ]
                        then
                                upgraded_os="yes"
                        else
                                log "error installing FreeBSD-kernel-polisy ..."
                        fi
                else
                        log "invalid product ..."
                fi

                if [ "$upgraded_os" = "yes" ]
                then
                        pkg install -yf FreeBSD-runtime
                        if [ $? -eq 0 ]
                        then
                                echo "clean.me" > $UDX_INSTALL_CLEANUP_FILE
                        else
                                log "error installing FreeBSD-runtime ..."
                        fi
                fi
        fi
}

was_recently_rebooted()
{
        uptime=$(ud_get_uptime)
        if is_polisy;
        then
                if [ $uptime -lt 150 ]
                then
                        true
                        return
                fi
        else
                if [ $uptime -lt 90 ]
                then
                        true
                        return
                fi
        fi
        false
}

install_openssl()
{
	if is_fbsd_14; then
		service sshd status > /dev/null 2>&1
		if [ $? -ne 0 ]
		then
			log "installing freebsd openssl "
			pkg-static -o IGNORE_OSVERSION=yes install -yf FreeBSD-openssl-lib
			pkg-static -o IGNORE_OSVERSION=yes install -yf sudo
		fi
	fi
} 

do_common()
{
	pkg info udx > /dev/null 2>&1
	if [ $? -ne 0 ]
	then
		pkg install -yf udx
	fi
	rm -f /var/udx/.udx_install_cleanup 
	service udx_cmd_processor stop
	rm -f /var/udx/.udx_cmd_fifo
	install_openssl

	chsh -s /usr/local/bin/bash root 
	/usr/sbin/pwd_mkdb -p /etc/master.passwd 
	
	chown -R isy:polyglot /var/isy

	if was_recently_rebooted; then
                echo " removing udx_install_cleanup ..."
       		rm /var/udx/.udx_install_cleanup
       		rm /var/udx/.upgrade_status
	fi
}

do_fix_polisy()
{
#        if [ -f /usr/local/etc/devd/udx_net.conf ]
#        then
#                echo " removing udx_net.conf "
#                rm -f /usr/local/etc/devd/udx_net.conf
#        fi

	pkg info pg3x > /dev/null 2>&1
	if [ $? -ne 0 ]
	then
		echo "installing pg3x ..."
		#pkg-static -o IGNORE_OSVERSION=yes install -f pg3x
	fi

	sysrc pg3x_on_polisy="YES"
}

do_fix_eisy()
{
	pkg info dhcpcd > /dev/null 2>&1
	if [ $? -ne 0 ]
	then
		echo "installing dhcpcd ..."
		pkg-static -o IGNORE_OSVERSION=yes install -f dhcpcd
	fi
}

if is_polisy; then
	do_fix_polisy
fi
if is_eisy; then
	do_fix_eisy
fi

do_common
log "fix done ... rebooting ..."


shutdown -r now
