#!/bin/sh
MOUNTPOINT="/mnt/floppy"
MD5_DIR=/usr/local/
MD5_ETC=etc.md5
MD5_USRLOCALETC=usr.local.etc.md5
MD5_ROOT=root.md5
#MD5_VAR=var.md5

echo "Trying to mount /dev/fd0 on ${MOUNTPOINT}..."
mount -t msdos /dev/fd0 ${MOUNTPOINT}
if [ $? = 0 ]; then
    echo "Ok."
else
    echo "failed. Exiting..."
    exit 1
fi;
TEMP=`/usr/bin/mktemp -t checklist`
menu() {
        dialog --menu "FreeSBIE Floppy Backup" 13 42 6 \
                E "Backup /etc" \
                L "Backup /usr/local/etc" \
                D "Backup /usr/local/etc/rc.d" \
                V "Backup /var" \
                R "Backup /root" \
                Q "Quit" \
        2> $TEMP
        case `cat $TEMP` in
                E)
				cd /
				[ -e tmp/etc_files.tmp ] && rm tmp/etc_files.tmp
                touch tmp/etc_files.tmp

				for i in `find etc -type f`
				do
					j=`md5 $i | awk '{print substr($0,5,length($0))}'`
					RESULT=`grep "${j}" $MD5_DIR/$MD5_ETC`
					if [ ! "${RESULT}" = "${j}" ]; then
						echo "${j}" | awk -F '=' '{print "/" substr($1,2,length($1)-3)}' >> /tmp/etc_files.tmp
					fi
				done
		
                tar jcvpf ${MOUNTPOINT}/etc.tar.bz2 -T tmp/etc_files.tmp
				rm tmp/etc_files.tmp
				;;
                
				L)
				cd /
				[ -e tmp/usrlocaletc_files.tmp ] && rm tmp/usrlocaletc_files.tmp
                touch tmp/usrlocaletc_files.tmp
				for i in `find usr/local/etc -type f`
				do
					j=`md5 $i | awk '{print substr($0,5,length($0))}'`
					RESULT=`grep "${j}" $MD5_DIR/$MD5_USRLOCALETC`
					if [ ! "${RESULT}" = "${j}" ]; then
						echo "${j}" | awk -F '=' '{print "/" substr($1,2,length($1)-3)}' >> /tmp/usrlocaletc_files.tmp
					fi
				done

				tar jcvpf ${MOUNTPOINT}/usrlocaletc.tar.bz2 -T /tmp/usrlocaletc_files.tmp
				rm tmp/usrlocaletc_files.tmp 
				;;
                
			D)
				cd /
				[ -e tmp/rc_files.tmp ] && rm tmp/rc_files.tmp
                touch tmp/rc_files.tmp

				for i in `ls usr/local/etc/rc.d`
				do
                        if [ -x $i ]; then
						echo "${i}" >> /tmp/rc_files.tmp
					fi
				done

                tar jcvpf ${MOUNTPOINT}/rc.tar.bz2 -T /tmp/rc_files.tmp
				rm tmp/rc_files.tmp 
                ;;
                
                V)
				cd /
				[ -e tmp/var_files.tmp ] && rm tmp/var_files.tmp
				touch tmp/var_files.tmp
				
				for i in `find var -type f`
				do
					j=`md5 $i | awk '{print substr($0,5,length($0))}'`
					RESULT=`grep "${j}" $MD5_DIR/$MD5_VAR`
					if [ ! "${RESULT}" = "${j}" ]; then
						echo "${j}" | awk -F '=' '{print "/" substr($1,2,length($1)-3)}' >> /tmp/var_files.tmp
					fi
				done

                tar jcvpf ${MOUNTPOINT}/var.tar.bz2 -T /var
				rm tmp/var_files.tmp 
                ;;

				R)
                cd /
				[ -e tmp/$root_files.tmp ] && rm tmp/root_files.tmp
                touch tmp/root_files.tmp
				
				for i in `find root -type f`
				do
					j=`md5 $i | awk '{print substr($0,5,length($0))}'`
					RESULT=`grep "${j}" $MD5_DIR/$MD5_ROOT`
					if [ ! "${RESULT}" = "${j}" ]; then
						echo "${j}" | awk -F '=' '{print substr($1,2,length($1)-3)}'>> /tmp/root_files.tmp
					fi
				done

                tar jcvpf ${MOUNTPOINT}/root.tar.bz2 -T /tmp/root_files.tmp
				rm tmp/root_files.tmp
                ;;

				Q)
                umount ${MOUNTPOINT}
                exit 0       
                ;;
				
                *)
                umount ${MOUNTPOINT}        
                exit 1        
        esac
        rm $TEMP
        menu        
}
menu
umount ${MOUNTPOINT}
