#!/bin/sh -e

##########################################################################
#   Title:
#       Optional, defaults to the name of the script sans extention
#
#   Section:
#       8
#
#   Synopsis:
#       
#   Description:
#       Set up linux_base on a FreeBSD system
#       
#   Arguments:
#       
#   Returns:
#
#   Examples:
#
#   Files:
#
#   Environment:
#
#   See also:
#       
#   History:
#   Date        Name        Modification
#   2018-08-27  J Bacon     Begin
#   2024-11-12  Jason Bacon Add man page template and usage
##########################################################################

usage()
{
    printf "Usage: $0\n"
    exit 1
}


##########################################################################
#   Main
##########################################################################

case $# in
0)
    ;;

*)
    usage
    ;;

esac

export AUTO_ASK_TAG_PREFIX=auto-install-linux_base
    
case $(auto-ostype) in
FreeBSD)
    # FIXME: Support $PORTSDIR
    MAKE_CONF=/etc/make.conf
    centos_version=7
    major=`uname -r | cut -d '.' -f 1`
    # Let flash port install the default linux_base as a depend
    
    # Install linux_base.  Can use 64-bit for 10.3 and later.
    linux_base=c$centos_version
    major=`uname -r | cut -d . -f 1`
    minor=`uname -r | cut -d . -f 2 | cut -d - -f 1`
    if [ `uname -m` = amd64 ]; then
	linux64=y
    else
	linux64=n
    fi
    pkg delete -f -y linux_base\* || true
    if [ $linux64 = y ]; then
	#auto-append-line OVERRIDE_LINUX_BASE_PORT=c${centos_version}_64 $MAKE_CONF $0
	#auto-append-line OVERRIDE_LINUX_NONBASE_PORTS=c${centos_version}_64 $MAKE_CONF $0
	#auto-append-line DEFAULT_VERSIONS+=linux=c${centos_version}_64 $MAKE_CONF $0
	kldload linux || true
	kldload linux64 || true
    else
	touch $MAKE_CONF    # Prevent no such file error below
	#sed -i '.bak' -e 's|OVERRIDE_LINUX_BASE_PORT=c${centos_version}_64||' $MAKE_CONF
	#sed -i '.bak' -e 's|OVERRIDE_LINUX_NONBASE_PORTS=c${centos_version}_64||' $MAKE_CONF
	#sed -i '.bak' -e '/DEFAULT_VERSIONS+=linux=c${centos_version}_64/d' $MAKE_CONF
	kldload linux || true
    fi
    pkg install -y linux_base-$linux_base
    
    auto-append-line 'linux_enable="YES"' /etc/rc.conf $0
    
    auto-append-line linprocfs "linprocfs\t/compat/linux/proc\tlinprocfs\trw\t0\t0" /etc/fstab $0
    if ! df | fgrep -q /compat/linux/proc; then
	mount /compat/linux/proc
    fi
    
    auto-append-line /compat/linux/dev/shm "tmpfs\t\t/compat/linux/dev/shm\ttmpfs\trw,mode=1777\t0\t0" /etc/fstab $0
    if ! df | fgrep /compat/linux/dev/shm; then
	mount /compat/linux/dev/shm
    fi
    
    if [ $linux_base = c7 ]; then
	auto-append-line linsysfs "linsysfs\t/compat/linux/sys\tlinsysfs\trw\t0\t0" /etc/fstab $0
	if ! df | fgrep -q /compat/linux/sys; then
	    mount /compat/linux/sys
	fi
    fi
    ;;
    
*)
    auto-unsupported-os $0
    exit 1
    ;;

esac
