#!/bin/sh -e
#
# BURG - Brand-new Universal loadeR from GRUB
# Copyright 2010 Bean Lee - All Rights Reserved
#
# BURG 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 3 of the License, or
# (at your option) any later version.
#
# BURG 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 BURG.  If not, see <http://www.gnu.org/licenses/>.

# Initialize some variables.
transform="s,x,x,"

prefix=/usr
exec_prefix=${prefix}
bindir=${exec_prefix}/bin
sbindir=${exec_prefix}/sbin
sysconfdir=/etc

pass_type=pbkdf2
grub_mkpasswd=${bindir}/`echo burg-mkpasswd | sed ${transform}`-${pass_type}
grub_deluser=${sbindir}/`echo burg-deluser | sed ${transform}`
pass_file=${sysconfdir}/default/burg-passwd

# Usage: usage
# Print the usage.
usage () {
    cat <<EOF
Usage: $0 [OPTION] username
Add or edit a burg user

  -s, --super             add super user
  -h, --help              print this message and exit
  -v, --version           print the version information and exit

Report bugs to <bean123ch@gmail.com>.
EOF
}

user_type="u"

# Check the arguments.
for option in "$@"; do
    case "$option" in
    -h | --help)
	usage
	exit 0 ;;
    -v | --version)
	echo "$0 (${PACKAGE_NAME} ${package_version})"
	exit 0 ;;
    -s | --super)
	user_type="s"
	shift
	;;
    -*)
	echo "Unrecognized option \`$option'" 1>&2
	usage
	exit 1
	;;
    esac
done

if [ "x$1" = "x" ] ; then
    echo "$0: No username is specified" >&2
    exit 1
fi

username=`echo $1 | sed 's,[^a-zA-Z0-9],,'`
if [ "x$1" != "x$username" ] ; then
    echo "$0: Only digits and letters are allowed in username" >&2
    exit 1
fi

if [ "x$EUID" = "x" ] ; then
  EUID=`id -u`
fi

if [ "$EUID" != 0 ] ; then
  root=f
  case "`uname 2>/dev/null`" in
    CYGWIN*)
      # Cygwin: Assume root if member of admin group
      for g in `id -G 2>/dev/null` ; do
	case $g in
	  0|544) root=t ;;
	esac
      done ;;
  esac
  if [ $root != t ] ; then
    echo "$0: You must run this as root" >&2
    exit 1
  fi
fi

temp_file=`mktemp`
chmod 600 $temp_file
$grub_mkpasswd -o $temp_file
password=`cat $temp_file`
rm $temp_file

$grub_deluser $username || true

echo "$username:$user_type:$pass_type:$password" >> ${pass_file}
password=""

exit 0
