# old version (without -$pkgrel): ${1%%-*}
# new version (without -$pkgrel): ${2%%-*}

PKGNAME="init-system-helpers"

# arg 1:  the new package version
pre_install() {
    pre_upgrade
}

# arg 1:  the new package version
post_install() {
    post_upgrade $@
}

# arg 1:  the new package version
# arg 2:  the old package version
pre_upgrade() {
    [[ -L /etc/init.d ]] && rm -f /etc/init.d 2>/dev/null
    [[ -L /etc/rc.d/init.d ]] && rm -f /etc/rc.d/init.d 2>/dev/null
    for i in 0 1 2 3 4 5 6 S; do
	[[ -L /etc/rc${i}.d ]] && rm -f /etc/rc${i}.d 2>/dev/null
	[[ -d /etc/rc.d/rc${i}.d ]] && rm -rdf /etc/rc.d/rc${i}.d 2>/dev/null
    done
}

# arg 1:  the new package version
# arg 2:  the old package version
post_upgrade() {
    [[ -d /etc/init.d ]] || install -dm0755 /etc/init.d
    [[ -d /etc/rc.d/init.d ]] || install -dm0755 /etc/rc.d/init.d
    for i in 0 1 2 3 4 5 6 S; do
    	install -dm0755 /etc/rc${i}.d
	[[ -L /etc/rc.d/rc${i}.d ]] || ln -s /etc/rc${i}.d /etc/rc.d/rc${i}.d 2>/dev/null
    done
    while read -u3 FILE_SERVICE; do
	[[ -e /etc/init.d/${FILE_SERVICE} ]] || ln -s /etc/rc.d/init.d/${FILE_SERVICE} /etc/init.d/ 2>/dev/null
    done 3< <(ls -1 /etc/rc.d/init.d/ 2>/dev/null | grep -vE "README|functions")
}

# arg 1:  the old package version
pre_remove() {
    true
}

# arg 1:  the old package version
post_remove() {
    true
}
