#!/bin/bash
# saveconfig - create KNOPPIX configuration floppy
# (C) Klaus Knopper Mar 2002
# Spanish translation by Luis Llorente, luis.llorente@hispalinux.es

PATH="/bin:/sbin:/usr/bin:/usr/sbin"
export PATH

XDIALOG_HIGH_DIALOG_COMPAT=1
export XDIALOG_HIGH_DIALOG_COMPAT

# Get root
# K.Wonderboy ; 1.12 s    (13)  1
#[ "`id -u`" != "0" ] && exec sudo "$0" "$@"
[ "`id -u`" != "0" ] && sudo -v && exec sudo "$0" "$@"
RUSER=$(ls -ld $HOME | awk '{print $3}')
#fi
# K.Wonderboy ; 1.12 e

TMP="/tmp/saveconfig.tmp$$"

bailout(){
rm -f "$TMP" "$TMP.done" "$TMP.err"
umount "$DIRECTORY"
exit 0
}

DIALOG="dialog"
[ -n "$DISPLAY" ] && [ -x /usr/bin/Xdialog ] && DIALOG="Xdialog"

trap bailout 1 2 3 15

# LANGUAGE etc.
[ -f /etc/sysconfig/knoppix ] && . /etc/sysconfig/knoppix
[ -z "$LANG" ] && export LANG
[ -z "$LANGUAGE" ] && export LANGUAGE
[ -z "$CHARSET" ] && export CHARSET

DESKTOPKB="$(du -sk $HOME/Desktop 2>/dev/null | awk '{print $1}')"

# Language-dependent Messages
case "$LANGUAGE" in
de*|at*|ch*)
TITLE1="KNOPPIX Konfigurationsarchiv anlegen"
MESSAGE1="Konfigurationsdateien ausw?len:"
MESSAGE2="Bitte legen Sie jetzt eine leere DOS- oder ext2-formatierte, schreibbare Diskette ein."
MESSAGE3="Archiviere ge?derte Konfigurationsdateien..."
MESSAGE4="Bitte w?len Sie das Verzeichnis, in das Sie die Konfigurationsdateien schreiben wollen."
E1="Pers?liche Einstellungen (Desktop, Programme)"
E2="Netzwerk Einstellungen (LAN, Modem, ISDN, ASDL)"
E3="Grafik Subsystem Einstellungen (XF86Config)"
E4="Weitere systemweite Einstellungen (Drucker etc.)"
E5="Alle Desktop-Dateien (${DESKTOPKB}kB)"
ERROR="Leider konnte die KNOPPIX-Konfiguration NICHT gespeichert werden:"
;;
es*)
TITLE1="Crear un archive de configuraci? de KNOPPIX"
MESSAGE1="Elija el tipo de archivos de configuraci?:"
MESSAGE2="Por favor, inserte un disquete vac? escribible formateado para DOS o ext2."
MESSAGE3="Grabando archivos de la configuraci?..."
MESSAGE4="Por favor, seleccione el directorio para los archivos de la configuraci? del ahorro."
E1="Configuraci? personal (escritorio, programas)"
E2="Configuraci? de Red (LAN, Modem, RDSI, ADSL)"
E3="Configuraci? del subsistema gr?ico (XF86Config)"
E4="Otras configuraciones del sistema (impresora, etc.)"
E5="Todos los archivos en el Escritorio (${DESKTOPKB}kB)"
ERROR="No se pudo crear de configuraci? de KNOPPIX:"
;;
# K.Wonderboy ; 1.12 s    2
ja*)
TITLE1="ǥ쥯ȥꡧ$HOME γƼ"
MESSAGE1="򤹤եμǲ"
MESSAGE2="MSDOSext2ǽ줿Υեåԡ򥻥åȤƲ"
MESSAGE3="򤷤ե뷲..."
MESSAGE4="Υǥ쥯ȥꤷƲ:"
E1="Ŀե (ǥȥå,ץ)"
E2="ͥåȥ (LAN, Modem, ISDN, ADSL)"
E3="եå (XF86Config,X11)"
E4="¾ƥ (printer,init.dʤ)"
E5="ǥȥå׾Υե ߤ̡(${DESKTOPKB}kB)"
ERROR="ե뤳ȤǤޤ"
HDINST="򤹤եμǲ"
HDINST1="Ŀ(p)ȥǥȥå(d) ʳ򤹤ˤ  root ǥ󤷤Ʋ"
HDINST2="ե򤹤ˡǲ
----------------------------------------------------------------------------------- \
/etc/skelƱեdefault) = yes  \
եƤޤ뤴򤹤    = no     
-----------------------------------------------------------------------------------              
OS졼ɥå׸ʤɤ򤷤եǤưݾڤޤ
¿ξ硢/etc/skel ϥƥϢưƿʤޤ"
HDINST3="ե (x),(o) 򤵤Ƥޤ  \
ϡƱСOSǹԤʤäƲ  \
OS졼ɥå׸ȥƥबưǽˤʤޤ"
MNTERR="HD󥹥ȡ벼ǳƥ桼Ǥμ¹Ԥξ \
ѡ桼¤ʤ VFATFloppyʳ  \
ѡƥؤν񤭹ߤϤǤޤ"
;;
# K.Wonderboy ; 1.12 e
*)
TITLE1="Create KNOPPIX configuration archive"
MESSAGE1="Chose type of configuration files:"
MESSAGE2="Please insert an empty DOS- or ext2-formatted, writable floppy disk."
MESSAGE3="Saving configuration archive..."
MESSAGE4="Please select directory for saving configuration files:"
E1="Personal configuration (desktop, programs)"
E2="Network settings (LAN, Modem, ISDN, ADSL)"
E3="Graphics subsystem settings (XF86Config)"
E4="Other system configuration (printer etc.)"
E5="All files on the Desktop (${DESKTOPKB}kB)"
ERROR="The KNOPPIX configuration could NOT be saved:"
;;
esac

# K.Wonderboy ; 1.12 s      3
COPYTYPE=0
if [ ! -d /KNOPPIX/etc ]; then
  case "$LANGUAGE" in
    ja*)
    ;;
    *)
      MNTERR="$ERROR"
    ;;
   esac 
  if [ "`id -u`" = "0" ]; then
    $DIALOG --clear --title "$TITLE1" --checklist "$HDINST" 18 75 9 p "$E1" on d "$E5" on n "$E2" off x "$E3" off o "$E4" off 2>"$TMP" || exit 0
  else
    HDINST="$HDINST           $HDINST1"
    $DIALOG --clear --title "$TITLE1" --checklist "$HDINST" 18 75 9 p "$E1" on d "$E5" on  2>"$TMP" || exit 0
  fi
  KWEX1=""
  KWEX2=""
  for i in `sed 's,",,g;s,/, ,g;' < "$TMP"`; do
    case $i in p|d) KWEX1="yes";; x|o) KWEX2="yes";; esac
  done
  if [ "$KWEX1" = "yes" ]; then
    $DIALOG --title "$TITLE1" --yesno "$HDINST2" 18 75 
    COPYTYPE=$?
  fi
  if [ "$KWEX2" = "yes" ]; then
    $DIALOG --title "$TITLE1" --yesno "$HDINST3" 18 65 || exit 0
  fi
else
$DIALOG --clear --title "$TITLE1" --checklist "$MESSAGE1" 18 75 9 p "$E1" on d "$E5" off n "$E2" on x "$E3" on o "$E4" on 2>"$TMP" || exit 0
fi
#$DIALOG --clear --title "$TITLE1" --checklist "$MESSAGE1" 18 75 9 p "$E1" on d "$E5" off n "$E2" on x "$E3" on o "$E4" on 2>"$TMP" || bailout
# K.Wonderboy ; 1.12 e

FILES=""
SAVEHOME=""
SAVEDESK=""
SAVEETC=""
SAVENET=""
SAVEX=""
CHOWNHOME=""
NETFILES='/etc/network|/etc/ppp|/etc/chat|/etc/isdn|/etc/sysconfig/isdn|/etc/sysconfig/provider|/etc/resolv\.conf|/etc/hosts|/etc/hosts\.|/etc/inetd\.conf'
XFILES='/etc/X11'
for i in `sed 's,",,g;s,/, ,g;' < "$TMP"`; do
case $i in
p) SAVEHOME="yes" ;;
d) SAVEDESK="yes" ;;
# K.Wonderboy ; 1.12 s           4  
#n|x|o) SAVEETC="yes"; case "$i" in n) SAVENET="yes";; x) SAVEX="yes";; esac ;;
n) SAVENET="yes" ;; 
x) SAVEX="yes" ;; 
o) SAVEETC="yes" ;;
# K.Wonderboy ; 1.12 e
esac
done

[ -n "$SAVEHOME" -o -n "$SAVEDESK" ] && CHOWNHOME="chown -R $(ls -ld $HOME | awk '{print $3"."$4}') $HOME"

# Find changes in (real) files of dir1 ... dir2
findchanged(){
if [ -d "$1" ]; then
for i in `( cd "$1"; find . -type f | sed 's,^\./,,g' | grep -v ' ' )`; do
cmp -s "$1/$i" "$2/$i" || echo "$1/$i"
done
elif [ -e "$1" ]; then
cmp -s "$1" "$2" || echo "$1"
fi
}

writescript(){
cat >"$1" <<EOT
#!/bin/sh
[ "\`id -u\`" = "0" ] || { echo "You need root privileges to modify the system!" >&2 ; exit 1; }
[ -d "\$1" ] && CONFIGS="\$1/configs.tbz"
[ -f "\$CONFIGS" ] || CONFIGS="/cdrom/KNOPPIX/configs.tbz"
[ -f "\$CONFIGS" ] || CONFIGS="/mnt/floppy/configs.tbz"
if [ -f "\$CONFIGS" ]; then
echo "[1mExtracting config archive \$CONFIGS...[0m"
# K.Wonderboy ; 1.12 s          5
tar -jpPtf "\$CONFIGS" | while read i; do rm -f "\$i"; done
#tar -jpPtf "\$CONFIGS" | while read i; do rm -rfv "\$i"; done
# K.Wonderboy ; 1.12 e
tar -jpPxf "\$CONFIGS" ; $CHOWNHOME
fi
EOT
return "$?"
}

FLOPPYDIR="$(ls -l /mnt/floppy | awk '{print $NF}')"

umountfloppy(){
mount | grep -q "$FLOPPYDIR" && umount "$FLOPPYDIR" 2>/dev/null
}

# Directory selector
PARTITIONS="/mnt/floppy [Floppy] on"
for i in `awk '/^\/dev\/[hs]d[a-z].*\/mnt\/[hs]d[a-z]/{if(!/ntfs/){print $2}}' /etc/fstab`; do
PARTITIONS="$PARTITIONS ${i} [Disk/Partition] off"
done


rm -f "$TMP"
$DIALOG --clear --title "$TITLE1" --radiolist "$MESSAGE4" 18 75 9 $PARTITIONS 2>"$TMP" || bailout

DIRECTORY="$(<$TMP)"
[ -z "$DIRECTORY" -o ! -e "$DIRECTORY" ] && bailout

case "$DIRECTORY" in *floppy*)
DIRECTORY="$FLOPPYDIR"
KNOPPIXSH="$DIRECTORY/knoppix.sh"
while :; do
umountfloppy
$DIALOG --title "$TITLE1" --yesno "$MESSAGE2" 8 65 || bailout
echo "$FLOPPYDIR" | grep -q auto || mount /mnt/floppy
[ "$?" = "0" ] && writescript "$KNOPPIXSH" && break
done
;;
*)
rm -f "$TMP.err"
mount | grep -q "$DIRECTORY" || mount -r "$DIRECTORY" >"$TMP.err"
[ "$?" != "0" ] && { $DIALOG --title "$TITLE1" --msgbox "$ERROR `cat $TMP.err`" 10 75; bailout; }
mount | grep -q "$DIRECTORY.*ntfs" && { $DIALOG --title "$TITLE1" --msgbox "$ERROR NTFS" 10 75; bailout; }
mount -o remount,rw "$DIRECTORY" 
# K.Wonderboy ; 1.12 s        6
if [ "$?" != "0" ]; then
  if [ ! -d /KNOPPIX/etc ]; then
     $DIALOG --title "$TITLE1" --msgbox "$MNTERR" 10 75; bailout
  fi
fi
# K.Wonderboy ; 1.12 e
KNOPPIXSH="$DIRECTORY/knoppix.sh"
writescript "$KNOPPIXSH"
# K.Wonderboy ; 1.12 s         7
chmod 755 $DIRECTORY/knoppix.sh
# K.Wonderboy ; 1.12 e
esac

gpid=""

gauge(){
status=0
while [ ! -e "$TMP.done" ]; do echo "$status" ; status="`expr \( 100 - $status \) / 4 + $status`"; sleep 2; done | $DIALOG --title "$TITLE1" --gauge "$MESSAGE3" 8 65 0
}

# Start status bar
# if [ "$DIALOG" = "dialog" ]; then
gauge &
# else
## Gauge doesn't work yet with gdialog
# $DIALOG --title "$TITLE1" --gauge "$MESSAGE3" 8 65 75 &
# gpid="$!"
# fi

rm -f "$TMP"
touch "$TMP"

# K.Wonderboy ; 1.12 s         8
if [ $COPYTYPE = 0 ] ; then
   [ -n "$SAVEHOME" ] && for i in $HOME/* $HOME/.??*; do findchanged "$i" /etc/skel/$(basename "$i"); done | egrep -v -e '(\Desktop|\Desktop/*|\.ICEauthority|ksycoka|\.xsession-errors|\.DCOP|\.MCOP|kio_http/cache|\.mozilla/.*/Cache|favicons|office/user/work|\.tumbnails)' >"$TMP"
   if [ $HOME = "/root" ]; then
     [ -n "$SAVEDESK" ] && for i in $HOME/Desktop; do echo "$i"; done >>"$TMP"
   else
     [ -n "$SAVEDESK" ] && for i in $HOME/Desktop; do findchanged "$i" /etc/skel/$(basename "$i"); done >>"$TMP"
   fi
else
  [ -n "$SAVEHOME" ] && for i in $HOME/* $HOME/.??*; do echo "$i"; done | egrep -v -e '(\Desktop|\Desktop/*|\.ICEauthority|ksycoka|\.xsession-errors|\.DCOP|\.MCOP|kio_http/cache|\.mozilla/.*/Cache|favicons|office/user/work|\.thumbnails)' >"$TMP"
  [ -n "$SAVEDESK" ] && for i in $HOME/Desktop; do echo "$i"; done >>"$TMP"
fi
#[ -n "$SAVEHOME" ] && for i in $HOME/.??* $HOME/office $HOME/evolution; do findchanged "$i" /etc/skel/$(basename "$i"); done | egrep -v -e '(\.ICEauthority|ksycoka|\.xsession-errors|\.DCOP|\.MCOP|kio_http/cache|\.mozilla/.*/Cache/|favicons|office/user/work)' >"$TMP"
#[ -n "$SAVEDESK" ] && for i in $HOME/Desktop; do findchanged "$i" /etc/skel/$(basename "$i"); done >>"$TMP"
# K.Wonderboy ; 1.12 e

IGNORE='/etc/ioctl.save|/etc/mtab'

# K.Wonderboy ; 1.12 s        9
#if [ -n "$SAVEETC" -a -d /KNOPPIX/etc ]; then
#findchanged /etc /KNOPPIX/etc >>"$TMP"
#[ -n "$SAVENET" ] || IGNORE="$IGNORE|$NETFILES"
#[ -n "$SAVEX" ] || IGNORE="$IGNORE|$XFILES"
#fi
if [ -d /KNOPPIX/etc ]; then
   [ -n "$SAVENET" ] && SAVEETC="yes"SAVEETC
   [ -n "$SAVEX" ] && SAVEETC="yes"    
   if [ -n "$SAVEETC" -a -d /KNOPPIX/etc ]; then
      findchanged /etc /KNOPPIX/etc >>"$TMP"
      [ -n "$SAVENET" ] || IGNORE="$IGNORE|$NETFILES"
      [ -n "$SAVEX" ] || IGNORE="$IGNORE|$XFILES"
  fi
else
  if [ -n "$SAVEETC" ]; then
      for i in /etc; do echo "$i"; done >>"$TMP"
      [ -n "$SAVENET" ] || IGNORE="$IGNORE|$NETFILES"
      [ -n "$SAVEX" ] || IGNORE="$IGNORE|$XFILES"
  else
      rm -f "$TMP.tmp" 
      touch "$TMP.tmp"
      [ -n "$SAVENET" ] && for i in /etc /etc/*; do echo "$i"; done > "$TMP.tmp"
      egrep -e '('"$NETFILES"')' "$TMP.tmp" >>"$TMP"
      rm -f "$TMP.tmp" 
      [ -n "$SAVEX" ] && for i in $XFILES; do echo "$i"; done >>"$TMP"
  fi
fi
# K.Wonderboy ; 1.12 e

rm -f "$TMP.tmp"
egrep -v -e '('"$IGNORE"')' "$TMP" > "$TMP.tmp"
mv -f "$TMP.tmp" "$TMP"

SCRIPTS=""
STARTNET=""
STARTPCMCIA=""
STARTCUPS=""
REINIT=""

while read i; do
case "$i" in
/etc/network/interfaces) STARTNET="yes";;
/etc/pcmcia/*) STARTPCMCIA="yes";;
/etc/cups*) STARTCUPS="yes";;
/etc/inittab) REINIT="yes";;
esac
done <"$TMP"

# K.Wonderboy ; 1.12 s        10
if [ -d /KNOPPIX/etc ]; then 
# K.Wonderboy ; 1.12 e
[ -n "$STARTNET" ]    && { echo "killall pump 2>/dev/null && sleep 2 && killall -9 pump 2>/dev/null && sleep 2" >>"$KNOPPIXSH" ; SCRIPTS="$SCRIPTS ifupdown networking"; }
[ -n "$STARTPCMCIA" ] && echo 'echo "[1m(Re)starting PCMCIA services.[0m"; killall cardmgr 2>/dev/null && sleep 4; cardmgr && sleep 4' >>"$KNOPPIXSH"
[ -n "$STARTCUPS" ]   && SCRIPTS="$SCRIPTS cupsys"

if [ -n "$SCRIPTS" ]; then
echo 'echo "[1mStarting daemons...[0m"' >>"$KNOPPIXSH"
echo "for i in $SCRIPTS; do [ -x /etc/init.d/\$i ] && /etc/init.d/\$i start; done" >>"$KNOPPIXSH"
fi

[ -n "$REINIT" ] && echo 'echo "[1mReloading INIT.[0m" ; init q' >> "$KNOPPIXSH"
# K.Wonderboy ; 1.12 s       11
fi
# K.Wonderboy ; 1.12 e

rm -f "$TMP.err"
BZIP2=-9 tar -T - -cpPjf "$DIRECTORY/configs.tbz" <"$TMP" 2>"$TMP.err"

RC="$?"

if [ "$DIRECTORY" = "$FLOPPYDIR" ]; then
umountfloppy
else
umount "$DIRECTORY"
fi

# More language-dependent Messages
case "$LANGUAGE" in
de*|at*|ch*)
SUCCESS="Die KNOPPIX-Konfiguration wurde erfolgreich gespeichert. Ihre Konfigurationsdateien werden beim n?hsten KNOPPIX-Bootvorgang in die Ramdisk restauriert, wenn Sie im Bootscreen \"knoppix myconfig=$DIRECTORY\" bzw. \"knoppix myconfig=scan\" (automatische Suche) angeben."
;;
es*)
SUCCESS="La creaci? del configuraci? de KNOPPIX fue satisfactoria. Sus archivos de configuraci? ser? reinstalados en el siguiente arranque de KNOPPIX si especifica \"knoppix myconfig=$DIRECTORY\" o \"knoppix myconfig=scan\" (automatic) en la l?ea de comandos del arranque."
;;
# K.Wonderboy ; 1.12 s       12
ja*)
if [ ! -d /KNOPPIX/etc ]; then
SUCCESS="桼$RUSER ե򤷤ޤ  \
ϡHDεư knoppix 2 ƥȥ⡼ɤǵư  \
 $DIRECTORY/knoppix.sh  $DIRECTORY Ǽ¹Ԥ  \
reboot(Ƶư)ޤ"
else
### following lines, mnt/auto/floppy can not enabled "knoppix-autoconfig" 
   if  [ "$DIRECTORY" = "$FLOPPYDIR" ]; then
SUCCESS="桼$RUSER եλޤ  \
CDεưξ硢ư knoppix floppyconf ץ  \
ͿƵư¸꤬ޤ"      
   else
SUCCESS="桼$RUSER եλޤ  \
CDεưξ硢ư knoppix myconfig=$DIRECTORY   \
knoppix myconfig=scanʼưõ˥ץͿƵư  \
¸꤬ޤ"
fi
fi
;;
# K.Wonderboy ; 1.12 e
*)
SUCCESS="Creation of the KNOPPIX configuration archive was successful. Your configuration files will be reinstalled to the ramdisk on next KNOPPIX boot if you specify \"knoppix myconfig=$DIRECTORY\", or \"knoppix myconfig=scan\" (automatic search) at the boot prompt."
;;
esac


# Stop status bar
# [ "$DIALOG" = "dialog" ] && { touch "$TMP.done" ; wait ; rm -f "$TMP.done" ; } || { [ -n "$gpid" ] && kill "$gpid" 2>/dev/null; sleep 1; }
touch "$TMP.done" ; wait ; rm -f "$TMP.done"

# K.Wonderboy ; 1.12 s       13
#[ "$RC" = "0" ] && $DIALOG --title "$TITLE1" --msgbox "$SUCCESS" 12 65 || $DIALOG --title "$TITLE1" --msgbox "$ERROR `cat $TMP.err`" 10 75
[ "$RC" = "0" ] && $DIALOG --title "$TITLE1" --msgbox "$SUCCESS" 15 70 || $DIALOG --title "$TITLE1" --msgbox "$ERROR `cat $TMP.err`" 10 75
# K.Wonderboy ; 1.12 e

bailout
