#!/bin/sh

PROG="${0##*/}" #program name

print_version()
{
	cat <<EOF
$PROG version @VERSION@

Written by Anton Farygin

Copyright (C) 2005 ALT Linux Ltd.
EOF
	exit
}


print_usage()
{
	[ "$1" = 0 ] || exec >&2
	cat <<EOF
Usage: $PROG [options] 

alterator backend to change inittab

Valid options are:
  -h, --help	display help screen
  -v, --version	display version information
  -l, --list	get list
  -t, --type	check for  existance
  -d, --delete	(empty action, required for backend)
  -r, --read	read information
  -w, --write	(empty action, required for backend)

Report bugs to <inger@altlinux.org>
EOF
	[ -n "$1" ] && exit "$1" || exit
}

INITTAB=/etc/inittab


TEMP=`getopt -n $PROG -o h,v,l:,d:,r:,w:,t: -l help,version,list:,delete:,read:,write:,type: -- "$@"` || print_usage
eval set -- "$TEMP"

while :; do
	case "$1" in
		-h|--help) print_usage 0
			;;
		-v|--version) print_version
			;;
		-l|--list)
			shift;dir="$1";
			if [ "$dir" == "/" ] ;then
				echo "inittab r"
				echo "firsttime r"
			fi
			;;
		-d|--delete)
			#ignore
			shift;drv="$1";
			;;
		-t|--type)
			shift;drv="$1";
			if [ "$drv" == "inittab" ];then
				echo "inittab r"
			elif [ "$drv" == "firsttime" ];then
				echo "firsttime r"
			fi
			;;
		-r|--read)
			shift;drv="$1";
			if [ "$drv" == "inittab" ];then
			    runlevel=`fgrep "initdefault" $INITTAB 2>/dev/null|grep -v "^#"|cut -f2 -d':'`
			    echo "runlevel:$runlevel"
			elif [ "$drv" == "firsttime" ];then
				echo -n "firsttime:"
				if [ -f /var/lock/TMP_1ST ]; then 
				    echo "true"
				else
				    echo "false"
				fi
			fi
			;;
		-w|--write)
			#ignore
			shift;drv="$1";
			if [ "$drv" = "inittab" ];then
			    while read l; do
					option=${l%%:*}
                                        value=$(echo "${l#*:}")
					[ -n "$value" ] &&
					case "$option" in
						runlevel)
							subst "s,^\(id:\)\(.*\)\(:initdefault.*\),\\1$value\\3," $INITTAB 2>/dev/null
							;;
						*)
							;;
					esac
			    done
			fi
			;;
		--) shift; break
			;;
		*) Fatal "unrecognized option: $1"
			;;
	esac
	shift
done
