#!/bin/sh

. /lib/functions.sh
. /lib/functions/system.sh

is_ios_enabled || exit 0

CONFIG_FILE="$UCI_CONFIG_DIR/ioman"
[ -f "$CONFIG_FILE" ] || exit 0

fix_old_scheduler() {
	local section="$1"

	uci_remove "ioman" "$section" "gpio"
	uci_set "ioman" "$section" "pin" "dout1"
}

migrate_from_old_ioman() {
	uci_get "ioman" "pin1" || return 0

	# have old conf, migrate
	local CONFIG="
config gpio 'dout1'
	option direction 'out'
	option value '0'
	option bi_dir '0'

config gpio 'din1'
	option direction 'in'
	option value '0'
	option bi_dir '0'
	option invert_input '0'
"

	config_load "ioman"
	config_foreach fix_old_scheduler "scheduler"

	uci_remove "ioman" "pin1"
	uci_remove "ioman" "pin2"
	uci_commit "ioman"

	echo "$CONFIG" >> $CONFIG_FILE
}

migrate_from_old_ioman
exit 0
