#!/bin/bash
# diskcomp.sh v1.0
# This script is copyright by Patrick Lambert <drow@darkelf.net>
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Layout for this script was based on Slackware's /sbin/setup
#
TMP=/tmp
DIALOG=dialog
EXPR=expr
TAR=tar
DD=dd
AWK=awk
DRIVE=/dev/fd0
IDEA=idea
TITLE="DiskComp utility"

reset
rm -f $TMP/.diskcomp*

menu()
{
$DIALOG --title "$TITLE" --menu "Disk based compression and encryption \
utility" 12 75 4 "1" "Compress a directory to diskettes" "2" "Uncompress \
diskettes" "H" "Help" "X" "Exit" 2> $TMP/.diskcomp

util

if [ "$CHOICE" = "1" ]; then
 dc_compress
fi

if [ "$CHOICE" = "2" ]; then
 dc_uncompress
fi

if [ "$CHOICE" = "H" ]; then
 $DIALOG --sleep 5 --title "$TITLE" --infobox "DiskComp by Patrick \
Lambert\nEdit this file for copyright \
notice." 8 75
fi

if [ "$CHOICE" = "X" ]; then
 reset
 clear
 rm -f $TMP/.diskcomp*
 exit
fi

menu
}

util()
{
if [ $? = 1 -o $? = 255 ]; then
 exit
fi
CHOICE="`cat $TMP/.diskcomp`"
}

util2()
{
if [ $? = 1 -o $? = 255 ]; then
 menu
fi
CHOICE="`cat $TMP/.diskcomp`"
}

dc_uncompress()
{
$DIALOG --title "$TITLE" --menu "Where should I take the archive?" 10 75 3 \
"1" "Mount $DRIVE to /mnt (root)" "2" "Mount /mnt (user)" "3" "Don't mount \
and read from current dir" 2> $TMP/.diskcomp
util2
if [ "$CHOICE" = "1" ]; then
 DOIMOUNT=1
 ISUSER=0
 MOUNTPOINT=/mnt
fi
if [ "$CHOICE" = "2" ]; then
 DOIMOUNT=1
 ISUSER=1
 MOUNTPOINT=/mnt
fi
if [ "$CHOICE" = "3" ]; then
 DOIMOUNT=0
 MOUNTPOINT=.
fi
$DIALOG --title "$TITLE" --inputbox "Where do you want the files to go?" 8 \
75 "." 2> $TMP/.diskcomp
util2
DEST=$CHOICE

CONTINUE=1
CURRENT=1

rm -f $TMP/.diskcomp.tgz
dc_uncompress2
$DIALOG --title "$TITLE" --infobox "Uncompressing into $DEST" 8 75
cd $DEST
$TAR -zxf $TMP/.diskcomp.tgz
menu
}

dc_uncompress2()
{
if [ "$DOIMOUNT" = "1" ]; then
 $DIALOG --title "$TITLE" --menu "Enter diskette $CURRENT into $DRIVE" 8 75 1 \
 "OK" "Enter to continue" 2> $TMP/.diskcomp
 util2
fi
read_file
if [ "$CONTINUE" = "1" ]; then
 dc_uncompress2
fi
}

read_file()
{
$DIALOG --sleep 2 --title "$TITLE" --infobox "Reading $CURRENT" 8 75
if [ "$DOIMOUNT" = "1" ]; then
 if [ "$ISUSER" = "0" ]; then
  mount $DRIVE $MOUNTPOINT
 fi
 if [ "$ISUSER" = "1" ]; then
  mount $MOUNTPOINT
 fi
fi
rm -f $TMP/.diskcomp
if [ -e $MOUNTPOINT/disk$CURRENT ]; then
 cat $MOUNTPOINT/disk$CURRENT >> $TMP/.diskcomp.tgz
 WROTE=`ls -l $MOUNTPOINT/disk$CURRENT | $AWK '{print $5}'`
 if [ "$WROTE" != "1310720" ]; then
  CONTINUE=0
 fi
 CURRENT=`$EXPR $CURRENT + 1`
fi
if [ "$DOIMOUNT" = "1" ]; then
 umount $MOUNTPOINT
fi
}

dc_compress()
{
$DIALOG --title "$TITLE" --inputbox "Which directory do you want to \
compress?" 8 75 2> $TMP/.diskcomp
util2
$DIALOG --title "$TITLE" --infobox "Compressing `du -s $CHOICE` into \
$TMP\n\nPlease wait..." 8 75
$TAR -cpzf $TMP/.diskcomp.tgz $CHOICE
$DIALOG --title "$TITLE" --menu "Where should I write this?" 10 75 3 \
"1" "Mount $DRIVE to /mnt (root)" "2" "Mount /mnt (user)" "3" "Don't \
mount and write to current dir" 2> $TMP/.diskcomp
util2
if [ "$CHOICE" = "1" ]; then
 DOIMOUNT=1
 ISUSER=0
 MOUNTPOINT=/mnt
fi
if [ "$CHOICE" = "2" ]; then
 DOIMOUNT=1
 ISUSER=1
 MOUNTPOINT=/mnt
fi
if [ "$CHOICE" = "3" ]; then
 DOIMOUNT=0
 MOUNTPOINT=.
fi

CURRENT=1
CONTINUE=1

dc_compress2
menu
}

dc_compress2()
{
if [ "$DOIMOUNT" = "1" ]; then
 $DIALOG --title "$TITLE" --menu "Enter empty diskette $CURRENT into $DRIVE" 8 75 1 \
 "OK" "Enter to continue" 2> $TMP/.diskcomp
 util2
fi
write_to_disk
if [ "$CONTINUE" = "1" ]; then
 dc_compress2
fi
}

write_to_disk()
{
$DIALOG --sleep 2 --title "$TITLE" --infobox "Writing $CURRENT" 8 75
if [ "$DOIMOUNT" = "1" ]; then
 if [ "$ISUSER" = "0" ]; then
  mount $DRIVE $MOUNTPOINT
 fi
 if [ "$ISUSER" = "1" ]; then
  mount $MOUNTPOINT
 fi
fi
TMPC=`$EXPR $CURRENT - 1`
rm -f $TMP/.diskcomp
$DD if=$TMP/.diskcomp.tgz of=$MOUNTPOINT/disk$CURRENT skip=`$EXPR \
$TMPC \* 5`b count=5b 2>$TMP/.diskcomp
RESULT=`cat $TMP/.diskcomp`
WROTE=`ls -l $MOUNTPOINT/disk$CURRENT | $AWK '{print $5}'`
if [ "$DOIMOUNT" = "1" ]; then
 umount $MOUNTPOINT
fi
if [ "$WROTE" != "1310720" ]; then
 CONTINUE=0
fi
$DIALOG --sleep 2 --title "$TITLE" --infobox "Wrote $WROTE\nContinue: \
$CONTINUE\ndd reports:\n$RESULT" 8 75
CURRENT=`$EXPR $CURRENT + 1`
}

menu
