#!/bin/sh

DEFAULT_BINDIR=/usr/local/bin
DEFAULT_OPTDIR=/usr/local/divxPlayer

echo "*********************************************"
echo "*                                           *"
echo "*     DivX Player 2.0 Alpha Installation    *"
echo "*                                           *"
echo "*********************************************"
echo ""
echo "Welcome to the Divx Player 2.0 setup:"

echo ""

echo "Please choose the installation directory"
echo -n "for the DivX Player 2.0 Alpha executable [$DEFAULT_BINDIR]: "
read bindir

echo ""

echo "Please choose the installation directory"
echo -n "for the default skin directory [$DEFAULT_OPTDIR]: "
read optdir

if [ -z "$bindir" ] ; then

    bindir=$DEFAULT_BINDIR

fi

if [ -z "$optdir" ] ; then
 
   optdir=$DEFAULT_OPTDIR

fi

echo ""

if [ ! -e $bindir ] ; then

echo -n "Directory $bindir does not exist, create (y/n) [y]? : "
read answer
echo ""

if [ -z "$answer" ] ; then

  answer=Y

fi

case "$answer" in 

  "Y" | "y" | "" ) mkdir $bindir
                   if [ ! -e $bindir ] ; then
		     echo "Could not create $bindir directory, check persmisions"
		     echo "exiting..."
		     echo ""
		     exit 65
		   fi
		   ;;

  * )              echo "Cannot install if directory not created"
		   echo "exiting..."
		   echo ""
		   exit 65 
esac

fi

# test if it is actually a directory

if [ ! -d "$bindir" ] ; then

  echo "File $bindir exists but is not a directory!"
  echo "exiting..."
  echo ""
  exit 65

fi

# test for write permission

if [ ! -w "$bindir" ] ; then

  echo "Could not get write permission to $bindir, check permissions"
  echo "exiting..."
  echo ""
  exit 65

fi

# Now, we're ok :)

if [ ! -e $optdir ] ; then

echo -n "Directory $optdir does not exist, create (y/n) [y]? : "
read answer
echo""

if [ -z "$answer" ] ; then

  answer=Y

fi

case "$answer" in 

  "Y" | "y" | "" ) mkdir $optdir
                   if [ ! -e $optdir ] ; then
		     echo "Could not create $optdir directory, check permissions"
		     echo "exiting..."
		     echo ""
		     exit 65
		   fi
		   ;;

  * )              echo "Cannot install if directory not created"
		   echo "exiting..."
		   echo ""
		   exit 65 
esac

fi

# test if it is actually a directory

if [ ! -d "$optdir" ] ; then

  echo "File $optdir exists but is not a directory!"
  echo "exiting..."
  echo ""
  exit 65

fi

# test for write permission

if [ ! -w "$optdir" ] ; then

  echo "Could not get write permission to $optdir, check permissions"
  echo "exiting..."
  echo ""
  exit 65

fi

# Now, we're ok :)

# do the install

cp ./divxPlayer.bin $bindir
cp -r Skins $optdir/

# now create the bash script that will run the player

if [ -e "$bindir/divxPlayer" ] ; then

  if [ ! -w "$bindir/divxPlayer" ] ; then

    echo "The file $bindir/divxPlayer already exists and is not writeable!"
    echo "exiting..."
    exit 65

  fi

fi

skinDir=$optdir/Skins

echo "#!/bin/sh"                                         >  $bindir/divxPlayer
echo ""                                                  >> $bindir/divxPlayer
echo "BINDIR=$bindir"                                    >> $bindir/divxPlayer
echo "DIVX_SKIN_DIR=$skinDir; export DIVX_SKIN_DIR"      >> $bindir/divxPlayer
echo ""                                                  >> $bindir/divxPlayer
echo "$bindir/divxPlayer.bin \$@"                        >> $bindir/divxPlayer

chmod 755 $bindir/divxPlayer

echo "Installation successful!"
echo ""

echo "You may now run the Divx Player 2.0 Alpha by typing $bindir/divxPlayer" 
echo ""

exit 0

