#
# Copyright (c) 2016 D. Richard Hipp
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the Simplified BSD License (also
# known as the "2-Clause License" or "FreeBSD License".)
#
# This program is distributed in the hope that it will be useful,
# but without any warranty; without even the implied warranty of
# merchantability or fitness for a particular purpose.
#
# Author contact information:
#   drh@hwaci.com
#   http://www.hwaci.com/drh/
#
############################################################################
#
# The "settings" and "unset" commands that may modify the repository.
#

set path [file dirname [info script]]

require_no_open_checkout

test_setup

###############################################################################
#
# Complete syntax as tested:
#
#     fossil settings ?PROPERTY? ?VALUE? ?OPTIONS?
#     fossil unset PROPERTY ?OPTIONS?
#
# Where the only supported options are "--global" and "--exact".
#
###############################################################################

set all_settings [get_all_settings]

foreach name $all_settings {
  #
  # HACK: Make 100% sure that there are no non-default setting values
  #       present anywhere.
  #
  fossil unset $name --exact --global
  fossil unset $name --exact

  #
  # NOTE: Query for the hard-coded default value of this setting and
  #       save it.
  #
  fossil test-th-eval "setting $name"
  set defaults($name) [normalize_result]
}

###############################################################################

fossil settings bad-setting some_value

test settings-set-bad-local {
  [normalize_result] eq "no such setting: bad-setting"
}

fossil settings bad-setting some_value --global

test settings-set-bad-global {
  [normalize_result] eq "no such setting: bad-setting"
}

###############################################################################

fossil unset bad-setting

test settings-unset-bad-local {
  [normalize_result] eq "no such setting: bad-setting"
}

fossil unset bad-setting --global

test settings-unset-bad-global {
  [normalize_result] eq "no such setting: bad-setting"
}

###############################################################################

fossil settings ssl some_value

test settings-set-ambiguous-local {
  [normalize_result] eq
  "ambiguous setting \"ssl\" - might be: ssl-ca-location ssl-identity"
}

fossil settings ssl some_value --global

test settings-set-ambiguous-global {
  [normalize_result] eq
  "ambiguous setting \"ssl\" - might be: ssl-ca-location ssl-identity"
}

###############################################################################

fossil unset ssl

test settings-unset-ambiguous-local {
  [normalize_result] eq
  "ambiguous setting \"ssl\" - might be: ssl-ca-location ssl-identity"
}

fossil unset ssl --global

test settings-unset-ambiguous-global {
  [normalize_result] eq
  "ambiguous setting \"ssl\" - might be: ssl-ca-location ssl-identity"
}

###############################################################################

set pattern(1) {^%name%$}
set pattern(3) {^%name%[ ]+\(global\)[ ]+%value%+$}
set pattern(4) {^%name%[ ]+\(local\)[ ]+%value%+$}

foreach name $all_settings {
  if {$name ne "manifest"} {
    set value #global_for_$name
    fossil settings $name $value --exact --global
    set data [normalize_result]

    test settings-set-$name-global {
      $data eq ""
    }

    fossil settings $name --exact --global
    set data [normalize_result]

    test settings-set-check1-$name-global {
      [regexp -- [string map \
          [list %name% $name %value% $value] $pattern(3)] $data]
    }

    fossil test-th-eval --open-config "setting $name"
    set data [normalize_result]

    test settings-set-check2-$name-global {
      $data eq $value
    }

    fossil unset $name --exact --global
    set data [normalize_result]

    test settings-unset-$name-global {
      $data eq ""
    }

    fossil settings $name --exact --global
    set data [normalize_result]

    test settings-unset-check1-$name-global {
      [regexp -- [string map \
          [list %name% $name %value% $value] $pattern(1)] $data]
    }

    fossil test-th-eval --open-config "setting $name"
    set data [normalize_result]

    test settings-unset-check2-$name-global {
      $data eq $defaults($name)
    }
  }

  set value #local_for_$name
  fossil settings $name $value --exact
  set data [normalize_result]

  test settings-set-$name-local {
    $data eq ""
  }

  fossil settings $name --exact
  set data [normalize_result]

  test settings-set-check1-$name-local {
    [regexp -- [string map \
        [list %name% $name %value% $value] $pattern(4)] $data]
  }

  fossil test-th-eval --open-config "setting $name"
  set data [normalize_result]

  test settings-set-check2-$name-local {
    $data eq $value
  }

  fossil unset $name --exact
  set data [normalize_result]

  test settings-unset-$name-local {
    $data eq ""
  }

  fossil settings $name --exact
  set data [normalize_result]

  test settings-unset-check1-$name-local {
    [regexp -- [string map \
        [list %name% $name %value% $value] $pattern(1)] $data]
  }

  fossil test-th-eval --open-config "setting $name"
  set data [normalize_result]

  test settings-unset-check2-$name-local {
    $data eq $defaults($name)
  }
}

###############################################################################

set pattern(5) \
  {^%name%[ ]+\n  \(overridden by contents of file \.fossil-settings/%name%\)$}

set versionable_settings [get_versionable_settings]
file mkdir .fossil-settings

foreach name $versionable_settings {
  fossil settings $name --exact
  set data [normalize_result]

  test settings-before-versionable-$name {
    [regexp -- [string map [list %name% $name] $pattern(1)] $data]
  }

  set value #versionable_for_$name
  set fileName [file join .fossil-settings $name]
  write_file $fileName $value

  fossil settings $name --exact
  set data [normalize_result]

  test settings-set-check1-versionable-$name {
    [regexp -- [string map [list %name% $name] $pattern(5)] $data]
  }

  fossil test-th-eval --open-config "setting $name"
  set data [normalize_result]

  test settings-set-check2-versionable-$name {
    $data eq $value
  }

  file delete $fileName

  fossil settings $name --exact
  set data [normalize_result]

  test settings-after-versionable-$name {
    [regexp -- [string map [list %name% $name] $pattern(1)] $data]
  }
}

###############################################################################

test_cleanup
