#!/bin/bash
# Works through each user and builds component parts for a Minerva-compliant
# web interface

# Call this after adding users to the Minerva user list.

ETCDIR=/usr/local/minerva/etc/users
TARGET_FOLDER=/var/www/minerva/warp/conf/tvguide
TARGET=$TARGET_FOLDER/users.conf

mkdir -p $TARGET_FOLDER

# The users
echo \<?php >$TARGET

for user in `ls $ETCDIR`; do
if [ -f $ETCDIR/$user/tvsearch/list ]; then
   echo "require_once 'warp/conf/tvguide/users/$user.conf';" >> $TARGET
fi
done

echo "class Warp_TVGuide_Users {" >> $TARGET
echo function getUsers\(\) {  >> $TARGET
echo   return array\(  >> $TARGET

for user in `ls $ETCDIR`; do
 
 if [ -f $ETCDIR/$user/name ]
 then
    echo \"$user\" =\> \"`cat $ETCDIR/$user/name`\", >>$TARGET
 fi;

done
echo ");}" >> $TARGET

echo "function getUserGuide(\$idx) {" >> $TARGET
echo " switch(\$idx) {" >> $TARGET
IDX=0
for user in `ls $ETCDIR`; do
if [ -f $ETCDIR/$user/name ]; then
   echo " case "$IDX": return new User"$user"();" >> $TARGET
   IDX=$(($IDX+1));
fi;
done

echo "} }" >> $TARGET


echo "function getNumUsers() { return count(self::getUsers()); }" >> $TARGET
echo "function getUserID(\$i){\$u=self::getUsers();\$k=array_keys(\$u);return \$k[\$i];}"  >> $TARGET
echo "function getUserName(\$i){\$u=self::getUsers();\$k=array_keys(\$u);return \$u[\$k[\$i]];}"  >> $TARGET

echo "}" >> $TARGET

echo ?\> >> $TARGET

