#!/bin/ksh

#========================================
#
# fdadm - Floppy Disk ADMinistration tool
# KAWAMATA, Yoshihiro / kaw@on.rim.or.jp
#
#========================================

# Copyright (c) 2006--2025 Yoshihiro Kawamata
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 
#   * Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
# 
#   * Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in
#     the documentation and/or other materials provided with the
#     distribution.
# 
#   * Neither the name of the Yoshihiro Kawamata nor the names of its
#     contributors may be used to endorse or promote products derived
#     from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "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 COPYRIGHT
# OWNER OR CONTRIBUTORS 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.


echo 'Welcome to Floppy Disk maintenance tool.'
echo ''
echo 'Type ? for help.'

#-------------------
# command loop
#
while :; do
    #-------------------
    # prompting
    #
    echo ''
    echo -n "Cmd -> "; read cmd
    echo ''

    set X $cmd

    #-------------------
    # process every command
    #
    case X"$2" in
        #-------------------
        # make new FFS file system
        # on floppy disk, /dev/fd0c
        #
        Xmkfs)
        fdformat /dev/rfd0c

        echo -n "newfs /dev/rfd0c, OK? [y/n] -> "; read yn
        if [ 1 = `expr "$yn" : '[Yy]$'` ]; then
            disklabel -w fd0 floppy
            newfs /dev/rfd0c
        fi
        ;;

        #-------------------
        # make list of modified files
        #
        Xmklist)
        echo -n "Making list of modified files ..."
        (cd /ram
         find -x * -type f -cnewer /boottmp/boot_starts -print   \
         | awk '{print "cmp -s", $1, "/fuguita/" $1, "|| echo", $1}' \
         | sh
         find -x * -type l -cnewer /boottmp/boot_livecd_rc_ends -print) | sort > /tmp/fdfiles
        echo " done."
        ;;

        #-------------------
        # edit list of modified files
        #
        Xedlist)
            ${EDITOR:-vi} /tmp/fdfiles
        ;;

        #-------------------
        # write back files listed in /tmp/fdfiles
        # to floppy disk
        #
        Xwrite)
        echo -n "Write list of files into /mnt/livecd-config.tar.gz , OK? [y/n] -> "; read yn
        if [ 1 = `expr "$yn" : '[Yy]$'` ]; then
            if mount /dev/fd0c /mnt; then
                (cd /ram && tar -cvzp -f /mnt/livecd-config.tar.gz -I /tmp/fdfiles)
                cp /boottmp/livecd-retr.sh.inc /mnt
                umount /mnt
            else
                echo "mounting /dev/fd0c failed"
            fi
        fi
        ;;

        #-------------------
        # finish all
        #
        Xquit|Xbye|Xexit)
        rm -f /tmp/fdfiles
        break;
        ;;

        #-------------------
        # null command
        # ... only RET or EOF
        X)
        : # do nothing
        ;;

        #-------------------
        # other strings are invalid
        # then for help message
        #
        *)
        echo "Commands are;
    mkfs    -  make FFS on /dev/rfd0c
    mklist  -  make list of modified files
    edlist  -  edit list of modified files
    write   -  write list files to floppy
    bye, exit, quit
            - end of this utility"
        ;;
    esac
done
