#!/bin/bash -e

# debirf module: xkiosk
# create an graphical kiosk that starts x and a browser on boot.
#
# Depends: network-dhcp module, install-runit module
#
# The debirf scripts were written by
# Jameson Rollins <jrollins@fifthhorseman.net>
# and
# Daniel Kahn Gillmor <dkg@fifthhorseman.net>.
#
# They are Copyright 2007, and are all released under the GPL,
# version 3 or later.

### download needed packages
# could maybe pick and choose xorg packages if hardware known
debirf_exec apt-get install --assume-yes xorg matchbox-window-manager iceweasel

### properly configure X
# allow any user to start X (not just from command line):
# ("debirf_exec dpkg-reconfigure x11-common")
cat <<EOF > "$DEBIRF_ROOT"/etc/X11/Xwrapper.config
allowed_users=anybody
nice_value=0
EOF

# make standard xorg.conf
cat <<EOF > "$DEBIRF_ROOT"/etc/X11/xorg.conf
Section "Files"
EndSection
Section "InputDevice"
	Identifier	"Generic Keyboard"
	Driver		"kbd"
	Option		"CoreKeyboard"
	Option		"XkbRules"	"xorg"
	Option		"XkbModel"	"pc104"
	Option		"XkbLayout"	"us"
EndSection
Section "InputDevice"
	Identifier	"Configured Mouse"
	Driver		"mouse"
	Option		"CorePointer"
	Option		"Device"		"/dev/input/mice"
	Option		"Protocol"		"ImPS/2"
	Option		"Emulate3Buttons"	"true"
EndSection
Section "Device"
	Identifier	"Generic Video Card"
	Driver		"vesa"
EndSection
Section "Monitor"
	Identifier "VGA"
EndSection
Section "Screen"
	Identifier	"Default Screen"
	Device		"Generic Video Card"
	Monitor		"VGA"
	DefaultDepth	24
        SubSection "Display"
                Modes           "1280x1024" "1024x768" "800x600"
        EndSubSection
EndSection
Section "ServerLayout"
	Identifier	"Default Layout"
	Screen		"Default Screen"
	InputDevice	"Generic Keyboard"
	InputDevice	"Configured Mouse"
EndSection
EOF

### add guest user
if ! debirf_exec getent passwd guest >/dev/null 2>/dev/null ; then
    debirf_exec adduser --disabled-password --gecos "Kiosk User,,," guest
fi

### configure runit service dir to start X session
RUNIT_DIR=/srv/xkiosk
RUNIT_DIR_DEBIRF="${DEBIRF_ROOT}/${RUNIT_DIR}"

mkdir -p "$RUNIT_DIR_DEBIRF"/{env,log/main}
debirf_exec chown guest:guest "$RUNIT_DIR"/log/main

echo guest > "$RUNIT_DIR_DEBIRF"/env/UID
echo guest > "$RUNIT_DIR_DEBIRF"/env/GID
echo /home/guest > "$RUNIT_DIR_DEBIRF"/env/HOME

# creat run script
cat <<'EOF' > "$RUNIT_DIR_DEBIRF"/run
#!/bin/sh
exec 2>&1

# only allow 5 attempts at starting up X each minute.  This gives
# debirf admins a chance to fix from the console if there's repeated
# failing invocations of the X server (see
# http://cmrg.fifthhorseman.net/ticket/74)

BUCKETDIR=./bucket
mkdir -p "$BUCKETDIR"
find "$BUCKETDIR" -type f '!' -cmin 1 -delete
if [ $(find "$BUCKETDIR" -type f | wc -l ) -ge 5 ]; then
    echo "5 or more retries in the last minute.  Waiting a minute..."
    sleep 60
fi
touch "$BUCKETDIR"/$(date +%s)

exec chpst -u guest:guest:audio:video -e env /usr/bin/startx
EOF
debirf_exec chmod a+x "$RUNIT_DIR"/run

# create finish script
cat <<'EOF' > "$RUNIT_DIR_DEBIRF"/finish
#!/bin/sh
rm -rf ~guest
cp -r /etc/skel ~guest
chown -R guest:guest ~guest
EOF
debirf_exec chmod a+x "$RUNIT_DIR"/finish

# create log run script
cat <<EOF > "$RUNIT_DIR_DEBIRF"/log/run
#!/bin/sh
exec chpst -u guest svlogd -tt ./main
EOF
debirf_exec chmod a+x "$RUNIT_DIR"/log/run

# link in service dir
debirf_exec update-service --add "$RUNIT_DIR"

### make system xinitrc
cat <<EOF > "$DEBIRF_ROOT"/etc/X11/xinit/xinitrc
#!/bin/sh
xsetroot -solid blue
matchbox-window-manager &
TARG=file://$RUNIT_DIR/index.html
if (grep -q 'browse=' </proc/cmdline) ; then
  TARG=\$( sed 's/.*browse=\([^[:space:]]*\).*/\1/' </proc/cmdline)
fi
iceweasel \$TARG
EOF

# set permissions on /home/guest, just in case
debirf_exec chown -R guest:guest /home/guest

### make start page
cat <<EOF > "$RUNIT_DIR_DEBIRF"/index.html
<html>
<head>
<title>debirf - xkiosk</title>
</head>
<body>
<h1>Welcome to <a href="http://cmrg.fifthhorseman.net/wiki/debirf">debirf</a> xkiosk.</h1>
<p>To restart the session and purge all session history, just quit the browser by doing one of the following:
<ul>
  <li>Select "Quit" from the "File" menu.
  <li>Close all open windows by clicking the <span style="font-family: monospace;"><b>x</b></span> in the upper right corner.
</ul>
</body>
</html>
EOF
