#!/bin/sh

# $Id: teclogin_install.sh,v 1.1 2004/08/11 10:43:30 karsten Exp $
# Karsten Kruse - www.tecneeq.de
#
# TODO: make sure default init-level is 3
#
# teclogin_install.sh - Install teclogin for a particular user
#
#  Copyright (c) 2004, Karsten Kruse tecneeq(at)tecneeq(dot)de
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following  disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in
#     the documentation and/or other materials provided with the
#     distribution.
#  3. Neither the name of the author nor the names of its contributors
#     may be used to endorse or promote products derived from this
#     software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# even my scripts have to die sometimes
die() {
    echo
    echo ERROR: "$1"
    echo
    echo Usage as root: $0 existing_username
    echo
    exit 1
}

# do we have programs we need?
for i in type id grep cat gcc strip mkdir cp chmod egrep sed rm ; do
    type $i 2>&1 >/dev/null || die "Could not find $i installed"
done

# are we root?
if [ ! `id -u` = 0 ] ; then
    die "You have to be root to install teclogin"
fi

# check commandline
if [ -z $1 ] ; then
    die "Username missing!"
fi

# does the user exist on this box?
grep "^${1}:" /etc/passwd >/dev/null
if [ $? = 1 ] ; then
    die "User $1 is not in /etc/passwd"
fi

echo " Create /tmp/teclogin_${1}.c"
cat <<EOF > /tmp/teclogin_${1}.c

/*
 *  teclogin_${1}.c
 *  Karsten Kruse 2002 - tecneeq(at)tecneeq(at)de
 *  This code is in the Public Domain
 *
 */

#include <unistd.h>

int main() {
  execlp( "login", "login", "-f", "${1}", 0);
  return 0;
}

EOF

echo " Compile and strip /tmp/teclogin_${1}.c"
gcc -Os -Wall -static -o /tmp/teclogin_${1} /tmp/teclogin_${1}.c || \
    die "Could not compile /tmp/teclogin_${1}"
strip /tmp/teclogin_${1} || \
    die "Could not strip /tmp/teclogin_${1}"

echo " Install to /usr/local/sbin/teclogin_${1}"
mkdir -p /usr/local/sbin || \
    die "Could not create dir /usr/local/sbin"
cp /tmp/teclogin_${1} /usr/local/sbin || \
    die "Could not copy /tmp/teclogin_${1}  to /usr/local/sbin"
chmod +x /usr/local/sbin/teclogin_${1} || \
    die "Could not chmod +x /usr/local/sbin/teclogin_${1}"

# cleanup
rm /tmp/teclogin_${1} /tmp/teclogin_${1}.c

# suggest a line for /etc/inittab
old_line=$(egrep "[^1|^c1].*:respawn:.*getty .*tty1" /etc/inittab)
new_line=$(echo $old_line | sed 's/getty /getty \-n \-l \/usr\/local\/sbin\/teclogin_'$1' /')

cat <<THEEND

 To finish the installation you have to edit a line in /etc/inittab.
 Find this in /etc/inittab:

$old_line

 Change it to that (must be one line):

$new_line

 Then save the changes and reboot. Good luck.

THEEND


# eof

