#!/bin/bash
# simple filelocking and /tmp race avoid
# keep it all in users home dir.

E3G_ROOT=$1
E3G_GUI_DEFAULT=$2

if [ -z "$E3G_ROOT" ]
then
  E3G_ROOT="/"
fi

if [ -z "$E3G_GUI_DEFAULT" ]
then

if [ ! -d $HOME/tmp ]; then
 mkdir $HOME/tmp
fi
ANSFILE=/tmp/e3g_hwcon
BACKTITLE="Euro 3G - Configurazione per HW specifico"

export PATH=/bin:/sbin:/usr/bin:/usr/sbin:$PATH
setvesablank off

cd /opt/e3g_hdinst

# This is the main dialog menu
dialog  --nocancel --begin 3 4 --backtitle "$BACKTITLE" \
        --title "Configurazioni per HW specifico" \
        --menu "\nScegliere il modello della macchina :" 21 72 14 \
"Default"     "Sistema generico capace di usare resoluzione 800x600" \
"Explora_950" "Olivetti Explor@ 950" \
"Asem_RT560"  "Asem POS Touch RT560" \
"Fine" "Uscire dalla selezione" 2>$ANSFILE
# now the routines
ANS=`cat $ANSFILE`

else
ANS=$E3G_GUI_DEFAULT

if [ "$ANS" = "Explora_950" ]
then
  touch $E3G_ROOT/opt/euro-beta/xrandr_scale
  sed -i 's/GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX="i8042.nomux=1 i8042.noloop=1 i8042.reset=1"/' $E3G_ROOT/etc/default/grub    
  echo "#!/bin/bash" >> $E3G_ROOT/opt/euro-beta/postinstall_script
  echo "update-grub" >> $E3G_ROOT/opt/euro-beta/postinstall_script
  chmod +x $E3G_ROOT/opt/euro-beta/postinstall_script
fi

if [ "$ANS" = "Default" ]
then
  rm $E3G_ROOT/opt/euro-beta/xrandr_scale
  sed -i 's/GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX="i8042.nomux=1"/' $E3G_ROOT/etc/default/grub
  echo "#!/bin/bash" >> $E3G_ROOT/opt/euro-beta/postinstall_script
  echo "update-grub" >> $E3G_ROOT/opt/euro-beta/postinstall_script
  chmod +x $E3G_ROOT/opt/euro-beta/postinstall_script
fi


if [ "$ANS" = "Asem_RT560" ]
then
  rm $E3G_ROOT/opt/euro-beta/xrandr_scale
  sed -i 's/GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX="i8042.nomux=1"/' $E3G_ROOT/etc/default/grub
  sed -i 's/title=.*$/title=SpinTec Euro3G/' $E3G_ROOT/lib/plymouth/themes/lubuntu-text/lubuntu-text.plymouth
  sed -i 's|/opt/euro-beta/start3g.*$|/opt/euro-beta/start3g -- -dpi 72|' $E3G_ROOT/etc/init/euro3g.conf


  echo "#!/bin/bash" >> $E3G_ROOT/opt/euro-beta/postinstall_script
  echo "update-grub" >> $E3G_ROOT/opt/euro-beta/postinstall_script
  echo "update-initramfs -u" >> $E3G_ROOT/opt/euro-beta/postinstall_script
  chmod +x $E3G_ROOT/opt/euro-beta/postinstall_script
fi

fi