#!/usr/bin/env bash
#   !/bin/bash
# saveconfig - create KNOPPIX configuration floppy
# (C) Klaus Knopper Mar 2002
#
# modify by kinneko 2003/01/29
# (C) yet another knoppix-jp Porject 2003
#     http://sourceforge.jp/projects/ya-knoppix-jp
#
# changelog
#  0.9test
#    Add VERSION
#    gettextize messages
#      set TEXTDOMAINDIR TEXTDOMAIN current dir. ( testing only )

# Script Version
VERSION="0.9test"

# set language file to get local.
[ x"$TEXTDOMAIN" = x"saveconfig" ] || TEXTDOMAINDIR=. TEXTDOMAIN=saveconfig exec "$0"

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

XDIALOG_HIGH_DIALOG_COMPAT=1
export XDIALOG_HIGH_DIALOG_COMPAT

# Get root
[ "`id -u`" != "0" ] && exec sudo "$0" "$@"

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

bailout(){
rm -f "$TMP" "$TMP.done" "$TMP.err"
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}')"

#[ ! -n "$DISPLAY" ] && case "$LANGUAGE" in ja*) LANGUAGE=en ;; esac

# Messages
TITLE1=$"Create KNOPPIX configuration floppy"
MESSAGE1=$"Chose type of configuration files:"
MESSAGE2=$"Please insert an empty DOS- or ext2-formatted, writable floppy disk."
MESSAGE3=$"Saving to floppy..."
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 ("
E6=$"kB)"
SUCCESS=$"Creation of KNOPPIX configuration floppy was successful. Your configuration files will be reinstalled to the ramdisk on next KNOPPIX boot if you specify \"   knoppix floppyconf\" at the boot prompt."
ERROR=$"The KNOPPIX configuration floppy could NOT be created:"

$DIALOG --clear --title "$TITLE1 $VERSION" --checklist "$MESSAGE1" 18 75 9 p "$E1" on d "$E5 $DESKTOPKB $E6" off n "$E2" on x "$E3" on o "$E4" on 2>"$TMP" || bailout

FILES=""
SAVEHOME=""
SAVEDESK=""
SAVEETC=""
SAVENET=""
SAVEX=""
CHOWNHOME=""
NETFILES='/etc/network|/etc/ppp|/etc/chat|/etc/isdn|/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" ;;
n|x|o) SAVEETC="yes"; case "$i" in n) SAVENET="yes";; x) SAVEX="yes";; esac ;;
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"
tar -jpPtf "\$CONFIGS" | while read i; do rm -f "\$i"; done
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
}

KNOPPIXSH="$FLOPPYDIR/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

# Just to be on the safe side
umountfloppy

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"

[ -n "$SAVEHOME" ] && for i in $HOME/.??* $HOME/office; 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"

IGNORE='/etc/ioctl.save'

if [ -n "$SAVEETC" -a -d /KNOPPIX/etc ]; then
findchanged /etc /KNOPPIX/etc >>"$TMP"
[ -n "$SAVENET" ] || IGNORE="$IGNORE|$NETFILES"
[ -n "$SAVEX" ] || IGNORE="$IGNORE|$XFILES"
fi

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

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

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


[ -n "$STARTNET" ] && { echo "pump -k 2>/dev/null ; sleep 1" >>"$KNOPPIXSH" ; SCRIPTS="$SCRIPTS networking"; }
[ -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"

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

RC="$?"

umountfloppy

# 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"

[ "$RC" = "0" ] && $DIALOG --title "$TITLE1" --msgbox "$SUCCESS" 10 65 || $DIALOG --title "$TITLE1" --msgbox "$ERROR `cat $TMP.err`" 10 75

bailout
