#!/bin/bash
# Falken Installation script
# written by Eric Kilfoil (kEwjhOe) 
# http://bbs.ipass.net

echo "[40m[2J[1;1H[70C[0;1;30m"
echo "[16C[22C[10C[7C[47m[37m[0m[1;30m"
echo "[8C[47m[37m[0m[1;30;47m[40m  [47m[40m    [47m[37m[30m[40m [47m[37m[30m[40m   [47m[37m[40m[0m[1;30m"
echo "[5C[47m[37m[40m   [0m[1;47m[40m[0m[1;47m[30m [37;40m[0m[1;30m  [47m[37m[40m  [0m[1;30;47m[37m[40m    [47m[0m[1;30m [47m[0m[1m [0m[1;30m"
echo "   [47m[37m[40m[9C  [0m[1;45m[3C[40m[0m[1m   [0m[1;30m[0m[1m  [47m[40m[8C[47m[40m [47m[30m[37m[40m [0m[1;30m"
echo "  [0m[1m[6C[47m[40m    [47m[45m[3C[40m[47m[30m[37m[40m   [0m[1;30m[0m[1m      [47m[30m[37m[40m  [0m[1;30;47m[37m[40m [0m[1;30m"
echo "  [47m[37m[40m  [47m[0m[1;47m[40m    [47m[45m[3C[40m[0m[1;30m[0m[1m   [0m[1;30m[37;47m[40m [0m[1m  [47m[0m[1;30m[0m[1m   [47m[30m[0m[1m  [0m[1;30m"
echo "   [0m[1m   [47m[40m   [0m[1m  [45m[2C[40m[47m[30m[40m[47m[37m[40m [47m[30;40m[47m[37m[40m  [0m[1m  [47m[40m[0m[1;30m[0m[1m   [0m[1m  [0m[1;30m"
echo "  [47m[37m[40m         [0m[1;47m[40m   [0m[1;30m [47m[37m[40m [47m[40m  [47m[0m[1;47m[40m   [47m[30m[40m[47m[37m[40m  [47m[0m[1m  [0m[1;30m"
echo "  [47m[37m[40m    [47m[40m        [47m[30m[40m [0m[1;45m[4C[40m  [47m[0m[1;47m[40m [47m[30m[40m[47m[37m[40m   [47m[40m [47m[30m[40m"
echo "   [0m[1m   [47m[30m[37m[40m  [47m[40m   [47m[30m[40m[47m[37;40m [47m[30m[37m[40m [47m[40m  [0m[1;30m[47m[37m[40m [45m[2C[40m  [47m[40m  [0m[1;30m"
echo "   [0m[1m   [0m[1;30;47m[0m[1m  [0m[1;30;47m[40m[0m[1m   [0m[1;30m[47m[37m[40m[0m[1;30m[47m[40m[47m[37m[40m   [47m[40m    [45m[2C[40m[47m[40m    [0m[1;30m"
echo "   [47m[37m[40m [47m[30m[37m[40m [0m[1;30m  [0m[1m  [47m[40m[5C[0m[1;30m [0m[1m [7C [47m[0m[1m    [47m[30m[40m"
echo "    [0m[1m [0m[1;30;47m[37m[40m[0m[1;30m   [0m[1m [0m[1m[5C[45m[47m[30m[37m[0m[1;30m [0m[1m [45m[40m[47m[30m[0m[1m [0m[1;30m[47m[37m[40m  [47m[30m[40m"
echo "    [47m[37m[40m[47m[30m[40m[47m[37m[40m[0m[1;30m   [0m[1m [0m[1;47m[40m[47m[30m[40m [37m[0m[1;30m [47m[37m[40m[0m[1;30;47m[40m   [47m[37m[40m[0m[1;30m [47m[37m[30m[40m"
echo "[5C[37;47m[40m[0m[1;30m [0m[1m[47m[30m[40m   [47m[37m[40m[0m[1m[47m[30m[40m[47m[0m[1m   [0m[1;30m  [0m[1m[47m[30m[40m[10C[0m[1m[47m[30m[40m   "
echo "[5C[37;47m[30m[40m [47m[37m[0m[1;30m    [47m[37m[40m[30;47m[37m[0m[1;30m  [47m[0m[1m  [47m[30m[40m  [47m[37m[0m[1;30m[11C[47m[37m[0m[1;30m"
echo "[5C[37;47m[0m[1;30m   [0m[1;30;47m[40m[5C[0m[1m[47m[30m[40m[0m[1;30;47m[40m  [47m [37m[30m[40m[5C[0m[1;30;47m[40m[12C[0m[1;30;47m[40m"
echo "[5C[0m[1;30;47m[40m   [47m[40m[6C[47m[37m[0m[1;30m[47m[40m    [7C[47m[40m[13C[47m[40m"
echo "[5C[47m[40m[5C[7C[0m[1;30;47m[40m [16C[14C"
echo "[6C[5C[8C[47m[40m  [17C[15C"
echo "[6C[16C"
echo "[23C[0m"

echo "[1;33mWelcome to Falken BBS software.  As a future sysop, all that have put"
echo "time and effort into this software wish you good luck on a successful BBS."
echo ""
echo -n "[1;30mP[0mr[1me[0ms[1;30ms [1;30m[[0mE[1mNTE[0mR[1;30m] [1;30mt[0mo [1;30mc[0mo[1mntin[0mu[1;30me[0m"
read $blah
if [ `id -u` != 0 ]; then
	echo "We will be adding a user to the system.  In order to do this, you" 
	echo "must be logged in as root.  Please su root or relogin as root and"
	echo "re-run this installation script"
	exit 1
fi
echo "[2J[1;1H[1;33mTo start, we will need to know the name of the user you wish the bbs"
echo "to run as.  '[1;37mbbs[1;33m' is HIGHLY recommended.  In fact, the only reason"
echo "you should use a username OTHER than bbs is if the bbs user already"
echo "exists.  If you have already made a user for the bbs, you should hit CTRL-C"
echo "and delete the user before continuing"
echo "Enter the name of the BBS user:"
echo -n "[[1;37mbbs[1;33m]: [0m"
read BBSUID
if [ -z $BBSUID ] ; then
	BBSUID="bbs"
fi
echo "[1;33mWhat will be the Group Name the BBS will be in?"
echo "Keep in mind that the bbs should have it's OWN group. Do NOT put the bbs"
echo "in the same group as ANY other user.  The group name 'sysop' is HIGHLY"
echo "recommended"
echo -n "[[1;37msysop[1;33m]: [0m"
read BBSGID
if [ -z $BBSGID ] ; then
	BBSGID="sysop"
fi
echo "[1;33mNow enter the name of the directory you wish to install Falken to:"
echo "This MUST be the full path (~'s are ok)"
echo -n "[[1;37m/falken[1;33m]: [0m"
read INSTALLDIR
if [ -z $INSTALLDIR ] ; then
	INSTALLDIR=/falken
fi

echo -n "[1;37m$INSTALLDIR [1;33mis the correct directory? ([1;37my[1;33m/[1;37mN[1;33m): [0m"
read YN
if [ $YN != "y" -a $YN != "Y" ] ; then
	echo "Exiting error 2"
	echo "Please rerun the 'install' script"
  exit 1
fi
if [ ! -d $INSTALLDIR ] ; then
	echo -en "[1;37m$INSTALLDIR [1;33mdoesn't exist. \nDo you want to create it ([1;37my[1;33m/[1;37mN[1;33m): [0m"
	read YN
	if [ $YN != "y" -a $YN != "Y" ] ; then
		exit 1
	fi
	mkdir -p $INSTALLDIR
	chmod 6770 $INSTALLDIR
fi

echo "[1;34m-----------------------------------------------------"
echo "[1;33mAdding a couple lines into /etc/profile"
echo ""
echo "It is neccessary to trap the CTRL-C character in the /etc/profile"
echo "This will not affect NORMAL users on the system, but this will keep"
echo "BBS users from being able to CTRL-C out of the profile before the"
echo "bbslogin program runs thereby giving that user access to the BBS from"
echo "a bash shell.  DO NOT REMOVE THESE LINES from the /etc/profile"
echo "unless you REALLY know what you're doing!"
echo ""
echo -n "[1;30mP[0mr[1me[0ms[1;30ms [1;30m[[0mE[1mNTE[0mR[1;30m] [1;30mt[0mo [1;30mc[0mo[1mntin[0mu[1;30me[0m"
read $blah
echo ""
echo "Backing up the /etc/profile to /etc/profile.bak"
cp /etc/profile /etc/profile.bak
echo "" > /etc/profile
echo "trap '' 2 3" >> /etc/profile
echo -en "if [ \"\$USER\" = \"root\" ]; then\n" >> /etc/profile
echo "	PATH=\$PATH:$INSTALLDIR/modules:$INSTALLDIR" >> /etc/profile
echo "  FALKENDIR=$INSTALLDIR" >> /etc/profile
echo "  export FALKENDIR" >> /etc/profile
echo "fi" >> /etc/profile
cat /etc/profile.bak >> /etc/profile
echo "trap  2 3" >> /etc/profile

echo "[1;34m-----------------------------------------------------"
echo "[1;33mAdding the user for the BBS..."

# This portion of the installation script is from the REDHAT installation
# adduser script.  Full credit goes out to the guy who wrote this.

# trap "" 1 2 3 15

# Set a few important variables before getting started.
NUMARG=$#
LOGIN=$BBSUID
EXIST=0

NOHOME="$2"

PASSWD="/etc/passwd"
PBAK="/etc/passwd-"		
PNEW="/etc/passwd.new"

SHADOW="/etc/shadow"
SBAK="/etc/shadow-"
SNEW="/etc/shadow.new"

GROUP="/etc/group"
GBAK="/etc/group-"
GNEW="/etc/group.new"

PLOCK="/etc/.pwd.lock"		# Standard method of locking the password file.

#DSHELL="/bin/bash"
DSHELL=$INSTALLDIR/bbslogin
DHOME="/home"
SKEL="/etc/skel"
FIRST_UID=500
FIRST_GID=500

# A few sanity checks...

if id "$LOGIN" >/dev/null 2>&1 ; then
	echo "The user $LOGIN already exists.  You must select a different"
	echo "username or delete the $LOGIN account.  Exiting."
	exit 1
fi

if [ $(echo "$LOGIN" | wc -c | tr -d ' ') -gt 9 ] ; then
	# that 9 is intentional -- it counts the newline, too...
	echo "$LOGIN is over eight characters.  It is generally a bad idea to have"
	echo "a username with more than eight characters, so I will not condone it"
	echo "Exiting."
	exit 1
fi

if echo "$LOGIN" | grep -q : ; then
	echo "$LOGIN contains a \`:' character"
	echo "That character cannot be used.  Exiting."
	exit 1
fi

id $LOGIN >/dev/null 2>/dev/null && EXIST=1

if [ $EXIST = 1 ]; then
	echo "The user $LOGIN already exists.  You must select a different"
	echo "username or delete the $LOGIN account.  Exiting."
	exit 1
fi

if ln $PASSWD $PLOCK ; then : ; else
	echo "$PASSWD is locked." 
	echo "Verify that this is not a stale lock and try again later."
	exit 1
fi

die ()
{
  rm -f $PLOCK
  exit 1
}

# And now the program begins: 
echo "" ; echo -n "Looking for first available UID..."
NUID=`cut -f 3 -d ":" $PASSWD | sort -n | awk -v uid=$FIRST_UID '
		{ if ($1 == uid) uid = uid + 1; }
END		{ print uid; }
'`

if [ $NUID -ge 65535 ]; then
	echo "Sorry, ran out of uids."
	die
fi
echo " $NUID"


echo -n "Looking for first available GID..."
NGID=`cut -f 3 -d ":" $GROUP | sort -n | awk -v gid=$FIRST_GID '
		{ if ($1 == gid) gid = gid + 1; }
END		{ print gid; }
'`

if [ $NGID -lt $FIRST_GID ]; then
	NGID=$FIRST_GID
fi

if [ $NGID -ge 65535 ]; then
	echo "Sorry, ran out of gids."
	die
fi
echo " $NGID"

echo "" ; echo -n "Adding login: $LOGIN..."


cp $PASSWD $PBAK || die
cp $PASSWD $PNEW || die
if [ -f "$SHADOW" ] ; then
  echo "$LOGIN::$NUID:$NGID::$INSTALLDIR:$DSHELL" | \
     cat >> $PNEW || die
  # need cat because echo won't return a return code
  cp $SHADOW $SBAK || die
  cp $SHADOW $SNEW || die
  # The date invocation is non-standard, but works with GNU date.
  # It fills in the "last changed" field with the number of DAYS
  # since epoch (86400 seconds per day; %s is second since epoch).
  echo "$LOGIN:*:$(($(date '+%s')/86400))::::::" | \
    cat >> $SNEW || die
else
  echo "$LOGIN::$NUID:$NGID::$INSTALLDIR:$DSHELL" | \
    cat >> $PNEW || die
fi

# Add user to users group
cp $GROUP $GBAK || die
cp $GROUP $GNEW || die

echo "$BBSGID::$NGID:" | cat >> $GNEW || die

mv $GNEW $GROUP

if [ -f "$SHADOW" ] ; then
  mv $SNEW $SHADOW
fi
mv $PNEW $PASSWD

rm -f $PLOCK
echo "done."

echo "[1;34m-----------------------------------------------------"
echo "[1;33mExtracting files..."
cp falken.install.tgz $INSTALLDIR
cp setup $INSTALLDIR
cd $INSTALLDIR
tar xfz falken.install.tgz
rm falken.install.tgz
echo "Extraction complete"

echo "[1;34m-----------------------------------------------------"
echo "[1;33mSetting Ownership..."
echo "We will be setting the ownership of EVERYTHING under"
echo "[1;37m$INSTALLDIR [1;33mto [1;37m$BBSUID[1;33m:[1;37m$BBSGID"
echo ""
echo "[1;33mIf this is NOT what you want, please hit CTRL-C NOW.  Otherwise"
echo "press enter and we'll be on our way.  If you're not sure, then this"
echo "is probably OK"
echo -n "[1;30mP[0mr[1me[0ms[1;30ms [1;30m[[0mE[1mNTE[0mR[1;30m] [1;30mt[0mo [1;30mc[0mo[1mntin[0mu[1;30me[0m"
read $blah
cd $INSTALLDIR
chown -R $BBSUID:$BBSGID .
chmod 770 data docs logfiles mailatt menus menus/ansi modules workfiles
chmod 6770 mainbbs
echo "Done...  "
echo "[1;34m-----------------------------------------------------"
echo "[1;33mCreating the .profile for the bbs user..."
echo "trap '' 2 3" > $INSTALLDIR/.profile
echo "PATH=$PATH:$INSTALLDIR:$INSTALLDIR/modules:./" >> $INSTALLDIR/.profile
echo "export PATH" >> $INSTALLDIR/.profile
echo "cd $INSTALLDIR" >> $INSTALLDIR/.profile
echo "umask 007" >> $INSTALLDIR/.profile
echo "exec ./bbslogin" >> $INSTALLDIR/.profile
echo "logout" >> $INSTALLDIR/.profile
echo "Done..."
if [ $INSTALLDIR != "/falken" ] ; then
	echo "[1;34m-----------------------------------------------------"
	echo "[1;33mCreating a symbolic link from the / directory"
	echo "By default, Falken is setup to run from the /falken directory"
	echo "A symbolic link will be made from /falken to $INSTALLDIR"
	echo "If you do NOT want this to be there, you must go into the"
	echo "Falken setup program ([1;37mbbscfg[1;33m) and modify the"
	echo "paths for the bbs"
	echo ""
	echo -n "[1;30mP[0mr[1me[0ms[1;30ms [1;30m[[0mE[1mNTE[0mR[1;30m] [1;30mt[0mo [1;30mc[0mo[1mntin[0mu[1;30me[0m"
	read $blah
  ln -s $INSTALLDIR /falken
fi
echo
echo "[1;33mNext we are going to ask you some simple questions that will"
echo "automaticly setup your account and give you sysop access."
echo -n "[1;30mP[0mr[1me[0ms[1;30ms [1;30m[[0mE[1mNTE[0mR[1;30m] [1;30mt[0mo [1;30mc[0mo[1mntin[0mu[1;30me[0m"
read blah
./setup
chown $BBSUID:$BBSGID $INSTALLDIR/data/*
chmod 660 $INSTALLDIR/data/*
echo "[1;34m-----------------------------------------------------"
echo "[1;33mStaring the BBS for the first time"
echo "Now it's time to attempt to start the BBS for the first time"
echo "If this fails, then something is wrong with the way Falken"
echo "got installed.  Look everything over to see if it works"
echo "and you should be ready to go."
echo "To log into your bbs, use the login name $LOGIN at the login"
echo "prompt.  ENJOY!"
echo -n "[1;30mP[0mr[1me[0ms[1;30ms [1;30m[[0mE[1mNTE[0mR[1;30m] [1;30mt[0mo [1;30mc[0mo[1mntin[0mu[1;30me[0m"
read $blah
cd $INSTALLDIR
PATH=$PATH:$INSTALLDIR:$INSTALLDIR/modules
FALKENDIR=$INSTALLDIR
export PATH FALKENDIR
idxmaker msgtext >/dev/null ; idxmaker msgansi >/dev/null ; idxmaker msgrip >dev/null
mainbbs
echo "[0m"
