#! /bin/sh
# Author: Mirko Gnther (mi.guenther@ib-helms.de)
# Version: 0.2.3

WORKDIR=$PWD
VDRDIR="/usr/local/bin"
VIDEODIR="/video"
CPCONF="no"
DELUG="no"
INSTDIR="/usr/local/vdr" #!!don't change this!!

echo " "
echo "start $0 ..."

# get parameters
for ((i=1;i<=$#;i++)); do
 if test $i = "1"; then
  PARAM1=$1
  PARAM2=$2
 fi
 if test $i = "2"; then
  PARAM1=$2
  PARAM2=$3
 fi
 if test $i = "3"; then
  PARAM1=$3
  PARAM2=$4
 fi
 if test $i = "4"; then
  PARAM1=$4
  PARAM2=$5
 fi
 if test $i = "5"; then
  PARAM1=$5
  PARAM2=$6
 fi
 if test $i = "6"; then
  PARAM1=$6
  PARAM2=$7
 fi
 if test $i = "7"; then
  PARAM1=$7
  PARAM2=$8
 fi
 if test $i = "8"; then
  PARAM1=$8
  PARAM2=$9
 fi
 if test $i = "9"; then
  PARAM1=$9
  PARAM2=$9
 fi
 case $PARAM1 in
  -help)
    echo "Usage (as root!): ./mk_vdrdaemon [-d] [-vdr VDRDIR] [-video VIDEODIR] [-cpconf [-cdir CONFDIR]] [-help]"
    echo "                  d        - delete the user 'vdr' and the group 'vdrdaem' at start"
    echo "                  VDRDIR   - directory, where vdr stored in."
    echo "                             (default: /usr/local/bin)"
    echo "                  VIDEODIR - direcrory, where vdr shold store the records."
    echo "                             (default: /video)"
    echo "                  cpconf   - copy all *.conf from CONFDIR to /usr/local/vdr/etc"
    echo "                             (default: VDRDIR)"
    echo "                  CONFDIR  - dir, where *.conf stored in."
    echo "                  help     - prints this screen."
    exit 1
    ;;
  -video) VIDEODIR=$PARAM2
    ;;
  -vdr) VDRDIR=$PARAM2
    ;;
  -cpconf)
    CPCONF="yes"
    CPDIR=$VDRDIR
    ;;
  -cdir) CPDIR=$PARAM2
    ;;
  -d) DELUG="yes"
    ;;
 esac
done

echo "work directory    : $WORKDIR"
echo "VDR directory     : $VDRDIR"
echo "video directory   : $VIDEODIR"
echo "install directory : $INSTDIR"
if test $CPCONF = "yes"; then
 echo "'*.conf' directory: $CPDIR"
fi
echo " "

# create user, group and set rights ...
if test $DELUG = "yes"; then
 echo -n "delete user 'vdr' and group 'vdrdaem'... "
 userdel vdr
 groupdel vdrdaem
 echo "done."
fi

echo -n "chmod scripts... "
chmod -f 0755 dvb
chmod -f 0755 vdrdaemon
chmod -f 0755 start_vdr
echo "done."

echo -n "change owner of 'dvb','vdrdaemon' to user 'root' ... "
chown -f root:root dvb
chown -f root:root vdrdaemon
echo "done."

echo -n "create group 'vdrdaem'... "
groupadd -g 99 vdrdaem
echo "done."

echo -n "create user 'vdr' (homedir: $VIDEODIR) ... "
useradd -u 99 -g vdrdaem -d $VIDEODIR -p vdrdaem vdr
echo "done."

echo -n "copy scripts 'dvb', 'vdrdaemon' to /etc/init.d ... "
cp --remove-destination dvb /etc/init.d
cp --remove-destination vdrdaemon /etc/init.d
echo "done."

echo -n "install scripts... "
insserv -d dvb
insserv -d vdrdaemon
echo "done."

echo -n "create rc* links at /usr/sbin ... "
cd /usr/sbin
ln -s -f ../../etc/init.d/dvb rcdvb
ln -s -f ../../etc/init.d/vdrdaemon rcvdrdaemon
cd $WORKDIR
echo "done."

echo -n "test exist of $VIDEODIR ... "
if test -d $VIDEODIR; then
 echo "exist."
 echo -n "change owner of $VIDEODIR to user 'vdr'... "
 cd $VIDEODIR
 chown -f vdr:vdrdaem .
 cd $WORKDIR
 echo "done."
else
 echo "not exist."
 echo -n "create $VIDEODIR and change owner to user 'vdr'... "
 mkdir $VIDEODIR
 chown -f vdr:vdrdaem $VIDEODIR
 echo "done."
fi

# create workenvironment for 'start_vdr' ...
echo -n "test exist of $INSTDIR ... "
if test -d "$INSTDIR"; then
 echo "exist."
else
 echo "not exist."
 echo -n "creating $INSTDIR ... "
 mkdir -p $INSTDIR
 echo "done."
fi

cd $INSTDIR
echo "Workdir: `pwd`"

echo -n "test exist of ./rec ... "
if test -d "./rec"; then
 echo "exist."
 echo -n "delete all links ... "
 cd rec
 rm -f *
 cd ..
 echo "done."
else
 echo "not exist."
 echo -n "create ./rec ... "
 mkdir rec
 echo "done."
fi

echo -n "test exist of ./etc ... "
if test -d "./etc"; then
 echo "exist."
else
 echo "not exist."
 echo -n "create ./etc ... "
 mkdir etc
 echo "done."
fi

#echo "Work: `pwd`"
echo -n "change owner to user 'vdr' ... "
chown -R -f vdr:vdrdaem .
echo "done."

cd $WORKDIR
echo "Workdir: `pwd`"

echo -n "copy 'start_vdr' to $INSTDIR ... "
cp --remove-destination start_vdr $INSTDIR
echo "done."

echo -n "set owner and rights of 'start_vdr' to user 'root'... "
chown -f root:root $INSTDIR/start_vdr
chmod 0740 $INSTDIR/start_vdr
echo "done."

if test ! -e "$INSTDIR/vdr"; then
echo -n "create link for 'vdr' ... "
cd $INSTDIR
ln -s -f $VDRDIR/vdr vdr
cd $WORKDIR
echo "done."
fi

if test $CPCONF = "yes"; then
 echo -n "copy $CPDIR/*.conf to $INSTDIR/etc and change owner to 'vdr'... "
 cd $CPDIR
 cp -d *.conf $INSTDIR/etc
 cp -d mount.sh $INSTDIR/etc
 if test -e "$VDRDIR/mp3sources.conf.example"; then
  if test ! -e "$INSTDIR/etc/mp3sources.conf"; then
   cp $VDRDIR/mp3sources.conf.example $INSTDIR/etc/mp3sources.conf
  fi
  if test ! -e "$INSTDIR/etc/mount.sh"; then
   cp $VDRDIR/mount.sh.example /usr/local/etc/mount.sh
  fi
  if test ! -e "usr/local/vdr/etc/mplayersources.conf"; then
   cd $INSTDIR/etc
   ln -s -f mp3sources.conf mplayersources.conf
   cd $CPDIR
  fi
 fi
 cd $INSTDIR/etc
 chown -f vdr:vdrdaem *
 cd $WORKDIR
 echo "done."
fi

echo -n "create link for $VIDEODIR in $INSTDIR/rec ..."
cd $INSTDIR/rec
ln -s $VIDEODIR video0
echo "done."

echo -n "set attribute write to /dev/tty8 ..."
chmod o+w /dev/tty8
echo "done."

echo "all done... exit."

