#!/bin/sh
#
# Copyright 1993,1994,1999 Patrick Volkerding, Moorhead, Minnesota USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is 
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# As always, bug reports, suggestions, etc: volkerdi@mhd1.moorhead.msus.edu
# or bf703@cleveland.freenet.edu, or (if you absolutely must) (218) 233-4349.
# 
# Modified for DeLi Linux
#

set_keymap() {
	echo '==> Set Keyboard mapping <=='

	if [[ `uname -m` == i[3-6]86 ]] ; then
		mapdir="`echo /usr/share/kbd/keymaps/i386`"
	else
   		echo "Unknown architecture." ; return 1
	fi
echo "dialog --menu \"Select keymap - Default is us \" 20 70 15 \\" >/tmp/tskript$$
for i in $(find /usr/share/kbd/keymaps/i386  -name '*.map.gz' | sed 's,^.*/,,; s,\.map\.gz$,,' | sort -u); do 
echo "$i keyboard \\" >>/tmp/tskript$$; done
echo "2> /tmp/keymap" >>/tmp/tskript$$
chmod a+x /tmp/tskript$$
. /tmp/tskript$$

if [ $? = 1 -o $? = 255 ]; then
    in='us';       
else
    in=`cat /tmp/keymap`;
fi


sed s/KEYMAP=.*/KEYMAP=$in/ /etc/rc.conf >/tmp/rc.conf.tmp
mv /tmp/rc.conf.tmp /etc/rc.conf

rm -f /tmp/tskript$$ >/dev/null
rm -f /tmp/keymap >/dev/null

}


set_locale() {

echo '==> Set language(s) <=='

echo "dialog --menu \"Select language \" 20 70 15 \\" >/tmp/tskript$$
while read i; do
echo "$i \\*  \\" >>/tmp/tskript$$; done</usr/lib/delisetup/locales.txt
echo "2> /tmp/return" >>/tmp/tskript$$
chmod a+x /tmp/tskript$$
. /tmp/tskript$$

if [ $? = 1 -o $? = 255 ]; then
    in='C';       
else
    in=`cat /tmp/return`;
fi
newprofile=`mktemp`

egrep -v "export LANG|unicode_start" /etc/profile >$newprofile

echo "export LANG='$in'" >> $newprofile
echo "unicode_start" >> $newprofile
mv  /etc/profile /etc/profile.old
cp $newprofile /etc/profile
chmod 644 /etc/profile
rm $newprofile

rm -f /tmp/tskript$$ >/dev/null
rm -f /tmp/return >/dev/null


}

set_rootpw() {
	echo '==> Set root password <=='
	while ! passwd root ; do sleep 1 ; done
	echo
}

set_lilo()  {
	/sbin/liloconfig
	echo
}

set_bootloader()  {
	/sbin/grubconfig /
	echo
}

set_ppp()   {
    if [ -x /usr/sbin/pppsetup ]; then
     /usr/sbin/pppsetup
    else
     dialog --msgbox "ppp not installed"  5 40
    fi
    echo
}




set_net()   {
    /usr/lib/delisetup/netconfig
    echo
}

set_xorg()  {
 dialog --msgbox "Hint:This is for setup the XOrg-Xserver\n\
To setup the tiny X Servers choose 'Setup Tiny X Server'" 7 60
if [ -x /usr/bin/xorgcfg ]; then
  ln -sf /usr/bin/Xorg /usr/bin/X
  cp /usr/lib/delisetup/startx /usr/bin
  mkdir -p /etc/X11
  /usr/bin/screen -U /usr/bin/xorgcfg -textmode
else
 dialog --msgbox "/usr/bin/xorgcfg not found"  5 60
fi
}

set_x()  {
 dialog --msgbox "Hint: This is for setup the Tiny X Server\n\
To setup Xorg X Server choose 'Setup XOrg X Server'"  7 60

if [ -x /usr/bin/Xvesa ]; then
 /usr/lib/delisetup/xconfig
else
 dialog --msgbox "You don't have the Tiny X-Servers installed"  5 60
fi
}




set_xwm()   {
    /usr/lib/delisetup/xwmconfig
    echo
}


set_services() {
rm -f /tmp/return
OLDSERV=`grep ^SERVICES /etc/rc.conf`
OSFILE=`mktemp`
echo $OLDSERV >$OSFILE


echo "dialog --separate-output --checklist \"Select services to run at boot\" 20 78 15 \\" >/tmp/tskript$$
    for i in $(ls /etc/rc.d/*); do
         s=`basename $i`
         description=`grep "^# SHORT-DESCRIPTION:" $i | cut -f2- -d':' 2>/dev/null`
         description=`echo $description`
         if egrep -q "$s |$s\"" $OSFILE; then
	   echo "$s \"$description\" on \\" >>/tmp/tskript$$
         else
	   echo "$s \"$description\" off \\" >>/tmp/tskript$$
         fi
     done
    echo "2> /tmp/return" >>/tmp/tskript$$
chmod a+x /tmp/tskript$$
. /tmp/tskript$$


if [ $? = 1 -o $? = 255 ]; then
    SERV=$OLDSERV;
else
    first=1
    # net service must come first
    if grep -q ^net$ /tmp/return; then
      SERV="SERVICES=\"net"
      first=0
      grep -v ^net$ /tmp/return >/tmp/return.new
      mv /tmp/return.new /tmp/return
    else   
      SERV="SERVICES=\""
    fi
    while read n; do
      if [ $first -ne 1 ]; then
       SERV="${SERV} ${n}"
      else
       SERV="${SERV}${n}"
       first=0
      fi
    done < /tmp/return
    SERV="$SERV\""
fi
cp /tmp/return /tmp/retun-alt
cp /etc/rc.conf /etc/rc.conf.old
grep -v SERVICES /etc/rc.conf >/tmp/rc.conf.new

echo "$SERV" >>/tmp/rc.conf.new
mv /tmp/rc.conf.new /etc/rc.conf
rm $OSFILE /tmp/tskript$$
}


set_repos_state() {
rm -f /tmp/return

repos_url_default="/usr/lib/delisetup/repos.url.default"
repos_url_custom="/usr/lib/delisetup/repos.url.custom"
repos_state_default="/usr/lib/delisetup/repos.state.default"
repos_state_custom="/usr/lib/delisetup/repos.state.custom"
pacman_conf_header="/usr/lib/delisetup/pacman.conf.header"

[ -f $repos_state_default ] || dialog --title 'First configuration' --msgbox "This is Your first try to enable/disable repositories.\n\nDefault settings have been loaded.\n\nThis action will replace Your existing pacman.conf file!\n\nYou can discard all changes by pressing the <cancel> button\non the next dialog." 13 65

echo "dialog --separate-output --checklist \"Select repositories\" 20 70 15 \\" >/tmp/tskript$$
    (
      cat $repos_url_default
      [ -f $repos_url_custom ] && cat $repos_url_custom
    ) | while read repo_line; do
      repo_name=`echo $repo_line | cut -f1 -d" "`
      repo_state=""
      [ -f $repos_state_default ] && repo_state=`grep "^$repo_name " < $repos_state_default | cut -f2 -d" "`
      [ "$repo_state" = "" ] && [ -f $repos_state_custom ] && repo_state=`grep "^$repo_name " < $repos_state_custom | cut -f2 -d" "`
      # state unknown? -> set default state
      [ "$repo_name" = "main" ] && [ "$repo_state" = "" ] && repo_state="1"  # main repo is enabled by default
      [ "$repo_state" = "" ] && repo_state="0" # all other repos are disabled by default

      if [ "$repo_state" = "1" ]; then
        on_off_str="on"
      else
        on_off_str="off"
      fi
      echo "$repo_line $on_off_str \\" >>/tmp/tskript$$
    done
echo "2> /tmp/return" >>/tmp/tskript$$
chmod a+x /tmp/tskript$$
. /tmp/tskript$$

# 1 - cancel, 255 - esc
if [ $? = 1 -o $? = 255 ]; then
  :
else
  # set all repos to disabled
  cat $repos_url_default | while read repo_line; do
    repo_name=`echo $repo_line | cut -f1 -d" "`
    echo "$repo_name 0"
  done > $repos_state_default
  if [ -f $repos_url_custom ]; then
    cat $repos_url_custom | while read repo_line; do
      repo_name=`echo $repo_line | cut -f1 -d" "`
      echo "$repo_name 0"
    done > $repos_state_custom
  fi

  # set selected repos to enabled
  (
  cat $pacman_conf_header
  cat /tmp/return | while read repo_name; do
    repo_url=`grep "^$repo_name " < $repos_url_default | cut -f2- -d" "`
    [ "$repo_url" = "" ] && repo_url=`grep "^$repo_name " < $repos_url_custom | cut -f2- -d" "`
    # add in the pacman.conf
    echo "[$repo_name]"
    echo "Server = $repo_url"
    echo
    # set state to enabled
    sed -i "s/$repo_name 0$/$repo_name 1/g" $repos_state_default
    [ -f $repos_state_custom ] && sed -i "s/$repo_name 0$/$repo_name 1/g" $repos_state_custom
  done
  ) > /etc/pacman.conf
fi
rm /tmp/tskript$$
}


add_repo() {
  rm -f /tmp/return

  repos_url_default="/usr/lib/delisetup/repos.url.default"
  repos_url_custom="/usr/lib/delisetup/repos.url.custom"

  while : ; do
    dialog --title "Add custom repository" --inputbox "Enter the repository name (matching the target *.db filename):\nValid characters A-Z a-z 0-9 ( ) _ -" 9 76 2> /tmp/return
    status=$?
    [ $status = 1 -o $status = 255 ] && break;
    repo_name=`cat /tmp/return`
    repo_name_trim=`echo $repo_name`
    [ "$repo_name_trim" = "" ] && continue;
    repo_name_invalid_characters=`echo "$repo_name_trim" | sed 's/[A-Za-z0-9()_-]//g'`
    if [ "$repo_name_invalid_characters" = "" ]; then
      :
    else
      dialog --title 'Failure' --msgbox "Invalid name entered!" 6 39
      continue;
    fi
    repo_exists=`(([ -f $repos_url_custom ] && cat $repos_url_custom) ; cat $repos_url_default ; ) | grep "^$repo_name_trim "`
    if [ "$repo_exists" = "" ]; then
      :
    else
      dialog --title 'Failure' --msgbox "Repository of that name already exists!" 6 59
      continue;
    fi
    break;
  done

  if [ $status = 1 -o $status = 255 ]; then
    :
  else
    dialog --colors --inputbox "Enter the repository URL:" 8 76 2> /tmp/return
    status=$?
    if [ $status = 1 -o $status = 255 ]; then
      :
    else
      repo_url=`cat /tmp/return`
      echo "$repo_name_trim $repo_url" >> $repos_url_custom
    fi
  fi
}


delete_repo() {
  rm -f /tmp/return

  repos_url_default="/usr/lib/delisetup/repos.url.default"
  repos_url_custom="/usr/lib/delisetup/repos.url.custom"

  while : ; do
    dialog --title "Delete custom repository" --inputbox "Enter the repository name:\nValid characters A-Z a-z 0-9 ( ) _ -" 9 76 2> /tmp/return
    status=$?
    [ $status = 1 -o $status = 255 ] && break;
    repo_name=`cat /tmp/return`
    repo_name_trim=`echo $repo_name`
    [ "$repo_name_trim" = "" ] && continue;
    repo_name_invalid_characters=`echo "$repo_name_trim" | sed 's/[A-Za-z0-9()_-]//g'`
    if [ "$repo_name_invalid_characters" = "" ]; then
      :
    else
      dialog --title 'Failure' --msgbox "Invalid name entered!" 6 39
      continue;
    fi
    repo_exists=`cat $repos_url_default | grep "^$repo_name_trim "`
    if [ "$repo_exists" = "" ]; then
      :
    else
      dialog --title 'Failure' --msgbox "Only custom repositories can be deleted!" 6 59
      continue;
    fi
    repo_exists=`([ -f $repos_url_custom ] && cat $repos_url_custom ) | grep "^$repo_name_trim "`
    if [ "$repo_exists" = "" ]; then
      dialog --title 'Failure' --msgbox "Repository of that name doesn't exist!" 6 59
      continue;
    fi
    break;
  done

  if [ $status = 1 -o $status = 255 ]; then
    :
  else
    grep -v "^$repo_name_trim " < $repos_url_custom > $repos_url_custom.tmp
    mv -f $repos_url_custom.tmp $repos_url_custom
  fi
}


set_repos() {
  rm -f /tmp/return

  while : ; do
    dialog --menu "Setup Repositories" 20 70 15 \
    "1" "Enable/Disable Repositories" \
    "2" "Add Custom Repository" \
    "3" "Delete Custom Repository" \
    "Q" "Return to the main menu" 2>/tmp/return

    if [ $? = 1 -o $? = 255 ]; then
      break;
    else
      ret=`cat /tmp/return`
      case "$ret" in
        "1" ) set_repos_state ;;
        "2" ) add_repo ;;
        "3" ) delete_repo ;;
        "Q" ) break ;;
      esac
    fi

  done
}


set_sound() {
  clear
  echo "Trying to play sample sound..."
  /usr/sbin/sndconfig
  echo
}

set_usb() {
  /usr/lib/delisetup/usbconfig
  echo
}

set_pcmcia() {
  /usr/lib/delisetup/pcmciaconfig
  echo
}


set_hw() {
  rm -f /tmp/return

  while : ; do
    dialog --menu "Setup Hardware" 20 70 15 \
    "1" "Setup Keyboard" \
    "2" "Setup PPP (needs data from your Internet Service Provider)" \
    "3" "Setup PCMCIA" \
    "4" "Setup USB" \
    "5" "Setup Network" \
    "6" "Setup XOrg X Server" \
    "7" "Setup Tiny X Server" \
    "8" "Setup Sound" \
    "Q" "Return to the main menu" 2>/tmp/return

    if [ $? = 1 -o $? = 255 ]; then
      break;
    else
      ret=`cat /tmp/return`
      case "$ret" in
        "1" ) set_keymap ;;
        "2" ) set_ppp ;;
        "3" ) set_pcmcia ;;
        "4" ) set_usb ;;
        "5" ) set_net ;;
        "6" ) set_xorg ;;
        "7" ) set_x ;;
        "8" ) set_sound ;;
        "Q" ) break ;;
      esac
    fi

  done
}



TMP=/var/log/setup/tmp

PATH="$PATH:/usr/lib/delisetup"
if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
rm -f $TMP/* >/dev/null

I_AM=`whoami`


if [ -e /var/delisetupmade ]; then # Setup-Menu
  while : ; do
    if [ "$I_AM" = "root" ]; then # root account
      dialog --menu "DeLi(cate) Setup" 20 70 15 \
      "1" "Setup Language" \
      "2" "Setup Hardware" \
      "3" "Setup Bootloader" \
      "4" "Setup Window Manager" \
      "5" "Setup Services" \
      "6" "Setup Repositories" \
      "Q" "Quit" 2>/tmp/return

      if [ $? = 1 -o $? = 255 ]; then
        exit 1;
      fi

      ret=`cat /tmp/return`

      case "$ret" in
        "1" ) set_locale ;;
        "2" ) set_hw ;;
        "3" ) set_bootloader ;;
        "4" ) set_xwm ;;
        "5" ) set_services ;;
        "6" ) set_repos ;;
        "Q" ) rm -f /tmp/return ; clear ; exit 0 ;;
        *)    rm -f /tmp/return ; clear ; exit 1 ;;
      esac

    else # not-root account
      dialog --menu "DeLi(cate) Setup" 20 70 15 \
      "1" "Setup Window Manager" \
      "Q" "Quit" 2>/tmp/return

      if [ $? = 1 -o $? = 255 ]; then
        exit 1;
      fi

      ret=`cat /tmp/return`

      case "$ret" in
        "1" ) set_xwm ;;
        "Q" ) rm -f /tmp/return ; clear ; exit 0 ;;
        *)    rm -f /tmp/return ; clear ; exit 1 ;;
      esac
    fi
  done

else # First Setup

probing
SeTpartitions
SeTswap
SeTDOS
T_PX=/
     cat $TMP/SeTnative > $T_PX/etc/fstab
     if [ -r $TMP/SeTswap ]; then
      cat $TMP/SeTswap >> $T_PX/etc/fstab
     fi


     if [ -r $TMP/SeTDOS ]; then
      cat $TMP/SeTDOS >> $T_PX/etc/fstab
     fi
     echo "none             /dev/pts  devpts     gid=5,mode=620  0   0" >> $T_PX/etc/fstab
     echo "none             /proc    proc        defaults   0   0" >> $T_PX/etc/fstab

# set right permissions for some device files

chmod 666 /dev/tty* >/dev/null
chmod 666 /dev/null* >/dev/null
chmod 666 /dev/pty* >/dev/null

set_keymap
set_rootpw
set_locale
set_lilo

  
   dialog --title "SETUP COMPLETE" --msgbox "System configuration \
and installation is complete. You may EXIT setup and reboot your machine with \
ctrl-alt-delete." 7 55
touch /var/delisetupmade
exit 0

fi
