#!/bin/sh

### BEGIN INIT INFO
# Provides:          scancnf
# Required-Start:    
# Required-Stop:     
# Default-Start:     5
# Default-Stop:      
# Short-Description: Scan partition and read cocon.cnf, etc.
# Description:       
### END INIT INFO

DEFCONF="/usr/share/cocon/default.cnf"        # Factory cocon.cnf
SECCONF_EXTREME="/cocon.cnf"                  # Boot disk built-in cocon.cnf (Extreme mode only)
CONF_MOUNT="/mnt/cfg"                         # config store to (ramdisk)
FIRMWARE_DIR="/lib/firmware"                  # firmware directory
#LOGFILE="/var/log/scan-cnf-from-storage.log"
CNFREPO="/tmp/.cnfrepo"
BOOT_DEVICE_FILE="/tmp/.boot_device"

COCONCONF_PREFIX_DIR="/tmp/"
COCONCONF_PREFIX_FILE=".coconcnf-copy."



log_info_msg()
{ 
  #echo "$1" >> $LOGFILE
  echo "$1"
}


get_partition_type()
{
  fstype="$( blkid /dev/"$1" )"
  fstype="$( expr "$fstype" : '.*TYPE="\([A-Za-z0-9]*\)".*' )"
  echo "$fstype"
}


# Scan cocon.cnf, coconfrm
scan_coconcnf_files()
{
  userconfig_scanpath="$1"  # Almost in /mnt/cfg
  userconfig_realpath="$2"  # mount partition etc

  userconfig_storefile="$CNFFILE"

  if [ $# -ge 3 ];
  then
    userconfig_storefile="$3" # cocon.cnf store file (Default: $CNFFILE)
  fi

  log_info_msg "config scan path : $userconfig_realpath"
  log_info_msg "userconfig_scanpath : $userconfig_scanpath"
  log_info_msg "userconfig_storefile : $userconfig_storefile"

  copy_coconcnf_to_tmp "$userconfig_scanpath" "$userconfig_storefile"

  copy_firmware "$userconfig_scanpath"
}


copy_coconcnf_to_tmp()
{
  copy_from="$1/cocon.cnf"

  if [ -r "$copy_from" ];
  then
    copy_to="${COCONCONF_PREFIX_DIR}/${COCONCONF_PREFIX_FILE}$( busybox stat -c "%Y" "$copy_from" )-${RANDOM}"
    log_info_msg " --> cocon.cnf found ($copy_from -> $copy_to)"
    cp "$copy_from" "$copy_to"
  fi
}


copy_firmware()
{
  ALLOW_LOAD_FIRMWARE_B43="wl_apsta-3.130.20.0.o\|wl_apsta.o"
  ALLOW_LOAD_FIRMWARE_IPW2X00="ipw2100-1.3-i.fw\|ipw2100-1.3-p.fw\|ipw2100-1.3.fw\|ipw2200-bss.fw\|ipw2200-ibss.fw\|ipw2200-sniffer.fw"
  ALLOW_LOAD_FIRMWARE_P54="isl3886pci\|isl3886usb\|isl3887usb"

  # Non-redistributable Firmwares
  if [ -d "$1/coconfrm" ];
  then
    log_info_msg "--> firmware directory found"
    for frm in $1/coconfrm; do
      if expr "$frm" : "$ALLOW_LOAD_FIRMWARE_B43" > /dev/null ;
      then
        # Broadcom is big firmware file, so cut now.
        b43-fwcutter -w "$FIRMWARE_DIR" "$1/coconfrm/$frm"
        continue;
      fi

      if expr "$frm" : "$ALLOW_LOAD_FIRMWARE_IPW2X00\|$ALLOW_LOAD_FIRMWARE_P54" > /dev/null ;
      then
        # Just copy
        cp "$1/coconfrm/$frm" "/lib/firmware"
        continue;
      fi
    done
  fi
}

# factory setting : scan cocon.cnf only 
read_factory_coconcnf()
{
  # first, parse default config.
  if [ -f "$DEFCONF" ];
  then
    coconcnf_parse -c "$DEFCONF" -m "$CNFREPO"
  fi

  # If have cocon.cnf on root of this system, parse this.
  if [ -f "$SECCONF_EXTREME" ];
  then
    coconcnf_parse -c "$SECCONF_EXTREME" -m "$CNFREPO"
  fi
}

# Scan disks (except boot device) and pickup cocon.cnf and some files
scan_coconcnf_from_partiton()
{
  log_info_msg "diskstat is : "
  log_info_msg "$(cat /proc/diskstats)"

  while read maj min dev ext; do

    # Exclude ram and loop device
    #if ! echo "$arg" | grep -q loop;
    #then
    #  continue;
    #fi

    #if ! echo "$arg" | grep -q ram;
    #then
    #  continue;
    #fi

    log_info_msg "Searching: $dev"
 
    if [ -r "$BOOT_DEVICE_FILE" ];
    then
      boot_device_file="$(cat $BOOT_DEVICE_FILE)"
    else
      boot_device_file=""
    fi


    if [ "$ROOT_DEVICE" = "/dev/$dev" ] || [ "$boot_device_file" = "/dev/$dev" ];
    then
       # This is boot CD drive or USB Stick, HDD, etc.
       # then ignore this drive.
       log_info_msg " is booted device."
       continue;
    fi

    fstype="$( get_partition_type "$dev" )"

    if [ "$fstype" = "iso9660" ] || [ "$fstype" = "vfat" ] || [ "$fstype" = "ext3" ] || [ "$fstype" = "ntfs" ] || [ "$fstype" = "ext4" ];
    then
      if mount -o ro "/dev/$dev" "$CONF_MOUNT";
      then
        log_info_msg "Mounted to check: $dev"
        scan_coconcnf_files "$CONF_MOUNT" "/dev/$dev"
        sync
        umount -f "/dev/$dev"
      fi
    fi
  done < /proc/diskstats

  # read parsedcocon config files, and parse to repository
  find "${COCONCONF_PREFIX_DIR}" -name "${COCONCONF_PREFIX_FILE}*" -exec coconcnf_parse -c {} -m "$CNFREPO" \;
}


case "${1}" in
   start)
      log_info_msg "Starting $0"

      # Read opencocon default cocon.
      read_factory_coconcnf

      # Do scan and store
      scan_coconcnf_from_partiton

      # DEBUG
      #/bin/sh
      ;;

   *)
      echo "Usage: ${0} {start}"
      exit 1
      ;;
esac

exit 0

