#!/bin/sh

### BEGIN INIT INFO
# Provides:          scan-cnf-from-storage
# Required-Start:    
# Required-Stop:     
# Default-Start:     S
# Default-Stop:      0 1 6
# 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)
SECCONF="/mnt/oldroot/mnt/copytoram"
CONF_MOUNT="/mnt/cfg"                         # config store to (ramdisk)
FIRMWARE_DIR="/lib/firmware"                  # firmware directory
CNFFILE="/tmp/.cocon.cnf"                     # parsed cocon.cnf
LOGFILE="/var/log/scan-cnf-from-storage.log"
CNF_NM_FILE_MOVETO="/tmp/.cocon.cnf.files/nm/"

# Unused on v9
#. /lib/lsb/init-functions

# Instead, Output to log.
log_info_msg()
{ 
  echo "$1" >> $LOGFILE
}


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


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

  userconfig_storefile="$CNFFILE"
  nmconfig_storepath="$CNF_NM_FILE_MOVETO"

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

  if [ $# -ge 4 ];
  then
    nmconfig_storepath="$4"   # coconnm store path
  fi

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

  copy_coconcnf_to_tmp "$userconfig_scanpath" "$userconfig_storefile"

  copy_firmware "$userconfig_scanpath"

  copy_coconnm "$userconfig_scanpath" "$nmconfig_storepath"
}


copy_coconcnf_to_tmp()
{
  # cocon.cnf and related files
  if [ -r "$1/cocon.cnf" ];
  then
    log_info_msg " --> cocon.cnf found ($1/cocon.cnf)"
    log_info_msg "$( cat "$1/cocon.cnf" )"
    log_info_msg "====="
    cocon-read-cnf "$1/cocon.cnf" >> "$2"
    log_info_msg "parsed data ($2) ==>"
    log_info_msg "$( cat "$2" )"
    log_info_msg "====="
  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
}


copy_coconnm()
{
  # NetworkManager Settings
  if [ -d "$1/coconnm" ];
  then
    if [ ! -d "$2" ];
    then
      mkdir -p "$2"
    fi

    log_info_msg "--> NetworkManager setting directory found"
    for nm in $1/coconnm; do
      if ! grep -q '\#\!\/' < "$nm";
      then
        # TODO : more more inf file check
        #cp $CONF_MOUNT/coconnm/$nm $CNF_NM_FILE_MOVETO
        # copy NetworkManager strrings, but delete mac address.
        sed '/mac-address=/d' "$1/coconnm/$nm" > "$2/$nm"
        chmod 600 "$2/$nm"
      fi
    done
  fi
}


# factory setting : scan cocon.cnf only 
read_factory_coconcnf()
{
  cocon-read-cnf $DEFCONF >> $CNFFILE
}


# cocon.cnf and some files passed from initrd
scan_coconcnf_from_initrd()
{
  scan_coconcnf_files "$SECCONF" "$SECCONF"
}


# Extreme mode : scan cocon.cnf on root directory
scan_coconcnf_rootdir()
{
  cocon-read-cnf $SECCONF_EXTREME >> $CNFFILE
}


# Scan disks (except boot device) and pickup cocon.cnf and some files
scan_coconcnf_from_partiton()
{
  #if [ "$ROOT_DEVICE" ];
  #then
  #  # Pickup root device from initrd env. ( -> like sda1 )
  #  rootdv=`basename $ROOT_DEVICE`
  #fi

  log_info_msg "diskstat is : "
  log_info_msg "$(cat /proc/diskstats)"

  while read maj min dev ext; do

    #log_info_msg "Scanning : $dev"

    # 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 [ "$ROOT_DEVICE" = "/dev/$dev" ];
    then
       # This is booted CD drive (or USB Stick, HDD, etc),
       # and maybe already scanned on initrd.
       # then ignore this drive.
       log_info_msg " is booted device."
       continue;
    fi

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

    if [ "$fstype" = "iso9660" -o "$fstype" = "vfat" -o "$fstype" = "ext3" -o "$fstype" = "ntfs" -o "$fstype" = "ext4" ];
    then
      mount -o ro "/dev/$dev" "$CONF_MOUNT"
      # Read config
      scan_coconcnf_files "$CONF_MOUNT" "/dev/$dev"
      sync
      umount "/dev/$dev"
    fi
  done < /proc/diskstats
}


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

      # Read opencocon default cocon.
      read_factory_coconcnf

      # Do scan and store
      scan_coconcnf_from_partiton

      if [ "$ROOT_DEVICE" ];
      then
        # Then, scan directory which generated on initrd.
        scan_coconcnf_from_initrd
      else
        # Extreme mode : only scan cocon.cnf on root directory
        scan_coconcnf_rootdir
      fi

      ;;

   stop)
      log_info_msg "Stopping $0"

      # clear settings
      rm $CNFFILE
      rm -rf $CONF_MOUNT/coconnm
      ;;

   restart)
      ${0} stop
      sleep 1
      ${0} start
      ;;

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

exit 0

