#! /bin/sh -e

# Naive shell script to translate modules.conf + defaults to modprobe.conf
# Normal caveats apply, as with any machine translation.
if [ $# -gt 2 ] || [ x"$1" = x--help ]; then
    echo "Usage: $0 [--stdin] [modprobe.conf]"
    echo "   Converts your current modules setup to modprobe.conf."
    echo "   Don't trust it too much."
    echo " Version 0.1"
    exit 1
fi

STDIN=
if [ "$1" = "--stdin" ]; then
  STDIN=1
  shift
fi

# Set up output if specified.
if [ $# -eq 1 ]; then exec > $1; fi

MODPROBECONF=`mktemp /tmp/modprobe.XXXXXX || tempfile || echo /tmp/modprobe.$$`
trap "rm -f $MODPROBECONF" 0

if [ x"$TESTING_MODPROBE_CONF" != x ]; then
    cp $TESTING_MODPROBE_CONF $MODPROBECONF
elif [ "$STDIN" = "1" ]; then
    cat > $MODPROBECONF
elif [ -x /sbin/modprobe.24 ]; then
    # In sbin.
    /sbin/modprobe.24 -c > $MODPROBECONF
elif modprobe.24 -c >/dev/null 2>&1; then
    # Somewhere in path.
    modprobe.24 -c > $MODPROBECONF
elif /sbin/modprobe -V 2>/dev/null | grep -q 'modprobe version'; then
    # Running /sbin/modprobe gives old version.
    /sbin/modprobe -c > $MODPROBECONF
elif modprobe -V 2>/dev/null | grep -q 'modprobe version'; then
    # Running modprobe gives old version.
    modprobe -c > $MODPROBECONF
else
    echo Cannot find old version of modprobe.  Giving up. 2>&1
    exit 1
fi

# Resolve (possibly recursive) alias: args filename alias
resolve_alias()
{
    RA_RESOLVE=`grep "^alias[ 	][ 	]*$2[ 	]" -- $1 | awk '{ print $3 }'`
    if [ x"$RA_RESOLVE" = x ]; then
	echo $2
	return
    fi
    # Recurse.
    (resolve_alias $1 "$RA_RESOLVE")
}

# Parse alias: args filename modulename aliasto.
parse_alias()
{
    PA_ALIAS=`resolve_alias $1 $3`
    NAME=`echo $2|sed -e 's/\(block\|char\)-major-\([0-9]\+\)$/\1-major-\2-*/'`

    echo "alias $NAME $PA_ALIAS"
}

# Parse options: args modulename aliasto.
parse_options()
{
    # Second arg can be null if grab_noninstall options returns empty.
    [ x"$2" = x ] || echo "options $1 $2"
}

# Separate out the modprobe options from the module options
grab_install_options()
{
    GRAB_OPTS=""
    GRAB_NEXT=0
    for grab_i; do
	case "$grab_i" in
	-o) GRAB_OPTS="$GRAB_OPTS -o"; GRAB_NEXT=1;;
	-*) echo Warning: Ignoring unknown alias option "$grab_i" >&2;;
	*)
	    [ $GRAB_NEXT = 0 ] || GRAB_OPTS="$GRAB_OPTS $grab_i"
	    GRAB_NEXT=0
	    ;;
	esac
    done
    echo "$GRAB_OPTS"
}

grab_noninstall_options()
{
    GRAB_OPTS=""
    GRAB_NEXT=0
    for grab_i; do
	case "$grab_i" in
	-o) GRAB_NEXT=1;;
	-*) ;;
	*)
	    [ $GRAB_NEXT = 1 ] || GRAB_OPTS="$GRAB_OPTS $grab_i"
	    GRAB_NEXT=0
	    ;;
	esac
    done
    echo "$GRAB_OPTS"
}

# Produce a multiple install/remove commands. Args: list, command, separator.
multi()
{
    mp_res=""
    for mp_arg in $1; do
	if [ x"$mp_res" = x ]; then mp_res="/sbin/$2 $mp_arg";
	else mp_res="$mp_res${3}/sbin/$2 $mp_arg";
	fi
    done
    echo "$mp_res"
}

# Install and remove commands frequently refer to insmod and rmmod.
# Use "modprobe --ignore-install " and "modprobe -r --ignore-remove" since 
# our versions of rmmod and insmod are deliberately stupid.
convert_command()
{
    echo "$@" | sed -e 's,insmod,/sbin/modprobe --ignore-install,' -e 's,rmmod,/sbin/modprobe -r --ignore-remove,'
}

while read ADD NAME MODULE REST; do
    # Ignore a spurious add, otherwise move them down.
    if [ x"$ADD" = x"add" ]; then :; else
	REST="`echo $MODULE $REST | sed 's/ *$//'`"
	MODULE="$NAME"
	NAME="$ADD";
    fi
    #echo "NAME=$NAME MODULE=$MODULE REST=$REST" >&2

    # MODNAME is the converted name for use in variables.
    MODNAME=`echo $MODULE | tr -c A-Za-z_0-9 _`
    case "$NAME" in
    '#'*) : ;; # Comment 
    '') : ;;   # Blank line

    keep) # Do nothing.
	;;

    alias)
	if [ x"$REST" = xoff ] || [ x"$REST" = xnull ]; then
	    INSTALL_COMMANDS="$INSTALL_COMMANDS $MODULE"
	    eval install_$MODNAME=\"/bin/true\"
	else
	    parse_alias $MODPROBECONF "$MODULE" $REST
	fi
	;;

    options) 
	# Options used to be able to contain extra args to insmod
	# (eg. -o foo).  Not any more.
	case " $REST" in
	*' '-[-a-z]*)
	    parse_options $MODULE `grab_noninstall_options $REST`
	    INSTALL_OPTIONS=`grab_install_options $REST`
	    INSTALL_COMMANDS="$INSTALL_COMMANDS $MODULE"
	    eval install_$MODNAME=\"/sbin/modprobe $INSTALL_OPTIONS --ignore-install `resolve_alias $MODPROBECONF $MODULE`\"
	    ;;
	*)
	    parse_options $MODULE "$REST"
	    ;;
	esac
	;;

    above)
	# Pull in these modules after me.
	INSTALL_COMMANDS="$INSTALL_COMMANDS $MODULE"
	eval post_install_$MODNAME=\"`multi "$REST" 'modprobe' '; '`\; /bin/true\"
	REMOVE_COMMANDS="$REMOVE_COMMANDS $MODULE"
	eval pre_remove_$MODNAME=\"`multi "$REST" 'modprobe -r' '; '`\"
	;;
    below)
	# Pull in these modules before me.
	INSTALL_COMMANDS="$INSTALL_COMMANDS $MODULE"
	eval pre_install_$MODNAME=\"`multi "$REST" 'modprobe' '; '`\"
	REMOVE_COMMANDS="$REMOVE_COMMANDS $MODULE"
	eval post_remove_$MODNAME=\"`multi "$REST" 'modprobe -r' '; '`\; /bin/true\"
	;;
    probe) 
	INSTALL_COMMANDS="$INSTALL_COMMANDS $MODULE"
	eval install_$MODNAME=\"`multi "$REST" 'modprobe' ' || '`\"
	;;
    probeall)
	# Not quite correct.  If they all fail, we should fail.
	INSTALL_COMMANDS="$INSTALL_COMMANDS $MODULE"
	eval install_$MODNAME=\"`multi "$REST" 'modprobe' '; '`\; /bin/true\"
	;;
    post-install)
	INSTALL_COMMANDS="$INSTALL_COMMANDS $MODULE"
	eval post_install_$MODNAME=\"`convert_command "$REST"`\"
	;;
    pre-install)
	INSTALL_COMMANDS="$INSTALL_COMMANDS $MODULE"
	eval pre_install_$MODNAME=\"`convert_command "$REST"`\"
	;;
    install)
	INSTALL_COMMANDS="$INSTALL_COMMANDS $MODULE"
	eval install_$MODNAME=\"`convert_command "$REST"`\"
	;;
    post-remove)
	REMOVE_COMMANDS="$REMOVE_COMMANDS $MODULE"
	eval post_remove_$MODNAME=\"`convert_command "$REST"`\"
	;;
    pre-remove)
	REMOVE_COMMANDS="$REMOVE_COMMANDS $MODULE"
	eval pre_remove_$MODNAME=\"`convert_command "$REST"`\"
	;;
    remove)
	REMOVE_COMMANDS="$REMOVE_COMMANDS $MODULE"
	eval remove_$MODNAME=\"`convert_command "$REST"`\"
	;;
    include)
	# Recurse.
	($0 $MODULE)
	;;
    # Ignore default lines which are not required any more.
    "path[boot]=/lib/modules/boot"|"path[toplevel]=/lib/modules/`uname -r`"|"path[toplevel]=/lib/modules/2.4"|"path[kernel]=/lib/modules/kernel"|"path[fs]=/lib/modules/fs"|"path[net]=/lib/modules/net"|"path[scsi]=/lib/modules/scsi"|"path[block]=/lib/modules/block"|"path[cdrom]=/lib/modules/cdrom"|"path[ipv4]=/lib/modules/ipv4"|"path[ipv6]=/lib/modules/ipv6"|"path[sound]=/lib/modules/sound"|"path[fc4]=/lib/modules/fc4"|"path[video]=/lib/modules/video"|"path[misc]=/lib/modules/misc"|"path[pcmcia]=/lib/modules/pcmcia"|"path[atm]=/lib/modules/atm"|"path[usb]=/lib/modules/usb"|"path[ide]=/lib/modules/ide"|"path[ieee1394]=/lib/modules/ieee1394"|"path[mtd]=/lib/modules/mtd"|"generic_stringfile=/lib/modules/`uname -r`/modules.generic_string"|"pcimapfile=/lib/modules/`uname -r`/modules.pcimap"|"isapnpmapfile=/lib/modules/`uname -r`/modules.isapnpmap"|"usbmapfile=/lib/modules/`uname -r`/modules.usbmap"|"parportmapfile=/lib/modules/`uname -r`/modules.parportmap"|"ieee1394mapfile=/lib/modules/`uname -r`/modules.ieee1394map"|"pnpbiosmapfile=/lib/modules/`uname -r`/modules.pnpbiosmap"|"depfile=/lib/modules/`uname -r`/modules.dep"|"persistdir=/var/lib/modules/persist")
	;;

    # Ignore prune lines unless they end in .o or .ko, which would
    # have an effect.
    prune)
	case "$MODULE" in
	*.ko|*.o)
	    echo "Warning: not translating $NAME $MODULE $REST" >&2
	    ;;
	esac
	;;
    *)
	echo "Warning: not translating $NAME $MODULE $REST" >&2
	;;
    esac
done < $MODPROBECONF

for mod in `echo $INSTALL_COMMANDS | tr ' ' '\n' | sort -u`; do
    # MODNAME is the converted name for use in variables.
    MODNAME=`echo $mod | tr -c A-Za-z_0-9 _`
    eval PREINSTALL=\"\$pre_install_$MODNAME\"
    eval POSTINSTALL=\"\$post_install_$MODNAME\"
    eval INSTALL=\"\$\{install_$MODNAME:-/sbin/modprobe --first-time --ignore-install $mod}\"
    if [ -n "$PREINSTALL" ]; then PREINSTALL="{ $PREINSTALL; } ; "; fi
    if [ -n "$POSTINSTALL" ]; then POSTINSTALL="&& { $POSTINSTALL; }"; fi
    echo install $mod $PREINSTALL $INSTALL $POSTINSTALL
done

for mod in `echo $REMOVE_COMMANDS | tr ' ' '\n' | sort -u`; do
    # MODNAME is the converted name for use in variables.
    MODNAME=`echo $mod | tr -c A-Za-z_0-9 _`
    eval PREREMOVE=\"\$pre_remove_$MODNAME\"
    eval POSTREMOVE=\"\$post_remove_$MODNAME\"
    eval REMOVE=\"\$\{remove_$MODNAME:-/sbin/modprobe -r --first-time --ignore-remove $mod}\"
    if [ -n "$PREREMOVE" ]; then PREREMOVE="{ $PREREMOVE; } ; "; fi
    if [ -n "$POSTREMOVE" ]; then POSTREMOVE="&& { $POSTREMOVE; }"; fi
    echo remove $mod $PREREMOVE $REMOVE $POSTREMOVE
done
