#!/bin/sh

I_AM=`whoami`
if [ "$I_AM" != "root" ]; then
  echo -e "This action requires root privileges.\n"
  exit 1
fi

TMP=/var/log/setup/tmp
if [ ! -d $TMP ]; then
 mkdir -p $TMP
 chmod 700 $TMP
fi

if [ ! -d lost+found -a ! -d vmlinuz -a ! -d proc ]; then # cheap, but it works :^)
 cd /
fi

#
############################################################################
#			 Question and answer.
############################################################################
#
cat << EOF > $TMP/tempmsg
Now we will attempt to configure your PCMCIA controller.
You can reconfigure your system at any time by typing:

  pcmciaconfig

EOF
dialog --title "PCMCIA CONFIGURATION" --msgbox "`cat $TMP/tempmsg`" 9 70

while : ;do
  dialog --menu "Select PCMCIA modules" 10 70 15 \
  built-in "Kernel built-in modules (recommended)" \
  pcmcia-cs "External pcmcia-cs modules (sometimes needed)" \
  none "No PCMCIA modules (PCMCIA disabled)" \
  2> /tmp/return

  if [ $? = 1 -o $? = 255 ]; then
    rm -f $TMP/tempmsg /tmp/return
    exit 1;
  fi

  ret=`cat /tmp/return`

  if [ "$ret" = "built-in" ]; then
    rm -f etc/rc.modules.d/pcmcia-cs

    cat << EOF > etc/rc.modules.d/pcmcia
# This script is automatically generated by the pcmciaconfig

/sbin/pcmcia start

EOF
    chmod 755 etc/rc.modules.d/pcmcia
    break;
  elif [ "$ret" = "pcmcia-cs" ]; then
    if [ -f /sbin/pcmcia-cs-enable ]; then
      cat << EOF > etc/rc.modules.d/pcmcia-cs
# This script is automatically generated by the pcmciaconfig

/sbin/pcmcia-cs-enable

EOF
      cat << EOF > etc/rc.modules.d/pcmcia
# This script is automatically generated by the pcmciaconfig

/sbin/pcmcia start

EOF
      chmod 755 etc/rc.modules.d/pcmcia
      chmod 755 etc/rc.modules.d/pcmcia-cs
      break;
    else
      dialog --title "NOT INSTALLED" --msgbox "Sorry, package 'pcmcia-cs-modules' is not installed.." 6 50
    fi
  else
    rm -f etc/rc.modules.d/pcmcia-cs etc/rc.modules.d/pcmcia
    break;
  fi
done

rm -f $TMP/tempmsg /tmp/return

dialog --title "REBOOT NEEDED" --msgbox "To apply the changes you've made, please, reboot." 6 50
