#!/bin/bash
# $Id: usbauth,v 1.1 2003/09/27 01:48:26 mac Exp $
#
# Copyleft Masaki Shinomiya <shino@pos.to>
# Modified by mac@research.co.jp
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

USER=""
FNAME=`cat /etc/usbauth.conf`
export FNAME
USER=`/usr/local/sbin/uauth -d $FNAME`
if [ x$USER == x ];
	then echo Auth Error $USER	> /dev/console
	exit -1
fi

/usr/bin/clear				>  /dev/console
echo Hello $USER			>  /dev/console
echo Welcom to `hostname`		>  /dev/console

DEVS="0:0"
for LOCKDIR in /var/run /var/lock;do
    [ -d $LOCKDIR/console ] && break
done
echo -n 1 > $LOCKDIR/console/$USER
echo -n $USER > $LOCKDIR/console.lock
if [ -x /sbin/pam_console_apply ] ;then /sbin/pam_console_apply
else
    cd /dev
    for i in $DEVS ;do 
        chown $USER $i
    done
fi
( exec su - $USER -c /usr/X11R6/bin/startx &> /tmp/x.log )
rm -f $LOCKDIR/console.lock
rm -fR $LOCKDIR/console/*
if [ -x /sbin/pam_console_apply ] ;then /sbin/pam_console_apply -r
else 
    cd /dev
    for i in $DEVS ;do
        chown root $i
    done
fi
exit 0
