#!/bin/sh

set -e

case $1 in
  start|restart|reload|force-reload)
    ;;
  stop)
    exit 0
    ;;
  *)
    echo "Usage: $0 {stop|start|restart|reload|force-reload}" >&2
    exit 1
    ;;
esac

if [ -f /etc/hdparm.conf ]; then
  mv /etc/hdparm.conf /etc/hdparm.conf.bak
  discs=""
  dowrite=1
  while read line; do
    ## comment
    if [ "$(echo "$line" | egrep -e '^[[:space:]]*(#|$)')" != "" -a $dowrite -eq 1 ]; then
      echo $line >> /etc/hdparm.conf ; continue
    fi

    ## check if the disk exists 
    KEY=$(echo "$line" | awk '{ print $1; }')
    SEP=$(echo "$line" | awk '{ print $2; }')
    case "$SEP" in
      '{')
        p=${KEY/\/dev\//}
        m=""
        [ -f /proc/ide/$p/media ] && m="$(cat /proc/ide/$p/media)"
        case "$m" in
          "disk"|"cdrom") 
            dowirte=1 
            discs="$discs $p"
            ;;
          *) dowrite=0 ;;
        esac
        ;;
    esac

    ## write the line
    if [ $dowrite -eq 1 ]; then echo "$line" >> /etc/hdparm.conf ; fi
  done < /etc/hdparm.conf.bak

  ## check any new devices
  if [ -f /proc/partitions ]; then
    while read x x x p x; do
      case "$p" in
        hd?)
           m=""
           [ -f /proc/ide/$p/media ] && m="$(cat /proc/ide/$p/media)"
           case "$m" in 
             "disk"|"cdrom") ;;
             *) continue ;;
           esac
 
           found=0
           for disc in $discs; do
             if [ "$disc" = "$p" ]; then found=1; break; fi
           done
           if [ $found -eq 0 ]; then
             echo "/dev/$p {" >> /etc/hdparm.conf
             echo "    dma = off" >> /etc/hdparm.conf
             echo "}" >> /etc/hdparm.conf
           fi
           ;;
        sd?) ;;
        *) ;;
      esac
    done < /proc/partitions
  fi

else
  ## create new hdparm.conf
  cat <<-EOF > /etc/hdparm.conf
## This is the default configuration for hdparm for Debian.  It is a 
## rather simple script, so please follow the following guidelines :)
## Any line that begins with a comment is ignored - add as many as you 
## like.  Note that an in-line comment is not supported.  If a line 
## consists of whitespace only (tabs, spaces, carriage return), it will be
## ignored, so you can space control fields as you like.  ANYTHING ELSE
## IS PARSED!!  This means that lines with stray characters or lines that 
## use non # comment characters will be interpreted by the initscript.  
## This has probably minor, but potentially serious, side effects for your 
## hard drives, so please follow the guidelines.  Patches to improve 
## flexibilty welcome.  Please read/usr/share/doc/hdparm/README.Debian for 
## notes about known issues, especially if yu have an MD array.


# -q be quiet
quiet 
# -a sector count for filesystem read-ahead
#read_ahead_sect = 12
# -A disable/enable the IDE drive's read-lookahead feature
#lookahead = on
# -b bus state
#bus = on
# -B apm setting
#apm = 255
# -c enable (E)IDE 32-bit I/O support - can be any of 0,1,3
#io32_support = 1
# -d disable/enable the "using_dma" flag for this drive
#dma = off
# -D enable/disable the on-drive defect management
#defect_mana = off
# -E cdrom speed
#cd_speed = 16
# -m sector count for multiple sector I/O
#mult_sect_io = 32
# -P maximum sector count for the drive's internal prefetch mechanism
#prefetch_sect = 12
# -r read-only flag for device
#read_only = off
# -S standby (spindown) timeout for the drive
#spindown_time = 24
# -u interrupt-unmask flag for the drive
#interrupt_unmask = on
# -W Disable/enable the IDE drive's write-caching feature
#write_cache = off
# -X IDE transfer mode for newer (E)IDE/ATA2 drives
#transfer_mode = 34
# -y force to immediately enter the standby mode
#standby
# -Y force to immediately enter the sleep mode
#sleep
# -Z Disable the power-saving function of certain Seagate drives
#disable_seagate
# -M Set the acoustic management properties of a drive
#acoustic_management

#Samples follow:

#/dev/discs/disc0/disc {
#	mult_sect_io = 16
#	write_cache = off
#	spindown_time = 240
#}

#/dev/discs/disc1/disc {
#	mult_sect_io = 32
#	spindown_time = 36
#	write_cache = off
#}

#/dev/cdroms/cdrom0 {
#	dma = on		   
#	interrupt_unmask = on
#	io32_support = 0
#}
#/dev/hda {
#	mult_sect_io = 16
#	write_cache = off
#	dma = on
#}
EOF
 
  ## check devices and write the configuration
  if [ -f /proc/partitions ]; then
    while read x x x p x; do
      case "$p" in
        hd?)
           m=""
           [ -f /proc/ide/$p/media ] && m="$(cat /proc/ide/$p/media)"
           case "$m" in 
             "disk"|"cdrom") ;;
             *) continue ;;
           esac
 
           echo "/dev/$p {" >> /etc/hdparm.conf
           echo "    dma = off" >> /etc/hdparm.conf
           echo "}" >> /etc/hdparm.conf
           ;;
        sd?) ;;
        *) ;;
      esac
    done < /proc/partitions
  fi
fi
 
/etc/init.d/hdparm.real $1
